Site icon MageComp Blog

How to Copy One Quote to Another Quote Programmatically in Magento 2

How to copy one quote to another programmatically In Magento 2

How to copy one quote to another programmatically In Magento 2

Working with Magento Projects & development, we always get some challenging work or task which we really love to solve. Store owners need to work hard and face real-time issue for delivering better service & use experience to their customers. One of our customers came up with the challenge to copy one quote to another programmatically in Magento 2. To save their time & efforts, we have developed a small piece of code that will help to copy one cart product items to another quote (cart items).

Generally, if you are trying to implement this code manually, it creates a problem while copying cart from the new users. But with our code, you don’t need to worry about session, Even after a user is visiting a website for a very first time, this code will definitely work with an empty cart.

All you need to do is to paste below code and it will automatically copy one quote with all product items into another.

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

Tadda! It’s copied! Now you can use or customize this code according to your need of copying one quote to another one in your Magento store. If you have any questions or suggestions, always feel free to drop a line in the comment section below!
Happy Coding!

Exit mobile version