3

For example. There is 1 product in the cart with base price 35. Working code:

foreach ($this->cart->getQuote()->getAllVisibleItems() as $item) {
    $item->setCustomPrice(11.11)
        ->setOriginalCustomPrice(11.11)
        ->getProduct()->setIsSuperMode(true);
}
// product rowTotal: 11.11
// cart subtotal/grandtotal: 11.11

NOT working code:

$this->cart->getQuote()->collectTotals(); // Attention to this line
foreach ($this->cart->getQuote()->getAllVisibleItems() as $item) {
    $item->setCustomPrice(11.11)
        ->setOriginalCustomPrice(11.11)
        ->getProduct()->setIsSuperMode(true);
}
// product rowTotal: 35.00
// product displayed price (customPrice): 11.11
// cart subtotal/grandtotal: 35.00

what I was trying to do:

$this->cart->getQuote()->collectTotals();
foreach ($this->cart->getQuote()->getAllVisibleItems() as $item) {
    $item->setCustomPrice(11.11)
        ->setOriginalCustomPrice(11.11)
        ->getProduct()->setIsSuperMode(true);
    $item->calcRowTotal();
}
 $this->cart->getQuote()->setTotalsCollectedFlag(false);
/** @var \Magento\Quote\Model\Quote\Address $address */
foreach ($this->cart->getQuote()->getAllAddresses() as $address) {
    $address->setCollectShippingRates(true);
}

Unfortunately, it doesn't work.

Also it fixes if I reload cart page, but how to do it correctly without reload? What things should I re-set like $quote->setTotalsCollectedFlag(false) and $address->setCollectShippingRates(true)

And there is a question: What am I doing wrong? :)

UPDATE:

Magento version 2.2.2

It happens if quote already exists in the database (even if we have an empty cart). If I clear cookies / delete quotes from quote table, first product add doesn't proceed the trouble.

  • in which file or observer you are using to make this changes? – Abhishek Panchal Mar 15 '18 at 21:06
  • @AbhishekPanchal, I was trying Observer to cart_product_add_after event and plugin of method addProduct() in the Cart class – spiil Mar 15 '18 at 21:40
  • How many types of products you have ? Just simple?? – Abhishek Panchal Mar 15 '18 at 21:43
  • Yes. At this moment just simple. I know, if a product has a parent, I need to set customPrice to the parent product. – spiil Mar 15 '18 at 22:13
1

I've dig into the core and found a lot of unclear things, so I made this helpful but very dirty hack:

public function updateItemCustomPrice($item, $price)
{
    $item->setCustomPrice($price)
        ->setOriginalCustomPrice($price)
        ->setBaseTaxCalculationPrice($price)
        ->setTaxCalculationPrice($price)
        ->getProduct()->setIsSuperMode(true);
    $item->calcRowTotal();
}

Now I use this code instead of

$item->setCustomPrice(11.11)
    ->setOriginalCustomPrice(11.11)
    ->getProduct()->setIsSuperMode(true);
$item->calcRowTotal();

I'm still not sure of the correctness of this solution, so I invite you in the comments.

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.