2

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();
$product->setName($data["name"]);
$product->setSku($data["sku"]);
$product->setPrice($data["difference"]);
$product->setTypeId("virtual");
$product->setWeight(0);
$product->setTaxClassId($this->taxClassId);
$product->setVisibility(1);

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());
}

...

$this->quote->addProduct($product);
$this->quote->save();

price subtotal is 0

2 Answers 2

3
+50

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();
$shippingAddress->setCollectShippingRates(true)
       ->collectShippingRates();

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

5
  • 1
    Hi, I think you can add this in comment section as its not answer. Just a suggestion
    – Jarnail S
    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. 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
    Nov 18, 2020 at 19:30
  • @Black see my updated answer 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 Feb 8, 2022 at 12:54
0

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.

1
  • 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
    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.