Jonathan Parisi
Jonathan Parisi

Reputation: 23

How to copy an existing order (core, php) with Prestashop 1.6.1

I'm making a script that should make a copy of an existing order. I can create the overall order, with this code:

$order = new Order($_GET["id_order"]);
$order->add();

But there's no products in the order - I tried with this:

$order_detail = new OrderDetail($_GET["id_order"]);
$order_detail->add();

What am I doing wrong, how can I copy an existing order?

Upvotes: 0

Views: 310

Answers (1)

WebXY
WebXY

Reputation: 139

You can duplicate an order using the duplicateObject() method from the ObjectModel class.

Here is a function that should do the trick :

function duplicateOrder($id_order)
{
    $order = new Order($id_order);
    $duplicatedOrder = $order->duplicateObject();

    $orderDetailList = $order->getOrderDetailList();
    foreach ($orderDetailList as $detail) {
        $orderDetail = new orderDetail($detail['id_order_detail']);
        $duplicatedOrderDetail = $orderDetail->duplicateObject();
        $duplicatedOrderDetail->id_order = $duplicatedOrder->id;
        $duplicatedOrderDetail->save();
    }

    $orderHistoryList = $order->getHistory(Configuration::get('PS_LANG_DEFAULT'));
    foreach ($orderHistoryList as $history) {
        $orderHistory = new OrderHistory($history['id_order']);
        $duplicatedOrderHistory = $orderHistory->duplicateObject();
        $duplicatedOrderHistory->id_order = $duplicatedOrder->id;
        $duplicatedOrderHistory->save();
    }
}

Upvotes: 4

Related Questions