0

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.

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

2 Answers 2

1

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.

0

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

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

declare(strict_types=1);

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) {
            $this->logger->info($e);
        } catch (\Magento\Framework\Exception\NoSuchEntityException $e) {
            $this->logger->info($e);
        } catch (\Magento\Framework\Exception\LocalizedException $e) {
            $this->logger->info($e);
        }
    }
}

I hope it helps you.

4
  • An empty cart is created but the product not added to the cart or quote. I added $productId = 1.
    – Mital Shah
    Commented 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. Commented 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
    Commented 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? Commented 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.