woocommerce_checkout_update_order_meta操作失效

时间:2017-05-18 20:38:39

标签: wordpress woocommerce hook-woocommerce woothemes

您好今天我正在使用woo-commerce,我已根据用户要求成功创建了一些自定义结帐字段,但我无法将其保存在数据库中。

这是我如何在子主题functions.php

中创建自定义结帐字段...
add_filter( 'woocommerce_checkout_fields' , 'custom_override_checkout_fields' );
// Over Ridding, Removing, Creating New Fields.
function custom_override_checkout_fields( $fields ) {
     unset($fields['billing']['billing_company']);
     unset($fields['billing']['billing_address_2']);    
     unset($fields['order']['order_comments']);
     unset($fields['billing']['billing_address_1']);
     unset($fields['billing']['billing_city']);
     unset($fields['billing']['billing_postcode']);
     unset($fields['billing']['billing_email']);


     $fields['billing']['your_name'] = array(
    'type'      => 'text',
    'label'     => __('Full Name', 'woocommerce'),
    'placeholder'   => _x('Full Name', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     $fields['billing']['your_phone_number'] = array(
    'type'      => 'text',
    'label'     => __('Your Phone Number', 'woocommerce'),
    'placeholder'   => _x('Your Phone Number', 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     $fields['billing']['recipient_name'] = array(
    'type'      => 'text',
    'label'     => __("Recipient's Name", 'woocommerce'),
    'placeholder'   => _x("Recipient's Name", 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     $fields['billing']['recipient_company_name'] = array(
    'type'      => 'text',
    'label'     => __("Recipient's Company (if any)", 'woocommerce'),
    'placeholder'   => _x("Recipient's Company (if any)", 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     $fields['billing']['recipient_phone_number'] = array(
    'type'      => 'text',
    'label'     => __("Recipient's Phone Number", 'woocommerce'),
    'placeholder'   => _x("Recipient's Phone Number", 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     $fields['billing']['recipient_address'] = array(
    'type'      => 'text',
    'label'     => __("Recipient's Address", 'woocommerce'),
    'placeholder'   => _x("Recipient's Address", 'placeholder', 'woocommerce'),
    'required'  => false,
    'class'     => array('form-row-wide'),
    'clear'     => true
     );

     return $fields;
}

在我正在寻找领域的数据库中。它的wp_postmeta表。附件是我正在搜索订单ID的屏幕截图.. wp_postmeta db table

现在我添加了checkout_update_order_meta操作来更新订单元并存储我自定义创建的字段。但似乎它不起作用,因为当我在wp_postmeta表格中查看最新创建的订单ID时,我在那里找不到自定义字段。

add_action( 'woocommerce_checkout_update_order_meta', 'some_custom_checkout_field_update_order_meta' );

function some_custom_checkout_field_update_order_meta( $order_id ) {


if ( ! empty( $_POST['recipient_address'] ) ) {
add_post_meta( $order_id, 'recipient_address', sanitize_text_field( $_POST['recipient_address'] ) );
}
if (!empty($_POST['recipient_phone_number'])) {
        update_post_meta($order_id, 'recipient phone number', sanitize_text_field($_POST['recipient_phone_number']));
    }

}

这是我第一次处理woocommerce代码我搜索了很多,来到这里,因为我放弃了它。请帮我解开这个谜。

请纠正我的错误。此后,我将不得不在woocommerce>下的wordpress仪表板中显示这些自定义字段。订单>订购详情,如果有任何有用的链接,请提供。

提前致谢。

1 个答案:

答案 0 :(得分:3)

我刚刚更改了你的最后一个钩子函数,它可以工作(在WC版本2.6.x和3.0+上)。使用empty() php函数更好地使用变量(复古兼容)
最好使用update_post_meta()代替 add_post_meta() ,因为此功能可确保 meta_key 已存在,如果不存在, add_post_meta() 将被调用...

  

此处是与订单元数据相关的 wp_postmeta 表的屏幕截图:   wp_postmeta table

如果 meta_key 不是像这里的下划线那样,它会显示在自定义字段元数据框的后端订单编辑页面中:enter image description here

以下是此代码:

add_action( 'woocommerce_checkout_update_order_meta', 'saving_checkout_cf_data');
function saving_checkout_cf_data( $order_id ) {

    $recipient_address = $_POST['recipient_address'];
    if ( ! empty( $recipient_address ) )
        update_post_meta( $order_id, 'recipient_address', sanitize_text_field( $recipient_address ) );

    $recipient_phone_number = $_POST['recipient_phone_number'];
    if ( ! empty( $recipient_phone_number ) )
        update_post_meta($order_id, 'recipient_phone_number', sanitize_text_field( $recipient_phone_number ) );

}

代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。

  

如果您希望 meta_key _billing… 开头,就像经典结算结帐字段一样,您只需要在 update_post_meta() 功能。例如:

update_post_meta( $order_id, '_billing_recipient_address', sanitize_text_field( $recipient_address ) );
     

但在这种情况下,这不会出现在订单编辑页面的自定义字段元数据中。