0

I added the Shipping price, Subtotal and Grand Total to the Mini Cart. So that the customers can check their shipping price in the Mini Cart. I think the magento is stored the shipping price in Quote Object. And the Quote object is created after the product to the Cart. So I tried to add the shipping address(zipcode, regioncode, city) to the Quote Object. And I get the shipping address(zipcode, regioncode, city) from the customer IP. But it's not working.

Vendor/Module/etc/di.xml

<type name="Magento\Checkout\CustomerData\Cart">
        <plugin name="plugin_name" type="Vendor\Module\Plugin\Frontend\Magento\Checkout\CustomerData\Cart" sortOrder="20" disabled="false"/>
</type>

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

use Magento\Checkout\Model\Session as CheckoutSession;
use Magento\Checkout\Helper\Data as CheckoutHelper;
use Magento\Quote\Model\Quote;
use Magento\Catalog\Model\Product\Type;
use Magento\Catalog\Model\ProductFactory;
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
use Magento\Framework\Exception\NoSuchEntityException;

public function afterGetSectionData(
        \Magento\Checkout\CustomerData\Cart $subject,
        $result
    ) {
        //Set quote shipping address
        $ipData = $this->miniHelper->getIPData();

        if (!$this->customerCart->getQuote()->getShippingAddress()) {

            $this->customerCart->getQuote()->getShippingAddress()
                ->setCountryId('US')
                ->setCity($ipData['city'])
                ->setPostcode($ipData['zipCode'])
                ->setRegionId($ipData['regionCode'])
                ->setCollectShippingRates(true);
        }

        $this->quoteRepository->save($this->customerCart->getQuote());
$shippingCost = $this->customerCart->getQuote()->getShippingAddress()->getShippingAmount();
$result['shipping_cost'] = isset($shippingCost) ? $this->checkoutHelper->formatPrice($shippingCost) : 0;

return $result;
}

1 Answer 1

0

The solution was to add a function call I found in the CartController::indexAction function. $cart->init(); will set the cart's status to CHECKOUT_STATE_BEGIN and clear all associated addresses. So it needs to be called before getting the address objects. Such as:

if((isset($params['territory_value_submitted']) && !empty($params['territory_value_submitted']))) {
      $cart->init();

      $shipping_address = $cart->getQuote()->getShippingAddress();
      $billing_address = $cart->getQuote()->getBillingAddress();

Now changes made to the addresses won't be cleared out by $cart->init() being called on the cart page Also, the correct shipping methods could not be chosen without setting the CountryId for the shipping address. Since I only ship within the US all my shipping rules were restricted to the US and no matching rules were found. This was fixed by changing $shipping_address ->setCountryId('US')

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.