Product totals calculating to $0.00 when adding products to cart programmatically
I had this problem recently where the cart page was displaying $0.00 on all the product line items after I had programmatically added 3 products to my cart. I didn’t find a solution after a quick Google search, so after lots of CMD+B
in PHPStorm and reading the checkout/cart/add
controller, I had my solution. The block of code below is a re-creation of what I was doing to add these products to my cart.
->addAttributeToSelect('*');
to the collection, and my totals were calculating correctly again.
$this->customerCart->save()
, the quote begins $quote->collectTotals()
. Because the price
attribute was never loaded when I executed the collection, when $product->getData('price')
returns NULL
and ends up calculating all of the totals to $0.00.
You may ask “why select all attributes instead of just the price attribute?” I noticed that in the /checkout/cart/add
controller, the product is loaded via the Magento\Catalog\Model\ProductRepository
, which also loads all product attributes. Any third party module can also inject their own totals collector, and you never know what attributes they could need during calculations.