【问题标题】:Update Order custom Field value with WooCommerce REST API使用 WooCommerce REST API 更新订单自定义字段值
【发布时间】:2022-01-13 11:57:06
【问题描述】:

我正在尝试更新订单自定义字段值lworder。我使用了以下代码,但没有找到任何更新。谁能帮我解决这个问题?

$api_response = wp_remote_post( 'https://your-website/wp-json/wc/v3/orders/{ORDER ID}', array(
    //'method'    => 'PUT',
    'headers' => array(
        'Authorization' => 'Basic ' . base64_encode( 'KEY:SECRET' )
    ),
    'meta_key' => array(
            'lworder' => 'ordered', 
    )
) );

$body = json_decode( $api_response['body'] );
//print_r( $body );

if( wp_remote_retrieve_response_message( $api_response ) === 'OK' ) {
    echo 'The Order field  ' . $body->name . ' has been updated';
}

【问题讨论】:

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


    【解决方案1】:

    使用 WooCommerce 6.1.1 测试正常

    $api_response = wp_remote_post('https://mujuonly.github.io/index.php/wp-json/wc/v3/orders/1631817799', array(
            'method' => 'PUT',
            'headers' => array(
                'Authorization' => 'Basic ' . base64_encode('ck_b0f7480d348807e3c065053e7810fb83ce3b6b41:cs_0c44e9ad9173fd3698010e86f1140914dcb8fc8a')
            ),
            'body' => array('billing' => array(
                    'city' => 'Disssssmmmmss',
                ),
                'meta_data' => array(0 => array(
                        'key' => 'lworder',
                        'value' => 'orderd'
                    )
                )
            )
        ));
    
        $body = json_decode($api_response['body']);
    
        if (wp_remote_retrieve_response_message($api_response) === 'OK') {
            echo 'The Order field  ' . $body->name . ' has been updated';
        }
    

    【讨论】:

    • 非常感谢@mujuonly
    • 我希望那些不是真正的钥匙......
    • @mujuonly 我们可以将https://mujuonly.github.io/index.php/wp-json/wc/v3/orders/1631817799 作为variable wp_remote_post 中传递,或者我可以将order id 添加为variable。我试过了,但没用,你能建议一下吗?
    • 数字1631817799应替换为您的实际订单ID
    猜你喜欢
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 2019-11-13
    • 2022-12-15
    • 2022-01-10
    • 2018-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多