Categories: How ToMagento 2

How to Copy One Quote to Another Quote 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!

Click to rate this post!
[Total: 9 Average: 2.9]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

2 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

4 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

4 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

5 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

6 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago