【问题标题】:WooCommerce how to update order meta data key value with woo rest api 3WooCommerce 如何使用 woo rest api 3 更新订单元数据键值
【发布时间】:2022-01-26 05:22:30
【问题描述】:

我一直在四处寻找,但无法正常工作。

使用 api v3 我能够获得所有订单并且一切正常,但现在我需要能够更新元数据,当我使用 insomnia 时我设法更新元数据,但现在我需要这样做来自php script,但它不起作用。

失眠使用put

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

它可以工作并更新订单元,但使用php,无论我尝试什么,我都无法更新它

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

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

?>  

【问题讨论】:

    标签: php wordpress woocommerce woocommerce-rest-api woocommerce-theming


    【解决方案1】:

    有多种方法可以更新元数据。一种常见的方法是使用order object 及其一种称为update_meta_data 的方法。像这样:

    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
    
        }
        
    }
    

    使用update_post_meta函数更新元数据的另一种方式:

    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
        }
        
    }
    

    【讨论】:

    • 它工作,如果我可以问你从哪里得到文件来了解如何完成这个
    • 当然可以!好吧,我作为woo 开发人员已经有一段时间了,所以我从经验中知道,而且,正如我在回答中所说,更新订单元数据在woo 开发人员中有几种常见的方法。除了经验之外,弄清楚如何做到这一点的最佳方法是阅读 woo 文档,特别是 woo 源代码。此外,您可以在 SO 上搜索,如下所示:woocommerce how to update order metadata。有几个示例,例如this onethis one
    猜你喜欢
    • 1970-01-01
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多