0

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?

asked May 27, 2021 at 9:29
3
  • When your controller will call on checkout page? Commented May 28, 2021 at 5:53
  • The controller called when the customer selects a timeslot. BTW, it has been done. Commented 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-totals Commented May 28, 2021 at 7:13

0

Know someone who can answer? Share a link to this question via email, Twitter, or Facebook.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.