2

I want to remove shipping amount from cart page because we do not provide estimate shipping amount calculation block.

Also, when customer add product to cart then its display grand total without shipping amount but if customer in checkout page and select shipping method and again back to cart page then cart grand total display amount including shipping amount and shipping method and amount not display here.

Please see below images. enter image description here

enter image description here

For this I have override cart index controller in custom module and code is as below.

1) app/code/Hs/Mageenhancement/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Controller\Cart\Index" type="Hs\Mageenhancement\Controller\Cart\Index" />
</config>

2) app/code/Hs/Mageenhancement/Controller/Cart/Index.php

    <?php

namespace Hs\Mageenhancement\Controller\Cart;

class Index extends \Magento\Checkout\Controller\Cart\Index
{
    /**
     * Shopping cart display action
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        $quote = $this->_checkoutSession->getQuote();
        $shippingMethod = $quote->getShippingAddress()->getShippingMethod();
        if($shippingMethod){
            $quote->getShippingAddress()->setShippingMethod(null);  //setting method to null
            $quote->save();

            $quote->collectTotals();
            $quote->save();
        }
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__('Shopping Cart'));
        return $resultPage;
    }
}

I have used same below technique in Magento 1 and its working fine but in Magento 2 its not working.

$quote = $this->_checkoutSession->getQuote();
        $shippingMethod = $quote->getShippingAddress()->getShippingMethod();
        if($shippingMethod){
            $quote->getShippingAddress()->setShippingMethod(null);  //setting method to null
            $quote->save();

            $quote->collectTotals();
            $quote->save();
        }

Please help me how to remove shipping charge in cart page.

4

1 Answer 1

0

Try This

app/code/VendoreName/ModuleName/etc/frontend

di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Checkout\Controller\Cart\Index" type="VendoreName\ModuleName\Controller\Cart\Index" />
</config>

app/code/VendoreName/ModuleName/Controller/Cart

Index.php

<?php
/**
 *
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace VendoreName\ModuleName\Controller\Cart;

use Magento\Framework\App\Action\HttpGetActionInterface as HttpGetActionInterface;

class Index extends \Magento\Checkout\Controller\Cart\Index implements HttpGetActionInterface
{
    /**
     * @var \Magento\Checkout\Model\Session
     */
    protected $checkoutSession;

    /**
     * @var \Magento\Framework\View\Result\PageFactory
     */
    protected $resultPageFactory;

    /**
     * @param \Magento\Framework\App\Action\Context $context
     * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
     * @param \Magento\Checkout\Model\Session $checkoutSession
     * @param \Magento\Store\Model\StoreManagerInterface $storeManager
     * @param \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator
     * @param \Magento\Checkout\Model\Cart $cart
     * @param \Magento\Framework\View\Result\PageFactory $resultPageFactory
     * @codeCoverageIgnore
     */
    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Store\Model\StoreManagerInterface $storeManager,
        \Magento\Framework\Data\Form\FormKey\Validator $formKeyValidator,
        \Magento\Checkout\Model\Cart $cart,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory
    ) {
        parent::__construct(
            $context,
            $scopeConfig,
            $checkoutSession,
            $storeManager,
            $formKeyValidator,
            $cart,
            $resultPageFactory
        );
        $this->checkoutSession = $checkoutSession;
        $this->resultPageFactory = $resultPageFactory;
    }

    /**
     * Shopping cart display action
     *
     * @return \Magento\Framework\View\Result\Page
     */
    public function execute()
    {
        $quote = $this->checkoutSession->getQuote();
        $shippingMethod = $quote->getShippingAddress()->getShippingMethod();
        if ($shippingMethod) {
            $quote->getShippingAddress()->setShippingMethod(null);
            $quote->save();
            $quote->collectTotals();
            $quote->save();
        }
        $resultPage = $this->resultPageFactory->create();
        $resultPage->getConfig()->getTitle()->set(__('Shopping Cart'));
        return $resultPage;
    }
}

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.

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