保存结帐自定义字段,将值合并到 WooCommerce 中的现有字段
Save checkout custom field merging the value to an existing field in WooCommerce
我根据所有可以找到的示例为 WooCommerce 创建了以下自定义结帐字段。
- 账单门牌号
billing_houseno
- 出货门牌号
shipping_houseno
输出显示地址字段(街道)和门牌号字段。工作得很好。将数据很好地存储在自定义门牌号字段中,地址像往常一样工作 - 当然。
我想将以下字段合并为一个:
billing_address_1
和 billing_houseno
在 billing_address_1
现有字段中。
shipping_address_1
和 shipping_houseno
在 shipping_address_1
现有字段中。
所以它会将两个字段合并到一个数据库字段中。
有没有办法将两个字段存储在一个现有字段中?
Update:
In your comment code link, there is some errors in your code:
- In your bones_add_field_and_reorder_fields function, you are unsetting billing and shipping company, but after few lines down, you are trying to set those values in:
$newfields['billing']['billing_company'] = $fields['billing']['billing_company'];
$newfields['shipping']['shipping_company'] = $fields['shipping']['shipping_company'];
And it's throwing some errors…
You should not unset 'billing_company'
and 'shipping_company'
.
- Everywhere you are using
$order->id
and it should be replaced by $order->get_id()
.
- I have tested everything with your updated code and my updated function below. It works…
使用挂钩在 woocommerce_checkout_update_order_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.
}
<?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 );
}
}
?>
我根据所有可以找到的示例为 WooCommerce 创建了以下自定义结帐字段。
- 账单门牌号
billing_houseno
- 出货门牌号
shipping_houseno
输出显示地址字段(街道)和门牌号字段。工作得很好。将数据很好地存储在自定义门牌号字段中,地址像往常一样工作 - 当然。
我想将以下字段合并为一个:
billing_address_1
和billing_houseno
在billing_address_1
现有字段中。shipping_address_1
和shipping_houseno
在shipping_address_1
现有字段中。
所以它会将两个字段合并到一个数据库字段中。
有没有办法将两个字段存储在一个现有字段中?
Update:
In your comment code link, there is some errors in your code:
- In your bones_add_field_and_reorder_fields function, you are unsetting billing and shipping company, but after few lines down, you are trying to set those values in:
$newfields['billing']['billing_company'] = $fields['billing']['billing_company'];
$newfields['shipping']['shipping_company'] = $fields['shipping']['shipping_company'];
And it's throwing some errors…
You should not unset'billing_company'
and'shipping_company'
.
- Everywhere you are using
$order->id
and it should be replaced by$order->get_id()
.
- I have tested everything with your updated code and my updated function below. It works…
使用挂钩在 woocommerce_checkout_update_order_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.
}
<?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 );
}
}
?>