I have delivery timeslot options on the checkout page. I have added the delivery charge according to the timeslot.
I want to update the shipping amount if a customer selects a timeslot. I have used a controller which is working onchange timeslot & getting timeslot & delivery charge inside the controller.
Now I want to update the shipping amount on that basis.
My controller code:
<?php
namespace Ktpl\Custom\Controller\Dateslot;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Magento\Quote\Model\Quote;
use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
class Apply extends \Magento\Framework\App\Action\Action
{
protected $_http;
protected $_quote;
protected $_checkoutSession;
protected $_result;
private $timeZoneResolver;
public function __construct(
Context $context,
Http $http,
Quote $quote,
CheckoutSession $checkoutSession,
TimezoneInterface $timeZoneResolver
){
parent::__construct($context);
$this->_http = $http;
$this->_quote = $quote;
$this->_checkoutSession = $checkoutSession;
$this->timeZoneResolver = $timeZoneResolver;
}
public function execute()
{
$results = array();
$timeslot = $this->getHttp()->getParam('timeslot');
$deliveryCharge = $this->getHttp()->getParam('delivery_charge');
$quote = $this->getQuote();
try{
$quote->setTimeslot($timeslot);
$quote->save();
$this->_result['status'] = 200;
$this->_result['message'] = __('timeslot has been applied successfully.');
}
catch(Exception $e){
$this->_result['status'] = 201;
$this->_result['message'] = $e->getMessage();
}
echo json_encode($this->_result);exit;
}
public function getHttp(){
return $this->_http;
}
public function getQuote(){
return $this->_checkoutSession->getQuote();
}
/**
* Get prepared date/time
*
* @param string $date
* @param int|null $time
* @return string
*/
private function getPreparedDateTime($date, $time = null)
{
$timezone = $this->timeZoneResolver->getConfigTimezone();
$date = new \DateTime($date, new \DateTimeZone($timezone));
if ($time) {
$date->add(new \DateInterval('PT' . $time . 'S'));
}
return $this->timeZoneResolver->convertConfigTimeToUtc($date);
}
}
Can anyone suggest how can I update the shipping amount from this controller?
-
When your controller will call on checkout page?ZealousWeb– ZealousWeb2021年05月28日 05:53:24 +00:00Commented May 28, 2021 at 5:53
-
The controller called when the customer selects a timeslot. BTW, it has been done.Vinod Kumar– Vinod Kumar2021年05月28日 07:12:29 +00:00Commented May 28, 2021 at 7:12
-
I want to show this amount in the checkout summary but this is not working. I followed this URL: apptha.com/blog/magento-2-add-extra-fee-to-order-totalsVinod Kumar– Vinod Kumar2021年05月28日 07:13:54 +00:00Commented May 28, 2021 at 7:13