0

Magento 2.4

I already have my observer in place, but setting a custom subtotal for my item does not work. I am listening to the checkout_cart_product_add_after event. This is my observer code:

$item = $observer->getEvent()->getData('quote_item');
$item->setQty(3); // works
$item->setCustomPrice(250); // works
$item->setOriginalCustomPrice(250); // works
$item->getQuote()->setSubtotal(250); // does NOT work ?
$item->setSubtotal(250); // does NOT work either?

No matter, it always multiplies the 250 (custom price) by 3 (custom qty). Any ideas how to solve this?

3
  • Maybe try var_dump($item->getQuote()->getId()); and check that this is retrieving the quote correctly.
    – rushtoni88
    May 5, 2022 at 11:35
  • It is the correct quote, since the price can be changed...or am I wrong? May 5, 2022 at 13:49
  • I think because it's the add product event you can access the "quote item" but if the snippet I put above doesn't give you a valid ID then you may not be able to access a quote via one of its products and may need to get the quote separately.
    – rushtoni88
    May 10, 2022 at 8:20

1 Answer 1

0

Check to see if $item->getQuote()->getId() gives a valid quote ID. If it does not then you may not have access to the quote itself and can try adding:

use \Magento\Checkout\Model\Cart;

...

/**
* @var \Magento\Checkout\Model\Cart
*/
protected $cart;

...

public function __construct(
        ...
        Cart $cart
        ...
    ) {

...

$quote = $cart->getQuote();

You should then have access to the quote itself. Note: This was done from memory so shout if you have any issues and I will take a closer look.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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