skip to Main Content

In my WooCommerce plugin I’m using woocommerce_checkout_update_order_meta hook to handle custom checkout fields I added.

First I add action:
add_action('woocommerce_checkout_update_order_meta', __NAMESPACE__ . 'my_update_order_meta');

here below the function:

`function my_update_order_meta()
{
 $order = new WC_Order();
 $billing_fields = ['billing_fiscalcode', 'billing_vatcode'];

 foreach ($billing_fields as $field) {
   if (!empty(sanitize_text_field($_POST[$field]))) {
      $order->update_meta_data($field, sanitize_text_field($_POST[$field]));
   }
 }
}`

When I try to place an order in my shop I get a checkout error. Diving a little in the code I got that I’m not placing the order because its id is 0. Any idea about a solution?

I tried to use wc_create_order() as suggested here, I’m still failing

2

Answers


  1. Chosen as BEST ANSWER

    At last I found a solution. I used woocommerce_checkout_create_order as suggested by LoicTheAztec (thanks for help!), then I edited the function like this:

    function add_custom_order_metadata( $order, $data ) {
        /** my new code starts here below **/
        
        $args = array(
               'orderby' => 'date',
               'order' => 'DESC',
               'return' => 'ids'
        );
       
        $order_ids = wc_get_orders($args);
        $new_order_id = empty($order_ids) ? 1 : max($order_ids) + 1;
        $order->set_id($new_order_id);  
      
        /** my new code ends here **/        
    
        foreach (['billing_fiscalcode', 'billing_vatcode'] as $field) {
            if ( isset($_POST[$field]) && !empty($_POST[$field]) ) {
                $order->add_meta_data($field, sanitize_text_field($_POST[$field]), true);
            }
        }
    }
    

  2. The main mistake is that you forgot the hook argument $order_id in the function and then you should replace:

    $order = new WC_Order();
    

    with:

    $order = wc_get_order( $order_id );
    

    to make it work, at the end, before the last closing bracket, you need to add:

    $order->save():
    

    But, since WooCommerce 3, use instead woocommerce_checkout_create_order hook as follows:

    add_action('woocommerce_checkout_create_order', 'add_custom_order_metadata', 10, 2);
    function add_custom_order_metadata( $order, $data ) {
        foreach (['billing_fiscalcode', 'billing_vatcode'] as $field) {
            if ( isset($_POST[$field]) && !empty($_POST[$field]) ) {
                $order->add_meta_data($field, sanitize_text_field($_POST[$field]), true);
            }
        }
    }
    

    Code goes in functions.php file of your child theme (or in a plugin). It should work.

    For a plugin, you should need to replace:

    add_action('woocommerce_checkout_create_order', 'add_custom_order_metadata', 10, 2);
    

    with:

    add_action('woocommerce_checkout_create_order', [$this, 'add_custom_order_metadata'], 10, 2);
    

    Notes:

    • When using woocommerce_checkout_create_order hook, the function doesn’t need $order->save(); as it is included just after the hook.
    • It’s better to use add_meta_data() method than update_meta_data() as you are adding metadata (not updating it).
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search