I want to create a new quote based on the old order. I can create order programmatically but not able to just add to cart product in a new quote. I don't want to place the order. Need is only add to cart product programmatically.

  • you mean create new quote based on last order order?? May 4, 2021 at 7:01
  • Yes, and I successfully did :)
    – Mital Shah
    May 4, 2021 at 7:14
  • can you please add your solution.. ??? So other people can easy to get a solution. :) May 4, 2021 at 7:48

2 Answers 2


You can follow magento reorder functionality. E.g: vendor/magento/module-sales/Controller/AbstractController/Reorder.php

The following line will create new quote:

$reorderOutput = $this->reorder->execute($order->getIncrementId(), $order->getStoreId());

You can open vendor/magento/module-sales/Model/Reorder/Reorder.php, where you can found more detail.


I developed a simple code and it works well, please take a look:

<!-- language: lang-php-->


namespace Vendor\Module\Model;

use Magento\Quote\Api\CartManagementInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Psr\Log\LoggerInterface;

class CustomerCartManagement
     * @var CartManagementInterface
    private CartManagementInterface $cart;

     * @var CartRepositoryInterface
    private CartRepositoryInterface $cartRepository;

     * @var CartRepositoryInterface|ProductRepositoryInterface
    private CartRepositoryInterface $productRepository;

     * @param CartManagementInterface $cart
     * @param CartRepositoryInterface $cartRepository
     * @param ProductRepositoryInterface $productRepository
     * @param LoggerInterface $logger
    public function __construct(
        CartManagementInterface $cart,
        CartRepositoryInterface $cartRepository,
        ProductRepositoryInterface $productRepository,
        LoggerInterface $logger
    ) {
        $this->cart = $cart;
        $this->cartRepository = $cartRepository;
        $this->productRepository = $productRepository;

     * @param $productId
    public function addProductToCart($productId)
        try {
            $cartId = $this->cart->createEmptyCart();
            $cart = $this->cartRepository->get($cartId);
            $product = $this->productRepository->getById($productId);
            $request = new \Magento\Framework\DataObject([
                'product' => $productId,
                'qty' => 1,
            $cart->addProduct($product, $request);
        } catch (\Magento\Framework\Exception\CouldNotSaveException $e) {
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
        } catch (\Magento\Framework\Exception\LocalizedException $e) {

I hope it helps you.

  • An empty cart is created but the product not added to the cart or quote. I added $productId = 1.
    – Mital Shah
    Apr 20, 2021 at 4:40
  • Did you check the logs? After executing any exception during the process should be written on the logs. Can you share some details about the product: qty, product type, stock details (MSI or default inventory), status, and anything you reckon is relevant. Apr 20, 2021 at 10:44
  • Yes checked logs. there is no exception. when i am going to just print product data like name or sku then all are blanks.
    – Mital Shah
    Apr 20, 2021 at 11:22
  • What do you mean by 'print product data'? Are you checking that data on product page admin panel? Or are you printing the product data of the object $product? Apr 20, 2021 at 12:12

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.