我遵循了一个教程,关于如何包括在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['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'] ) );
}
}} add_action( 'woocommerce_checkout_update_order_meta','cloudways_save_extra_checkout_fields',10,2 );
但是如果没有填写,我不知道如何将帐单地址数组保存到这个自定义文本字段中。
提前谢谢。
发布于 2017-10-26 00:59:11
您可以尝试将地址构建到数组中,并将其保存在字段中。或者您也可以将其构建为字符串,这取决于您希望字段包含什么数据,字符串或数组。下面是一个数组的示例:
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 );https://stackoverflow.com/questions/46935696
复制相似问题