Php 如果在woocommerce中为空,则将帐单地址保存到自定义签出字段
我遵循了一个教程,介绍了如何在woocommerce中的结帐时包含自定义字段,一切正常,但如果未填充,我希望将完整的帐单地址保存到此自定义字段 这就是我所拥有的Php 如果在woocommerce中为空,则将帐单地址保存到自定义签出字段,php,wordpress,woocommerce,Php,Wordpress,Woocommerce,我遵循了一个教程,介绍了如何在woocommerce中的结帐时包含自定义字段,一切正常,但如果未填充,我希望将完整的帐单地址保存到此自定义字段 这就是我所拥有的 function cloudways_save_extra_checkout_fields( $order_id, $posted ){ // don't forget appropriate sanitization if you are using a different field type if( isset( $posted[
function cloudways_save_extra_checkout_fields( $order_id, $posted ){
// don't forget appropriate sanitization if you are using a different field type
if( isset( $posted['cloudways_text_field'] ) ) {
update_post_meta( $order_id, '_cloudways_text_field', sanitize_text_field( $posted['cloudways_text_field'] ) );
if(empty($posted['cloudways_text_field']))
{
// it's empty!
update_post_meta( $order_id, '_cloudways_text_field', sanitize_text_field( $posted['cloudways_text_field'] ) );
}
else
{
update_post_meta( $order_id, '_cloudways_text_field', sanitize_text_field( $posted['cloudways_text_field'] ) );
}
}
}
添加操作“woocommerce\u checkout\u update\u order\u meta”、“cloudways\u save\u extra\u checkout\u fields”,10,2
但是如果这个自定义文本字段没有填充,我不知道如何将账单地址数组保存到这个自定义文本字段中
链接到教程是
提前感谢。您可以尝试将地址构建到数组中,并将其保存在字段中。或者也可以将其构建为字符串,具体取决于您希望字段包含的数据,字符串或数组。 以下是一个数组示例:
function cloudways_save_extra_checkout_fields( $order_id, $posted ) {
// don't forget appropriate sanitization if you are using a different field type
if ( isset( $posted['cloudways_text_field'] ) ) {
update_post_meta( $order_id, '_cloudways_text_field', sanitize_text_field( $posted['cloudways_text_field'] ) );
if ( empty( $posted['cloudways_text_field'] ) ) {
$billing_address_array = array(
'billing_address_1' => $posted['billing_address_1'],
'billing_address_2' => $posted['billing_address_2'],
'billing_city' => $posted['billing_city'],
'billing_postcode' => $posted['billing_postcode'],
'billing_state' => $posted['billing_state'],
'billing_country' => $posted['billing_country'],
);
update_post_meta( $order_id, '_cloudways_text_field', wc_clean( $billing_address_array ) );
} else {
update_post_meta( $order_id, '_cloudways_text_field', sanitize_text_field( $posted['cloudways_text_field'] ) );
}
}
}
add_action( 'woocommerce_checkout_update_order_meta', 'cloudways_save_extra_checkout_fields', 10, 2 );
它工作得很好,因为我只需要纯文本,我只是将wc_clean部分修改为:introde,$billing_address_数组;非常感谢