updating woocommerce order in checkout page instead to create a new one

I am creating a WordPress and WooCommerce plugin which does the following:

  1. An anonymous user customizes a product on the page
  2. In a php script in the process, it creates an order in the database
  3. The user goes to the checkout page where the customer data, delivery etc. is requested.

By clicking on “Buy Now” WooCommerce creates a new order in the system and what I want is to update the order created earlier during the personalization process, adding customer details, payment, delivery etc. to the order.

Read More

is it possible?

Thanks!

Related posts

Leave a Reply

3 comments

  1. You can use the function wc_update_order() to fetch an existing order object.

    $order_id = $_GET[ 'order_id' ]
    $order = wc_update_order( array ( 'order_id' => $order_id ) );
    
    // now you got the woocommerce order object and you can execute many functions
    //
    $address = array(
            'first_name' => 'Fresher',
            'last_name'  => 'StAcK OvErFloW',
            'company'    => 'stackoverflow',
            'email'      => 'test@test.com',
            'phone'      => '777-777-777-777',
            'address_1'  => '31 Main Street',
            'address_2'  => '', 
            'city'       => 'Chennai',
            'state'      => 'TN',
            'postcode'   => '12345',
            'country'    => 'IN'
        );
    
    $order->add_product( get_product( '12' ), 1 ); //(get_product with id and next is for quantity)
    $order->set_address( $address, 'billing' );
    $order->set_address( $address, 'shipping' );
    $order->calculate_totals();
    

    For a complete list of all the functions you can call on the order object, execute the following code after you fetch your order

    var_dump( get_class_methods( $order ) );
    

    This will list all the functions that are available to the Woocommerce Order object.

    Hope this answers your question

  2. Finally the solution is to update the values once the order has been made in the “thankyou.php” WooCommerce page.

    Are updated as follows:

    $item = $order->get_items('line_item');
    $data = (array) WC()->session->get('_lm_product_data');
    
    wc_update_order_item_meta(key($item), __( 'Name', 'hpwallart' ), $data['_name'] );
    wc_update_order_item_meta(key($item), __( 'Width', 'hpwallart' ), $data['_width'] );
    wc_update_order_item_meta(key($item), __( 'Height', 'hpwallart' ), $data['_height'] );
    

    Previously, in a step of the process, I have kept in the session variable “_lm_product_data” information that interests me for the last step.

  3. Hi Jorge I know it comes a little late, but i think this will suit your needs a little better than your solution. Actually this is the recomended way of doing this according to woocommerce. And it will work even if they dont reach the “thankyou” template for some reason. (for example, not finishing the payment proccess)

    In my case i had to do this in order to add aditional information that I needed from my customers, for that specific purchase, so I decided to attach it to the order meta.

    Basically you have to attach your custom function to a hook that gets executed on the creation of the order. This is an exaple using the hook that the plugin already provides so you can do your own things during the creation of the order:

    /**
     * Update the order meta with field value
     */
    add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
    
    function my_custom_checkout_field_update_order_meta( $order_id ) {
        if ( ! empty( $_POST['my_field_name'] ) ) {
            update_post_meta( $order_id, 'My Field', sanitize_text_field( $_POST['my_field_name'] ) );
        }
    }
    

    Source: http://docs.woothemes.com/document/tutorial-customising-checkout-fields-using-actions-and-filters/

    In this example they are adding an extra custom field added in the checkout page (not in the product purchase as you want), but this part of the code could be used to your situation by passing the information of your custom product(s) to the checkout form submmition (one option could be adding hidden inputs with style=”display:none” through javascript or on the php template that th eplugin offers).