I am adding product to cart in magento 2 programmatically here is the code for that:

public function getProduct($productId)
        //\Magento\Catalog\Model\Product $product

        if ($productId) {
            $storeId = $this->_objectManager->get(
            try {
                return $this->_productRepository->getById($productId, false, $storeId);
            } catch (NoSuchEntityException $e) {
                return false;
        return false;

and here is my code for adding product to cart:

  $product = $this->getProduct((int)$inputRequest['product']);

                    $this->_cart->addProduct($product, $inputRequest);

but when I save the cart, it saves the product in database but the data is zero for everything like price, discount amount and other attributes of items are zero, only quantity is correct which I am putting as input in $inputRequest. I have also called collectTotals and updateItems method of cart but there is no change in data.

Here is the quote_item table (I included the most important attributes), the most recent product (Impulse duffle) was added programmetically and it has zero price, row_total.

enter image description here

If I update cart on cart page, then prices are updated. May be I am missing to dispatch some event? Has someone else faced the same issue before?



Your Answer

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

Browse other questions tagged or ask your own question.