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="MagentoCheckoutCustomerDataCart">
<plugin name="plugin_name" type="VendorModulePluginFrontendMagentoCheckoutCustomerDataCart" sortOrder="20" disabled="false"/>
</type>
VendorModulePluginFrontendMagentoCheckoutCustomerDataCart.php
use MagentoCheckoutModelSession as CheckoutSession;
use MagentoCheckoutHelperData as CheckoutHelper;
use MagentoQuoteModelQuote;
use MagentoCatalogModelProductType;
use MagentoCatalogModelProductFactory;
use MagentoConfigurableProductModelProductTypeConfigurable;
use MagentoFrameworkExceptionNoSuchEntityException;
public function afterGetSectionData(
MagentoCheckoutCustomerDataCart $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;
}