woocommerce_checkout_update_order_meta 操作无效

woocommerce_checkout_update_order_meta action is not working

您好,今天我在与 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 table。附件是我正在使用订单 ID 搜索的屏幕截图。

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

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 仪表板中显示这些自定义字段,因此如果有任何帮助 link 请提供。

提前致谢。

我刚刚稍微更改了您的最后一个钩子函数并且它可以工作(在 WC 版本 2.6.x 和 3.0+ 上)。 empty() php 函数最好使用变量 (兼容复古)
也最好使用 update_post_meta() 而不是 add_post_meta() 因为这个函数将确保 meta_key已经存在,如果不存在,将调用 add_post_meta()...

Here a screenshot of the wp_postmeta table related to the order meta data:

如果 meta_key 不像这里那样以下划线开头,它会出现在自定义字段 metabox 的后端订单编辑页面中:

这是这段代码:

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 文件或任何插件文件。

If you want to have a meta_key starting by _billing… like classic billing checkout fields, you just need to change that in update_post_meta() function. For example:

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

But in this case, this will not appear in the custom Fields metabox in the order edit page.

我需要上传多个字段,我使用了这样的数组:

add_action('woocommerce_checkout_update_order_meta', 'tu_funcion'); 
function tu_funcion($order_id) 
{ 
$arrEnv = array('billing_cif', 'despliegue_nombre', 'despliegue_apellido', 'despliegue_correo');
foreach ($arrEnv as $valor) :
if (!empty($_POST[$valor]))
update_post_meta($order_id, $valor, sanitize_text_field($_POST[$valor]));
endforeach; }

之前我介绍他们使用以下方式结帐:

add_filter ('woocommerce_checkout_fields', 'custom_fields_finish_purchase');
function custom_fields_finish_purchase($fields){
 $fields['billing']['billing_cif'] = array (
     'type' => 'text',
     'label' => 'CIF',
     'placeholder' => 'Write here the CIF of the company',
     'class' => array ('form-row-wide'),
     'required' => true,
 );
// more fields here...
return $fields;
}

有关订单详情,您可以用这个 fook 写:

add_action('woocommerce_admin_order_data_after_billing_address', 'your_function', 10, 1);
function your_function{
echo '<p><strong>CIF:</strong> ' . get_post_meta($order->get_id(), 'billing_cif', true) . '</p>';}

对于写电子邮件,您可以使用这个 add_action 挂钩:

'woocommerce_email_after_order_table'