gpt4 book ai didi

wordpress - woocommerce_checkout_update_order_meta 操作不起作用

转载 作者:行者123 更新时间:2023-12-03 14:18:49 26 4
gpt4 key购买 nike

嗨,今天我正在与 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 搜索的屏幕截图。
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 仪表板中显示这些自定义字段,所以如果有任何有用的链接,请提供。

提前致谢。

最佳答案

我只是稍微改变了你最后一个 Hook 的函数,它可以工作(在 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: 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 文件或任何插件文件中。

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.

关于wordpress - woocommerce_checkout_update_order_meta 操作不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44057357/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com