1

I've been struggling with this issue for the past few weeks now.

My goal is to set a custom price for an item after I edit them on Cart.

Basically, I'm using an observer on the event checkout_cart_update_item_complete, and on that observer I eventually do the following:

$item->setCustomPrice($price);
$item->setOriginalCustomPrice($price);
$item->getProduct()->setIsSuperMode(true);
$item->save();

Where $item is the new quote item after the update (check Magento\Checkout\Controller\Cart\UpdateItemOptions.php) and $price is my custom price.

After this, the quote item price gets the value of 0, even though the custom_price field on database is actually the value that I wanted.

Any help is appreciated :)

1
  • have you check that the controller comes in that file or not? – Balwant Singh Dec 4 '18 at 17:38
0

Try to write your custom code under process() function.

vendor\magento\module-sales-rule\Model\Validator.php

/**
     * Quote item discount calculation process
     *
     * @param AbstractItem $item
     * @return $this
     */
    public function process(AbstractItem $item)
    {
     ..............
     ..............
     $price = 50;
     $item->setCustomPrice($price);
     $item->setOriginalCustomPrice($price);
     $item->getProduct()->setIsSuperMode(true);
     ..............
     ..............

    }

NOTE: Don't use $item->save() once and try.

I hope it help..!!

5
  • Tried it, didn't work :( even without the $item->save(). Thanks anyways! – gasparrmg Dec 5 '18 at 9:50
  • @gasparrmg have you tried your code in this vendor\magento\module-sales-rule\Model\Validator.php file? – Balwant Singh Dec 5 '18 at 10:00
  • Yes, it did not work. But even if it did, I wanted to this through my observer and not by extending/overriding this class :/ – gasparrmg Dec 5 '18 at 10:01
  • @BalwantSingh, same i am facing, without save() also its not working – jafar pinjar Mar 1 '19 at 16:21
  • It is not working. – Dhaduk Mitesh Apr 22 '19 at 7:33
0

You should override Magento\Catalog\Model\Product\Type\Price

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\Catalog\Model\Product\Type\Price" type="SY\Modulename\Model\Catalog\Product\Type\Price" />
</config>

and redefine getBasePrice

<?php 
namespace SY\Modulename\Model\Catalog\Product\Type;
class Price extends \Magento\Catalog\Model\Product\Type\Price {
    public function getBasePrice($product, $qty = null) {
        $price = (float) $product->getPrice();
        return min(
            $this->_applyTierPrice($product, $qty, $price),
            $this->_applySpecialPrice($product, $price),
            1.11 // where is your custom product price
        );
    }
}

for some reasons magento can ignore override for Magento\Catalog\Model\Product\Type\Price and then you should override Magento\Catalog\Model\Product\Type\FrontSpecialPrice

Also you can do this via observer

events.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="catalog_product_get_final_price">
        <observer name="custom_price" instance="SY\Modulename\Observer\CatalogProductGetFinalPrice"/>
    </event>
</config>

and

<?php 
namespace SY\Modulename\Observer;
class CatalogProductGetFinalPrice implements \Magento\Framework\Event\ObserverInterface {
    public function execute(\Magento\Framework\Event\Observer $observer){
        $_product->setFinalPrice(your custom price);
        return $this;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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