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?

3

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.