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.
class Copyquote extends \Magento\Framework\App\Action\Action
public function __construct(Context $context,
$this->_resultPageFactory = $resultPageFactory;
$this->quoteFactory = $quoteFactory;
$this->cart = $cart;
$this->product = $product;
public function execute()
$id = ‘YOUR QUOTE ID’
if($id > 0)
$quote = $this->quoteFactory->create()->load($id);
$items = $quote->getAllVisibleItems();
foreach ($items as $item)
$_product = $this->product->create()->load($productId);
$options = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct());
$info = $options['info_buyRequest'];
$request1 = new \Magento\Framework\DataObject();
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!