【问题标题】:Get the value of a custom checkout field in woocommerce 3在 woocommerce 3 中获取自定义结帐字段的值
【发布时间】:2019-01-16 07:51:12
【问题描述】:

在 Woocommerce 结帐中,我添加了一个自定义结帐字段,这是我的代码:

add_action( 'woocommerce_before_order_notes', 'shipping_add_select_checkout_field' );
function shipping_add_select_checkout_field( WC_Checkout $checkout ) {
    $options = array_merge( [ '' => __( 'Nothing to select' ), ], city_zone() );
    woocommerce_form_field( 'billing_country_zone', array(
            'type'     => 'select',
            'class'    => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
            'label'    => __( 'City zone' ),
            'required' => true,
            'options'  => $options
    ), WC()->customer->billing_country_zone );
}

现在我完全迷失了,因为我需要知道WC()->customer->billing_country_zone 是什么以及如何检查它的价值......

非常感谢任何帮助。

【问题讨论】:

    标签: php wordpress woocommerce field checkout


    【解决方案1】:

    对于WC()->customer->billing_country_zone

    • 自 Woocommerce 3 以来,首先可以访问大多数 Woocommerce 实例对象的属性。
    • 并且“billing_country_zone”不是WC_Customer 实例对象的默认属性。

    因为它是关于结帐字段,所以您应该使用$checkout 参数,它是WC_Checkout 对象的实例。然后有适当的method get_value() 用于它......

    这是干什么用的?
    一旦客户提交了至少一个订单,“billing_country_zone”的选定值将显示在结帐页面上。

    所以你将不得不更换这一行:

    ), WC()->customer->billing_country_zone );
    

    这个:

    ), $checkout->get_value('billing_country_zone') );
    

    如果$checkout变量参数没有定义,你将使用WC()->checkout like:

    ), WC()->checkout->get_value('billing_country_zone') );
    

    现在,当您保存此自定义结帐字段值时,您需要保存它:

    1. 作为订单元数据
    2. 作为用户元数据

    所以这是完整的代码(已注释)

    // Display custom checkout field
    add_action( 'woocommerce_before_order_notes', 'display_custom_checkout_field' );
    function display_custom_checkout_field( $checkout ) {
        $options = array_merge( [ '' => __( 'Nothing to select' ), ], city_zone() );
        woocommerce_form_field( 'billing_country_zone', array(
                'type'     => 'select',
                'class'    => array( 'form-row-wide', 'address-field', 'update_totals_on_change' ),
                'label'    => __( 'City zone' ),
                'required' => true,
                'options'  => $options
        ), $checkout->get_value('billing_country_zone') );
    }
    
    // custom checkout field validation
    add_action( 'woocommerce_checkout_process', 'custom_checkout_field_validation' );
    function custom_checkout_field_validation() {
        if ( isset( $_POST['billing_country_zone'] ) && empty( $_POST['billing_country_zone'] ) )
            wc_add_notice( __( 'Please select a <strong>"City zone"</strong>.', 'woocommerce' ), 'error' );
    }
    
    // Save custom checkout field value as custom order meta data and user meta data too
    add_action( 'woocommerce_checkout_create_order', 'custom_checkout_field_update_order_meta', 20, 2 );
    function custom_checkout_field_update_order_meta( $order, $data ) {
        if ( isset( $_POST['billing_country_zone'] ) ) {
            // Save custom checkout field value
            $order->update_meta_data( '_billing_country_zone', esc_attr( $_POST['billing_country_zone'] ) );
    
            // Save the custom checkout field value as user meta data
            if( $order->get_customer_id() )
                update_user_meta( $order->get_customer_id(), 'billing_country_zone', esc_attr( $_POST['billing_country_zone'] ) );
        }
    }
    

    代码进入您的活动子主题(或活动主题)的 function.php 文件中。经过测试并且有效。

    【讨论】:

    • 非常感谢它的工作......如果我需要将该字段值发送到我的自定义函数以计算托运人区域和目的地区域之间的费率,那么可以这样做 $checkout=new WC_Checkout() ; $zone = $checkout->get_value('billing_country_zone');并发送 $zone 值?或者我可以从函数或 wordpress 中的其他内容中获取字段值?
    • @RowanMamdouh 欢迎......正如所解释的,这个值只有在客户之前已经购买过东西的情况下才存在。因此,您可以使用get_user_meta( get_current_user_id(), 'billing_country_zone', true ); 从用户数据中获取此值(测试是否存在) ... 或者在后端从现有的$order 对象中使用$order-&gt;get_meta( '_billing_country_zone' );跨度>
    猜你喜欢
    • 2019-04-24
    • 2018-11-02
    • 2018-02-06
    • 2023-04-10
    • 2020-10-21
    • 2019-05-19
    • 1970-01-01
    相关资源
    最近更新 更多