I created a new product programmatically and added it to the cart, but the subtotal value which is showing in the cart is 0.

use \Magento\Catalog\Model\ProductFactory;
use \Magento\Catalog\Api\ProductRepositoryInterface;


     * @var \Magento\Catalog\Model\ProductFactory
    protected $productFactory;

     * @var \Magento\Catalog\Api\ProductRepositoryInterface
    protected $productRepository;


    public function __construct(
        ProductFactory $productFactory,
        ProductRepositoryInterface $productRepository,
    ) {
        $this->productFactory = $productFactory;
        $this->productRepository = $productRepository;


$product = $this->productFactory->create();

try {
    $product = $this->productRepository->save($product);
} catch (CouldNotSaveException $e) {
    $this->logger->info("CouldNotSaveException : ". $e->getMessage());
} catch (InputException $e) {
    $this->logger->info("InputException: " . $e->getMessage());
} catch (StateException $e) {
    $this->logger->info("StateException: ". $e->getMessage());



price subtotal is 0

2 Answers 2


Add $this->quote->collectTotals(); before quote save. You need to recalculate totals (including subtotal) after adding product to quote.

UPDATE: before collectTotals add

$shippingAddress = $quote->getShippingAddress();

Even if no shipping address is there, it will recalculate.

  • 1
    Hi, I think you can add this in comment section as its not answer. Just a suggestion
    – Jarnail S
    Commented Nov 18, 2020 at 18:44
  • 1
    What is answer and suggestion? If it is correct, then it will become answer or it still a suggestion? If I remove "Try to" then it will be answer? And after adding to quote you need to recalculate totals, it is not automatic. Commented Nov 18, 2020 at 19:05
  • I suggest you to add this as comment because your answer flagged by community. I hope you understand my point now.
    – Jarnail S
    Commented Nov 18, 2020 at 19:30
  • @Black see my updated answer Commented Nov 19, 2020 at 9:57
  • Thanks @KestutisK , after struggling for whole one day i found this code , my code was not recalculating total when there was no shipping address Commented Feb 8, 2022 at 12:54

Could be due to manu thing, your issue need more information.

Things that come up to my mind

  • Price is a complex object, so may be your $data['difference"] doesn't have the good type meaning what you set on the object isn't good.

  • You might have some after product save observer that might change the price

  • It might just be a frontend issue with the price not showing properly.

You might first want to check the price of the quote in your database to know if the issue is in the printing process or in the saving process.

I also notice that this product is virtual. You might have some special rules for non simple products like this one.

  • Hello, the $data['difference"] is definitely good, otherwise the price would not get set to 190€. There is no observer that changes the price.
    – Black
    Commented Nov 10, 2020 at 16:53

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.