Ait*_*han 7 wordpress woocommerce woothemes hook-woocommerce
嗨,今天我正在与 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;
}
Run Code Online (Sandbox Code Playgroud)
在我正在寻找字段的数据库中。它的wp_postmeta桌子。附件是我正在使用订单 ID 搜索的屏幕截图。

现在我添加了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']));
}
}
Run Code Online (Sandbox Code Playgroud)
这是我第一次处理 woocommerce 代码,我搜索了很多,然后在我放弃它时来到这里。请帮我解开这个谜。
请纠正我我做错了什么。同样在这一步之后,我将不得不在 woocommerce > 订单 > 订单详细信息下的 wordpress 仪表板中显示这些自定义字段,所以如果有任何有用的链接,请提供。
提前致谢。
我只是稍微改变了你最后一个挂钩的函数,它可以工作(在 WC 版本 2.6.x 和 3.0+ 上)。empty()php 函数最好使用变量(以兼容复古)。
也最好使用update_post_meta()而不是add_post_meta()因为这个函数将确保meta_key已经存在,如果不存在,add_post_meta()将被调用......
如果meta_key不像这里那样以下划线开头,它会出现在自定义字段元框中的后端订单编辑页面中:
这是这段代码:
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 ) );
}
Run Code Online (Sandbox Code Playgroud)
代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中。
如果您想要像经典的帐单结帐字段一样
meta_key开始_billing…,您只需要在update_post_meta()函数中更改它。例如:Run Code Online (Sandbox Code Playgroud)update_post_meta( $order_id, '_billing_recipient_address', sanitize_text_field( $recipient_address ) );但在这种情况下,这不会出现在订单编辑页面的自定义字段元框中。
| 归档时间: |
|
| 查看次数: |
13905 次 |
| 最近记录: |