Reading list Switch to dark mode

    Set Custom Price for Product In Multiple Addresses Checkout

    Updated 11 January 2023

    Sometimes it is required to set the custom price of product on the go. The issue occurs when we try to Checkout with Multiple Addresses, the Subtotal for each address is again calculated and we get original price for the product.

    To update the Price of Product check file:

    vendor/magento/module-quote/Model/Quote/Address/Total/Subtotal.php, the price is get from product in _initItem() method and set in _calculateRowTotal() method.

        /**
         * Address item initialization
         *
         * @param Address $address
         * @param AddressItem|Item $item
         * @return bool
         */
        protected function _initItem($address, $item)
        {
            if ($item instanceof AddressItem) {
                $quoteItem = $item->getAddress()->getQuote()->getItemById($item->getQuoteItemId());
            } else {
                $quoteItem = $item;
            }
            $product = $quoteItem->getProduct();
            $product->setCustomerGroupId($quoteItem->getQuote()->getCustomerGroupId());
    
            /**
             * Quote super mode flag mean what we work with quote without restriction
             */
            if ($item->getQuote()->getIsSuperMode()) {
                if (!$product) {
                    return false;
                }
            } else {
                if (!$product || !$product->isVisibleInCatalog()) {
                    return false;
                }
            }
    
            $quoteItem->setConvertedPrice(null);
            $originalPrice = $product->getPrice();
            if ($quoteItem->getParentItem() && $quoteItem->isChildrenCalculated()) {
                $finalPrice = $quoteItem->getParentItem()->getProduct()->getPriceModel()->getChildFinalPrice(
                    $quoteItem->getParentItem()->getProduct(),
                    $quoteItem->getParentItem()->getQty(),
                    $product,
                    $quoteItem->getQty()
                );
                $this->_calculateRowTotal($item, $finalPrice, $originalPrice);
            } elseif (!$quoteItem->getParentItem()) {
                $finalPrice = $product->getFinalPrice($quoteItem->getQty());
                $this->_calculateRowTotal($item, $finalPrice, $originalPrice);
                $this->_addAmount($item->getRowTotal());
                $this->_addBaseAmount($item->getBaseRowTotal());
                $address->setTotalQty($address->getTotalQty() + $item->getQty());
            }
            return true;
        }
    
        /**
         * Processing calculation of row price for address item
         *
         * @param AddressItem|Item $item
         * @param int $finalPrice
         * @param int $originalPrice
         * @return $this
         */
        protected function _calculateRowTotal($item, $finalPrice, $originalPrice)
        {
            if (!$originalPrice) {
                $originalPrice = $finalPrice;
            }
            $item->setPrice($finalPrice)->setBaseOriginalPrice($originalPrice);
            $item->calcRowTotal();
            return $this;
        }

    You can use these methods to update the product price as per your condition.

    In your custom module, create a preference for the above file to update the price on multi-address checkout as per your requirement.

    Searching for an experienced
    Magento 2 Company ?
    Find out More

    Create a preference in /app/code/Vendor/YourModule/etc/frontend/di.xml as below:

    <?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>

    Now, create a class Subtotal.php in your module following the path Vendor\YourModule\Model\Quote\Address\Total.
    Add the below code in Subtotal.php file:

    <?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 here to get the custom price on multi-address
            $finalPrice = 10; // your final custom price
            $originalPrice = 10; // your original custom price
            $item->setPrice($finalPrice)->setBaseOriginalPrice($originalPrice);
            $item->calcRowTotal();
            return $this;
        }
    }

    Run the command php bin/magento setup:di:compile.

    After the successful completion of the compilation, the price (or the subtotal) for the address is updated on the multi-address checkout as given below: (In our case: $10)

    custom-price-on-multi-address-checkout

    Hope, this may help someone while setting custom price for product. Thanks 🙂

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home