0

I am using event - checkout_cart_save_after to update cart item's price. But Item price is not getting updated.

Below is the code.

foreach ($observer->getCart()->getQuote()->getAllVisibleItems() as $item) {
    if ($item->getParentItem()) {
        $item = $item->getParentItem();
    }

    $item_price = $item->getProduct()->getFinalPrice();

    $price_for_tier = $item_price + 50;

    $item->setCustomPrice($price_for_tier);
    $item->setOriginalCustomPrice($price_for_tier);
    $item->getProduct()->setIsSuperMode(true);
}
6
  • Check first observer class is call or not? Commented Jan 30, 2018 at 5:49
  • yes its getting called. I can echo price.
    – Nitz
    Commented Jan 30, 2018 at 5:50
  • Try it with checkout_cart_product_add_after event. Commented Jan 30, 2018 at 5:54
  • I tried. It will update only one item price and not all
    – Nitz
    Commented Jan 30, 2018 at 5:57
  • You can get cart items like this $item = $observer->getEvent()->getData('quote_item'); $item = ( $item->getParentItem() ? $item->getParentItem() : $item ); After you can use $item in foreach. Commented Jan 30, 2018 at 6:00

2 Answers 2

0

if you see "sales_order_item" item table in Database then you can find below fields which are related to price

"price"
"base_price"
"original_price"
"base_original_price"
"price_incl_tax"
"base_price_incl_tax"

So you can use like below

$item->setPrice();
$item->setBasePrice();
$item->setOriginalPrice();
$item->setOriginalBasePrice();
$item->setPriceInclTax();
$item->setBasePriceInclTax();

Please Note: You also need to other filed like "raw_total","base_row_total" etc.

0

It worked with below code -

$cart = $observer->getData('cart');

$quote = $cart->getData('quote');

$items = $quote->getAllVisibleItems();

foreach($items as $item)
{
    $item->setCustomPrice(0);
    $item->setOriginalCustomPrice(0);
    $item->getProduct()->setIsSuperMode(true);
    $item->save();
}
$quote->save();
$quote->setTotalsCollectedFlag(false)->collectTotals();

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.