Php 保存签出自定义字段将值合并到Web Commerce中的现有字段
我已经为WooCommerce创建了以下自定义签出字段,基于可以找到的所有示例Php 保存签出自定义字段将值合并到Web Commerce中的现有字段,php,wordpress,woocommerce,checkout,custom-fields,Php,Wordpress,Woocommerce,Checkout,Custom Fields,我已经为WooCommerce创建了以下自定义签出字段,基于可以找到的所有示例 账单号账单号 船运公司编号船运公司编号 输出显示地址字段(街道)和门牌号字段。很好用。将数据很好地存储在自定义门牌号字段中,地址的工作原理与正常情况相同——当然 我想将以下字段合并到一个字段中: billing\u address\u 1和billing\u house no位于billing\u address\u 1现有字段中 shipping\u address\u 1和shipping\u house
- 账单号<代码>账单号<代码>
- 船运公司编号
船运公司编号
和billing\u address\u 1
位于billing\u house no
现有字段中李>billing\u address\u 1
和shipping\u address\u 1
位于shipping\u house no
现有字段中李>shipping\u address\u 1
你应该不要取消设置
“账单公司”
和“发货公司”
$order->id
,它都应该被$order->get_id()
替换woocommerce\u checkout\u update\u order\u meta
操作挂钩中的自定义函数,您将能够合并这些值
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order_id ) {
$separator = '. '; // Separator for merged fields
// Billing fields
$billing_address_1 = sanitize_text_field( $_POST['billing_address_1'] );
$billing_houseno = sanitize_text_field( $_POST['billing_houseno'] );
if( ! ( empty($billing_address_1) && empty($billing_houseno) ) )
update_post_meta( $order_id, '_billing_address_1', $billing_address_1 . $separator . $billing_houseno );
$shipping_address_1 = sanitize_text_field( $_POST['shipping_address_1'] );
$shipping_houseno = sanitize_text_field( $_POST['shipping_houseno'] );
if( ! ( empty($shipping_address_1) && empty($shipping_houseno) ) )
update_post_meta( $order_id, '_shipping_address_1', $shipping_address_1 . $separator . $shipping_houseno );
}
代码进入活动子主题(或活动主题)的function.php文件
这一次,这是经过测试并有效的。试试这个
add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta',10,2);
function customise_checkout_field_update_order_meta($order_id, $posted_data)
{
//write your code here to update custom data.
}
感谢@LoicTheAztec的评论。很好的例子,但对我来说仍然不起作用。让我向您展示如何构建自定义字段,也许我做错了什么。在此链接到我的代码共享:提前感谢@朱利安:我已经测试了一切,更新了我的答案。它起作用了。但是链接代码中有一些错误,请参阅我的更新说明。。。谢谢。啊,对不起,我不小心把它拿走了!我的错:)@Juliën谢谢julien…我只是非常惊讶。
add_action( 'woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta', 10, 2 );
function custom_checkout_field_update_order_meta( $order_id ) {
$separator = '. '; // Separator for merged fields
// Billing fields
$billing_address_1 = sanitize_text_field( $_POST['billing_address_1'] );
$billing_houseno = sanitize_text_field( $_POST['billing_houseno'] );
if( ! ( empty($billing_address_1) && empty($billing_houseno) ) )
update_post_meta( $order_id, '_billing_address_1', $billing_address_1 . $separator . $billing_houseno );
$shipping_address_1 = sanitize_text_field( $_POST['shipping_address_1'] );
$shipping_houseno = sanitize_text_field( $_POST['shipping_houseno'] );
if( ! ( empty($shipping_address_1) && empty($shipping_houseno) ) )
update_post_meta( $order_id, '_shipping_address_1', $shipping_address_1 . $separator . $shipping_houseno );
}
add_action('woocommerce_checkout_update_order_meta', 'customise_checkout_field_update_order_meta',10,2);
function customise_checkout_field_update_order_meta($order_id, $posted_data)
{
//write your code here to update custom data.
}
<?php
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['billing_houseno'] ) ) {
$billing_houseno = $_POST['billing_houseno'];
$billing_address_1 = $_POST['billing_address_1'];
$new_billing_address = $billing_houseno.'.'.$billing_address_1;
update_post_meta( $order_id, '_billing_address_1', $new_billing_address );
}
if(!empty($_POST['shipping_houseno'])){
$shipping_houseno = $_POST['shipping_houseno'];
$shipping_address_1 = $_POST['shipping_address_1'];
$new_shipping_address = $shipping_houseno.'.'.$shipping_address_1;
update_post_meta( $order_id, '_shipping_address_1', $new_shipping_address );
}
}
?>