0

I need to create a button ("Clone Order") in the admin > orders section to clone a customer's order to other customers.

enter image description here

By clicking this button, a new page will open where I need to put the customer emails to where these orders will be cloned. Any suggestions on how I can do this?

Thanks in advance.

1 Answer 1

0

Create a button and call below code :

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Model\Customer');
        $fromCustomer = $customer->loadByEmail('[email protected]');
        $toCustomer = $customer->loadByEmail('[email protected]');
        
        $orderEmailCollection = $objectManager->create('\Magento\Sales\Model\Order')->getCollection()->addFieldToFilter('customer_email',$fromCustomer->getEmail());
        $orderIdCollection = $objectManager->create('\Magento\Sales\Model\Order')->getCollection()->addFieldToFilter('customer_id',$fromCustomer->getId());
        
        foreach($orderEmailCollection as $order){
            $order->setCustomerId($toCustomer->getId());
            $order->setCustomerEmail($toCustomer->getEmail());
            $order->save();
        }
        
        foreach($orderIdCollection as $order){
            $order->setCustomerId($toCustomer->getId());
            $order->setCustomerEmail($toCustomer->getEmail());
            $order->save();
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.