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!
In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…
In the world of business, understanding financial metrics is crucial for making informed decisions and…
Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…
In this era, businesses are always on the lookout for ways to engage with their…
Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…
E-commerce has transformed the way consumers shop for products and services and interact with businesses.…
View Comments
Can we able to create a checkout session with the quoteID