3

Custom product price not working for multi shipping checkout.

Here is my code for the custom product price

app\code\Custom\Module\etc\frontend\events.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
 <event name="checkout_cart_product_add_after">
     <observer name="customprice" instance="Custom\Module\Observer\Customprice" />
 </event>
</config>

app\code\Custom\Module\Observer\Customprice.php

<?php
namespace Custom\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class Customprice implements ObserverInterface
{
 public function execute(\Magento\Framework\Event\Observer $observer) {
     $item = $observer->getEvent()->getData('quote_item');
     $item = ( $item->getParentItem() ? $item->getParentItem() : $item );
     $price = 100; //set your price here
     $item->setCustomPrice($price);
        $item->setOriginalCustomPrice($price);
        $item->getProduct()->setIsSuperMode(true);
 }

}
3
  • have you get any solution for this issue @Lokesh M Oct 6, 2020 at 5:24
  • @divyasekar can you please upload screenshot? Oct 6, 2020 at 7:04
  • can you try to move your events.xml to etc folder? Oct 7, 2020 at 14:11

1 Answer 1

3
+50

The prices of the items are set in the following functionality:

\Magento\Quote\Model\Quote\Address\Total\Subtotal

Inside the _initItem() function you can find the logic that obtains the final price for configurable or simple products, in this logic the$this->_calculateRowTotal($item, $finalPrice, $originalPrice) function is called which sets the Item price.

You can use a <preference /> and override the logic of one of those methods (_initItem() or _calculateRowTotal) to apply your custom price, for example:

MODULE_DIR/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\Quote\Model\Quote\Address\Total\Subtotal" type="Vendor\YourModule\Model\Quote\Address\Total\Subtotal"/>
</config>

Create your custom class

<?php

namespace Vendor\YourModule\Model\Quote\Address\Total;

class Subtotal extends \Magento\Quote\Model\Quote\Address\Total\Subtotal
{
    /**
     * {@inheritDoc}
     */
    protected function _calculateRowTotal($item, $finalPrice, $originalPrice)
    {
        // apply your logic to get the custom prices
        $finalPrice = 10; // your final custom price
        $originalPrice = 10; // your original custom price
        $item->setPrice($finalPrice)->setBaseOriginalPrice($originalPrice);
        $item->calcRowTotal();
        return $this;
    }
}
4
  • Let me check and update you Oct 8, 2020 at 4:34
  • I was get the custom price. Where going to set my custom price in the above function Oct 8, 2020 at 9:21
  • When i click a checkout with multiple address I get the custom price value as null Oct 8, 2020 at 12:32
  • @divyasekar You have to change the $finalPrice and $originalPrice values using your custom logic. Oct 8, 2020 at 13:56

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.