duanchoupo1104 2017-10-25 14:55
浏览 40

如果在woocommerce中为空,请将结算地址保存到自定义结帐字段

I have followed a tutorial on how to include custom fields at checkout in woocommerce, everything works, as expected but I want to save the full billing address to this custom field if is not filled.

This is what I have

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 );

But I don't know how to save the array of billing address to this custom text field if it's not filled.

Link to tutorial is https://www.cloudways.com/blog/how-to-edit-delete-fields-and-email-in-woocommerce-custom-checkout-fields/amp/

Thanks in advance.

  • 写回答

1条回答 默认 最新

  • dqaq59269 2017-10-25 16:59
    关注

    You can try building the address into an array and saving that in the field. Or you can also build it to a string, really depends what data you want the field to contain, string or array. Here is an example of an array:

    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 );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测