2

I've been searching around but can't get it to work.

Using the api v3 I'm able to get all orders and everything is ok, but now I need to be able to update a metadata, when I use insomnia I manage to update the metadata, but now I need to do it from a php script but it just won't work.

with insomnia using put.

{ "meta_data": [ { "key": "_mensajero", "value": "juan carlos" } ]}

and it works and update order meta, but with php no matter what I try I cannot get it to update

<?php
 if (isset($_POST['btn-update'])) {
 $oid = $_POST['cId']; /////the order id
 $data =  [
  'meta_data' => [
    '_mensajero' => '_mensajero'
    
    ]
   ];

   $woocommerce->put('orders/' . $oid,  $data);
  header('Location: #');
   }

?>  
Ruvee
  • 8,611
  • 4
  • 18
  • 44

1 Answers1

2

There are multiple ways to update the meta data. One common way would be to use the order object and one of its methods called update_meta_data. Like this:

if (isset($_POST['btn-update'])) 
{

    $oid = absint($_POST['cId']); // the order id

    if($oid)
    {
        $order = wc_get_order($oid);

        if($order)
        {

            $order->update_meta_data('_mensajero', '_mensajero');

            $order->save();

            // Do what ever you want here

        }else{

            die('NO ORDER WITH THE PROVIDED ID FOUND!');
            
           // OR send back a custom error message using 'wp_send_json_error' function
            
        }

    }else{

        die('NO ORDER ID RECIEVED!');

        // OR send back a custom error message using 'wp_send_json_error' function

    }
    
}

Another way of updating meta data using update_post_meta function:

if (isset($_POST['btn-update'])) 
{

    $oid = absint($_POST['cId']); // the order id

    if($oid)
    {
        $order = wc_get_order($oid);

        if ($order) {

            update_post_meta($oid, '_mensajero', '_mensajero');

            // Do what ever you want here

        } else {

            die('NO ORDER WITH THE PROVIDED ID FOUND!');

            // OR send back a custom error message using 'wp_send_json_error' function
        }

    }else{

        die('NO ORDER ID RECIEVED!');

        // OR send back a custom error message using 'wp_send_json_error' function
    }
    
}
Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • 1
    it worked, if i may ask where did u get documentation to understand how to accomplish this – tyrese humphreys Jan 26 '22 at 17:12
  • Sure you may! Well I've been a `woo` developer for a while so I know it from experience, also, as I said in the answer, updating order meta data has several common ways among `woo` developers. In addition to experience, the best way to figure out how to accomplish that is through **reading woo documentation specially the woo source code**. Also, you could have searched here on SO, like this: `woocommerce how to update order metadata`. There are several **examples**, like [this one](https://stackoverflow.com/a/66475757/15040627) and [this one](https://stackoverflow.com/a/48502896/15040627). – Ruvee Jan 26 '22 at 17:54