3

I want to set shipping method when product is added to cart and want to change shipping method based on products added to cart.I have tried multiple way but unable to set and change shipping method when product is added to cart.I want to achieve that when product is added to cart shipping method is set or changed based on condition and total in minicart also rendered including shipping cost.

1 Answer 1

1

If you want to add shipping method after add to cart first of all apply after plugin to execute function of Add to cart Controller.

di.xml

<type name="Magento\Checkout\Controller\Cart\Add">
        <plugin name="addtocart_after" type="vendor\module\Plugin\Cart\AddToCart"/>
</type>

vendor\module\Plugin\Cart\AddToCart.php

<?php
namespace vendor\module\Plugin\Cart;


class AddToCart
   {

    public function __construct(
        \Magento\Checkout\Model\Session  $checkoutSession,
        \Magento\Quote\Model\QuoteRepository $quoteRepository
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->quoteRepository = $quoteRepository;
    }
    public function afterExecute(\Magento\Checkout\Controller\Cart\Add $subject, $result)
    {
        $quote = $this->checkoutSession->getQuote();
        if($quote->getItemsCount() == "1"){
            $quote = $this->checkoutSession->getQuote();
            $shippingMethod= 'flatrate_flatrate';
            $this->setQuoteShippingMethod($shippingMethod);
        } else {
            $quote = $this->checkoutSession->getQuote();
            $shippingMethod= 'freeshipping_freeshipping';
            $this->setQuoteShippingMethod($shippingMethod);
        }
        return $result;
    }
    public function setQuoteShippingMethod($method)
    {
        
            $quoteId = $this->checkoutSession->getQuote()->getId();
            $quote = $this->quoteRepository->get($quoteId);
            $shippingAddress = $quote->getShippingAddress();
            $country = 'AE'; // Some country code
            $postcode = '00000'; // Some postcode
            $regionId = '0'; // Some region id
            $shippingAddress = $quote->getShippingAddress();
            $shippingAddress
                ->setCountryId($country)
                ->setRegionId($regionId)
                ->setPostcode($postcode)
                ->setShippingMethod($method)
                ->setCollectShippingRates(true);
            $shippingAddress->save();
            $quote->collectTotals();
            $this->quoteRepository->save($quote);
    }

}

To show Cart totals in minicart

di.xml

<type name="Magento\Checkout\CustomerData\Cart">
        <plugin name="custom_minicart_grand_total" type="Vendor\Module\Plugin\Checkout\CustomerData\Cart"/>
</type>

Vendor\Module\Plugin\Checkout\CustomerData\Cart.php

<?php

namespace vendor\module\Plugin\Checkout\CustomerData;

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Checkout\Helper\Data as CheckoutHelper;
use Magento\Quote\Model\Quote;

class Cart 
{
    /**
     * @var CheckoutSession
     */
    protected $checkoutSession;
    
    /**
     * @var CheckoutHelper
     */
    protected $checkoutHelper;

    /**
     * @var Quote|null
     */
    protected $quote = null;

    /**
     * @param CheckoutSession $checkoutSession
     * @param CheckoutHelper $checkoutHelper
     */
    public function __construct(
        CheckoutSession $checkoutSession,
        CheckoutHelper $checkoutHelper
    ) {
        $this->checkoutSession = $checkoutSession;
        $this->checkoutHelper = $checkoutHelper;
    }

    /**
     * Add grand total to result
     *
     * @param \Magento\Checkout\CustomerData\Cart $subject
     * @param array $result
     * @return array
     */
    public function afterGetSectionData(
        \Magento\Checkout\CustomerData\Cart $subject,
        $result
    ) {
        $totals = $this->getQuote()->getTotals();
        $result['grand_total'] = isset($totals['grand_total'])
            ? $this->checkoutHelper->formatPrice($totals['grand_total']->getValue())
            : 0;
        return $result;
    }

    /**
     * Get active quote
     *
     * @return Quote
     */
    protected function getQuote()
    {
        if (null === $this->quote) {
            $this->quote = $this->checkoutSession->getQuote();
        }
        return $this->quote;
    }
}

Now override this template file

magento/module-tax/view/frontend/web/template/checkout/minicart/subtotal/totals.html

<span class="price-wrapper" data-bind="html: cart().grand_total"></span>

You will get the Grand Total In Minicart.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.