Magento 2 admin recreate cart

Magento 2 admin recreate cart

To restore quote or magento 2 admin recreate cart pro-grammatically we need to use admin session model, frontend magento checkout model will not be useful here. The sample code was given below,

Model

namespace Yournamespace\Yourmodulename\Model\Adminhtml\Order;

class RecreateOrder
{
    public function __construct(
        \Magento\Backend\Model\Session\Quote $adminsessionquote,
        \Magento\Sales\Model\AdminOrder\Create $adminordercreate
    ) {
        $this->adminsessionquote = $adminsessionquote;
        $this->adminordercreate = $adminordercreate;
    }

    public function reactivateAdminQuoteForOrder($mageOrder)
    {

        $session = $this->adminsessionquote;
        $mageOrder->setReordered(true);
        $session->setStoreId($mageOrder->getStoreId());
        $session->setCustomerId($mageOrder->getCustomerId());
        $session->setUseOldShippingMethod(true);
        $this->adminordercreate->initFromOrder($mageOrder);
    }
    

}

In custom model file use magento admin session and set the following above attributes, which will recreate the existing quote again in admin order.