0

I want to add a free product to cart with another product. For that I wrote checkout_cart_product_add_after event observer. My problem is, I don't get the updated cart subtotal(after adding first product, it returns subtotal as 0). This is my observer code.

namespace StwBuyXGetY\BuyXGetY\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;
use Magento\Framework\Registry;

class AddfreeProduct implements ObserverInterface
{
    protected $_productRepository;
    protected $_cart;
    protected $formKey;
    public function __construct(
        \Magento\Checkout\Model\Cart $cart, 
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\Data\Form\FormKey $formKey){
        $this->cart = $cart;
        $this->formKey = $formKey;
        $this->logger = $logger;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $totals = $this->cart->getQuote()->getTotals();
        $subtotal = $totals['subtotal']->getValue();  
        $this->logger->debug($subtotal);
    }

}

enter image description here

enter image description here

Any help will be greatly appreciated.

3

3 Answers 3

0

We should use \Magento\Checkout\Model\Session:

/**
 * @var \Magento\Checkout\Model\Session
 */
protected $_checkoutSession;

public function __construct(
    \Magento\Checkout\Model\Session $checkoutSession,
    .....
)
{
    $this->_checkoutSession = $checkoutSession;

}

Try to get the totals:

//Get totals
$this->_checkoutSession->getQuote()->getTotals();
//Get sub total.
$this->_checkoutSession->getQuote()->getSubtotal();
13
  • Thanks Khoa, for your answer. But, my bad luck, it is not working :(
    – Hima Mohan
    Aug 26, 2016 at 6:27
  • You tried to dump out the data? Actually, your question also make me confused. You want to know the collect total data or you want to add an extra fee? Aug 26, 2016 at 6:30
  • I just want to get cart subtotal. But in checkout_cart_product_add_after observer I don't get it by using the code I mentioned above.
    – Hima Mohan
    Aug 26, 2016 at 6:35
  • You tried to dump out this data? Aug 26, 2016 at 6:38
  • Yes, I have used logger to print this data. But it displays 0.
    – Hima Mohan
    Aug 26, 2016 at 6:42
0

You can try

$this->_cartTotalRepository using \Magento\Quote\Model\Cart\CartTotalRepository

$totals = $this->_cartTotalRepository->get($this->cart->getQuote()->getId());
// Subtotal
$subtotal = (float) $totals->getSubtotal();

Let me know if you have any question.

0

To get the updated cart subtotal in the checkout_cart_product_add_after event observer in Magento 2, you can use the following code:

    use Magento\Framework\Event\ObserverInterface;
    use Magento\Framework\Event\Observer;

    class YourObserver implements ObserverInterface
    {
        protected $cart;

        public function __construct(
            \Magento\Checkout\Model\Cart $cart
        ) {
            $this->cart = $cart;
        }

        public function execute(Observer $observer)
        {
            $cartSubtotal = $this->cart->getQuote()->getSubtotal();
            // Do something with the cart subtotal
        }
    }

Here, we are injecting the Magento\Checkout\Model\Cart class in the constructor of the observer. This class provides methods to work with the current cart.

In the execute() method of the observer, we are getting the current cart subtotal by calling the getSubtotal() method on the quote object. The quote object represents the current cart and provides methods to work with the cart data.

Once you have the cart subtotal, you can perform any necessary actions on it within the observer.

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.