1

Is there a way to somehow take the existing quote and create a new one identical to it but with a new ID?

This is what I currently have:

Updated following suggestions from @Vishwas:

use Magento\Framework\App\Action\Context;
use Magento\Framework\Controller\ResultFactory;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Quote\Model\QuoteRepository;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Framework\Translate\Inline\StateInterface;
use Magento\Checkout\Model\Cart;
use Magento\Catalog\Model\ProductFactory;

class ShareCart extends \Magento\Framework\App\Action\Action
{
    protected $resultFactory;
    protected $storeManager;
    protected $quoteFactory;
    protected $transportBuilder;
    protected $stateInterface;
    protected $cart;
    protected $productFactory;

    public function __construct(
        Context $context,
        ResultFactory $resultFactory,
        StoreManagerInterface $storeManager,
        QuoteRepository $quoteFactory,
        TransportBuilder $transportBuilder,
        StateInterface $stateInterface,
        Cart $cart,
        ProductFactory $productFactory
    ) {
        $this->resultFactory = $resultFactory;
        $this->storeManager = $storeManager;
        $this->quoteFactory = $quoteFactory;
        $this->transportBuilder = $transportBuilder;
        $this->stateInterface = $stateInterface;
        $this->cart = $cart;
        $this->productFactory = $productFactory;

        parent::__construct($context);
    }
    public function execute()
    {
        try
        {
            $post = (array) $this->getRequest()->getPost();

            $id = $this->cart->getQuote()->getId();

            if ($id > 0) {
                $quote = $this->quoteFactory->get($id);
                $items = $quote->getAllVisibleItems();

                foreach ($items as $item) {
                    $productId = $item->getProductId();
                    $_product = $this->productFactory->create()->load($productId);

                    $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());

                    $info = $options['info_buyRequest'];
                    $request1 = new \Magento\Framework\DataObject();
                    $request1->setData($info);

                    $this->cart->addProduct($_product, $request1);
                }
                $this->cart->save();

                // Retrieve form data
                $email   = $post['basketshare-email'];
                $message = $post['basketshare-message'];

                $from_name = "";
        }
    } catch (\Exception $e) {
        $this->messageManager->addError(__($e->getMessage()));
    }
}

}

UPDATE:

    $this->cart->addProduct($product, $request);

}

// save the updated cart items then re-attach the cart to the original quote
$this->cart->save();
$this->cart->setQuote($quote);
$this->checkoutSession->setQuoteId($quote->getId());
$this->cart->save();

I think there is something I'm not doing right here but I'm struggling to find any help on Google at the moment :)

  • what have you tried so far? show us some code – Philipp Sander Mar 19 at 10:48
0

All you need to do is to paste below code in controller action that you are executing for copying quote and copy one quote with all product items into another.

<?php
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
use Magento\Quote\Model\QuoteFactory;
use Magento\Checkout\Model\Cart;
use Magento\Catalog\Model\ProductFactory;
class Copyquote extends \Magento\Framework\App\Action\Action
{
    protected $_resultPageFactory;
    protected $quoteFactory;
    protected $formKey;
    protected $cart;
    protected $product;
    public function __construct(
        Context $context,
        PageFactory $resultPageFactory,
        QuoteFactory $quoteFactory,
        Cart $cart,
        ProductFactory $product
    ) {
        $this->_resultPageFactory = $resultPageFactory;
        $this->quoteFactory = $quoteFactory;
        $this->cart = $cart;
        $this->product = $product;
        parent::__construct($context);
    }
    public function execute()
    {
        try
        {
            $id = 'YOUR QUOTE ID';
            if ($id > 0) {
                $quote = $this->quoteFactory->create()->load($id);
                $items = $quote->getAllVisibleItems();

                foreach ($items as $item) {
                    $productId = $item->getProductId();
                    $_product = $this->product->create()->load($productId);

                    $options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());

                    $info = $options['info_buyRequest'];
                    $request1 = new \Magento\Framework\DataObject();
                    $request1->setData($info);

                    $this->cart->addProduct($_product, $request1);
                }
                $this->cart->save();
            }
        } catch (\Exception $e) {
            $this->messageManager->addError(__($e->getMessage()));
        }
    }
}

Now you can use or customize this code according to your need of copying one quote to another one in your Magento store

  • Thank you @Vishwas, I'll give that a try :) – SNAFU Mar 19 at 11:56
  • Building on top of this, do you know which would be the best way to get it to save the quote with a new ID please, so that it's a new quote but has it's own ID? – SNAFU Mar 19 at 12:16
  • 1
    Ok, I think I nearly have this working now. Thanks again for your code suggestion @vishwas I was half asleep yesterday and must have been looking at the code different. Your suggestion works well. I have added a little to it in order to meet the requirements for my task. Upto now, I have managed to get it creating a new quote based upon the original one but then if I update the cart on the original quote, it seems to update the new quote instead :( – SNAFU Mar 20 at 12:57
  • 1
    I will take a look at this now. Thank you again for your help :) – SNAFU Mar 20 at 13:13
  • 1
    Aha, of course! This is because multiple tabs will be using the session on the same computer isn't it? Everything works as expected when I open the different quotes in incognito tabs. Thanks again for your help with this. Time for me to click that grey tick button above :D – SNAFU Mar 20 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.