I am using this code to clone or duplicate an order I have previously created.
It ask me for the order id I want to use and copy it duplicating order and products with a new order id. It's very ursefull, I have this code in a block and when I need to clone or duplicate an order to create a similar one I use it.
It has only an error, and I dont find it in the code.
It clones an given order well, including products in order.
The cloned order has an error in subtotal area, where subtotal, taxes, and total is showed. It shows a line with just :0.00 in this line.
I don't know how to delete this from cloned orders, please what in the code is creating this issue?
Attachment is the capture with extrange line in subtotal area.

This is the code:

<form method="post">
<input type="text" name="order_id" value="0">
<input type="submit">
</form>
<?php
    if(isset($_POST['order_id'])){
        $order_id = $_POST['order_id'];
        $order = uc_order_load($order_id);
        if($order)
        {
            //create new order in db
            $new_order = uc_order_new($order->uid);
            
           //hack in the products for the new order
           $i=0;
           foreach($order->products as $product) {
                $insert = new StdClass();
                uc_order_product_save($new_order->order_id, $insert);
                $order->products[$i]->order_product_id = $insert->order_product_id;
                $order->products[$i]->order_id = $new_order->order_id;
                uc_order_product_save($new_order->order_id, $order->products[$i]);
                $i++;
            }

            //hack in the line items for the new order
            $i=0;
            foreach($order->line_items as $line_item) {
                if($i!=0){
                    uc_order_line_item_add($new_order->order_id,$order->line_items[$i]->type,$order->line_items[$i]->title,$order->line_items[$i]->amount,$order->line_items[$i]->weight);
                    $order->line_items[$i]->line_item_id = mysql_insert_id();
                }
                $i++;
            }
            
            $order->order_id = $new_order->order_id;
            $order->order_status = "in_checkout";
            
            //save newly created order
            uc_order_save($order);
            
            //Now charge the card
            //uc_cim_charge($new_order->order_id, $order->order_total);
            
            $order = uc_order_load($new_order->order_id);
            //update the status
            $order->order_status = "pending";
            uc_order_save($order);
            
            echo

"<p>Order <strong>".$order->order_id."</strong> created as a duplicate of <strong>$order_id</strong><br/></p>";
            echo "<pre style='font-size:10px;'>";
            var_dump($order);
            echo "</pre>";
            
        } else {
            
            echo "could not load previous order details";
        
        }
    }
?>

Waiting for a hand. Best regards,
Bunset.
www.e-duca.eu

Comments

longwave’s picture

Status: Active » Fixed

http://drupal.org/project/uc_order_clone does this already, you could perhaps look at the code of that. In general we don't support debugging custom code in this issue queue.

bunset’s picture

Sorry, I see http://drupal.org/project/uc_order_clone and it put any previous order in cart but doesn't save the order.
Isn't it?

Kind regards,
Bunset

bunset’s picture

I am comparing two orders cloned, the original and the cloned, all data in orders table is exactly equal except order_id.
Which table is used to save the info showed in subtotal-total area of order please? Maybe there I find differences to found the wrong data saved in cloned order.
Thank you,
Bunset

bunset’s picture

Status: Fixed » Active

I move status from fixed to active to get response in last comment

longwave’s picture

uc_order_line_items is the table, it's probably something to do with that "hack in the line items for the new order" section of code I guess.

bunset’s picture

StatusFileSize
new37.67 KB

Thank you very much longwave for your suggestion.
Exactly, in line items table I have two lines wrong, it is a great progress for me.
I don't get to know which lines in my code are inserting this wrong lines in uc_order_line_items table and what is wrong there.
I attach capture of wrong line inserted in uc_order_line_items table for an example cloned order.

Best regards,
Bunset

bunset’s picture

Issue tags: +copiar o clonar una orden en drupal 6

Fixed and solved!,
I just have modified the syntax of order_line_items fields values to function uc_order_line_item_add() as I show:
Changed:
$order->line_items[$i]->type to $order->line_items[$i]['type']
and
$order->line_items[$i]->title to $order->line_items[$i]['title']
and
$order->line_items[$i]->amount to $order->line_items[$i]['amount']
and
to $order->line_items[$i]['weight'] to $order->line_items[$i]['weight']

So, this is the final code for Drupal 6 to clone an order. I hope it will be usefull for other people too.
I have this code in a custom block I show in orders admin pages, block is visible in my administration theme:

Code:

<form method="post">
<input type="text" name="order_id" value="enter an order to copy">
<input type="submit">
</form>
<?php
    if(isset($_POST['order_id'])){
        $order_id = $_POST['order_id'];
        $order = uc_order_load($order_id);
        if($order)
        {
            //create new order in db
            $new_order = uc_order_new($order->uid);
            
           //hack in the products for the new order
           $i=0;
           foreach($order->products as $product) {
                $insert = new StdClass();
                uc_order_product_save($new_order->order_id, $insert);
                $order->products[$i]->order_product_id = $insert->order_product_id;
                $order->products[$i]->order_id = $new_order->order_id;
                uc_order_product_save($new_order->order_id, $order->products[$i]);
                $i++;
            }

            //hack in the line items for the new order
            $i=0;
            foreach($order->line_items as $line_item) {
                if($i!=0){
                    uc_order_line_item_add($new_order->order_id,$order->line_items[$i]['type'],$order->line_items[$i]['title'],$order->line_items[$i]['amount'],$order->line_items[$i]['weight']);
                    $order->line_items[$i]->line_item_id = mysql_insert_id();
                }
                $i++;
            }   
            
            $order->order_id = $new_order->order_id;
            $order->order_status = "in_checkout";
            
            //save newly created order
            uc_order_save($order);
            
            //Now charge the card
            //uc_cim_charge($new_order->order_id, $order->order_total);
            
            $order = uc_order_load($new_order->order_id);
            //update the status
            $order->order_status = "pending";
            uc_order_save($order);
            
            echo

"<p>Order <strong>".$order->order_id."</strong> created as a duplicate of <strong>$order_id</strong><br/></p>";
            echo "<pre style='font-size:10px;'>";
            var_dump($order);
            echo "</pre>";
            
        } else {
            
            echo "could not load previous order details";
        
        }
    }
?>

Kind regards,
Bunset
Formación Drupal y Diseño Web en Madrid

bunset’s picture

Status: Active » Fixed

Fixed in last comment

Automatically closed -- issue fixed for 2 weeks with no activity.