doujiong2533 2016-08-18 13:31
浏览 193
已采纳

woocommerce webhook有效载荷中的自定义值

I have webhook with topic Order Updated. Webhook delivers payload (Order information) at someotherdomain.com. I want to add extra field from checkout form to be delivered at someotherdomain.com along with order information.

I have created custom checkout field by:

add_action( 'woocommerce_after_order_notes', 'fs_custom_checkout_field' );


function fs_custom_checkout_field( $checkout ) {


echo '<div id="my_custom_checkout_field"><h2>' . __('Extra Information') . '</h2>';

woocommerce_form_field( 'fs_psid_field', array(
    'type'          => 'text',
    'class'         => array('my-field-class form-row-wide'),
    'label'         => __('Fill in this field'),
    'placeholder'   => __('Enter something'),
    ), $checkout->get_value( 'fs_psid_field' ));

echo '</div>';

}

And saved meta as:

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['fs_psid_field'] ) ) {
        update_post_meta( $order_id, 'fs_psid_field', sanitize_text_field( $_POST['fs_psid_field'] ) );
    }
}

But field fs_psid_field is not posted via webhook.

Q: How can I post value of this field via Woocommerce webhook?

  • 写回答

1条回答 默认 最新

  • douwen1915 2016-08-19 12:22
    关注

    Figured out. First add custom field on check out form.

    add_action( 'woocommerce_after_order_notes', 'fs_custom_checkout_field' );
    
    function fs_custom_checkout_field( $checkout ) {
    
        echo '<div id="my_custom_checkout_field"><h2>' . __('Extra Information') . '</h2>';
    
        woocommerce_form_field( 'fs_psid_field', array(
            'type'          => 'text',
            'class'         => array('my-field-class form-row-wide'),
            'label'         => __('Fill in this field'),
            'placeholder'   => __('Enter something'),
            ), $checkout->get_value( 'fs_psid_field' ));
    
        echo '</div>';
    
    }
    

    Then save custom field's value as meta

    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['fs_psid_field'] ) ) {
            update_post_meta( $order_id, 'fs_psid_field', sanitize_text_field( $_POST['fs_psid_field'] ) );
        }
    }
    

    Then add custom field's value in api response

    function my_custom_wc_api_order_response( $order_data, $order ) {
    
        $psidMeta      = get_post_meta($order->id , 'fs_psid_field' , true );
        $order_data['psid'] = $psidMeta;
        return $order_data;
    }
    
    add_filter( 'woocommerce_api_order_response', 'my_custom_wc_api_order_response', 10, 3 );
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 matlab计算中误差
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊