获取选定的状态字段值以在Woocommerce结帐查看顺序中显示它

时间:2018-12-09 04:38:27

标签: php wordpress woocommerce state checkout

我需要在验证时检查billing_state的值等于“ xyz”。并在“ woocommerce_review_order_before_submit”中显示该值。

add_action( 'woocommerce_review_order_before_submit', 'messageonstate' );

function messageonstate() {
    echo $fields['billing']['billing_state']['value'];
    echo $_POST['_billing_state'];
    echo"====+++++";
    foreach( $checkout_fields as $key_field => $field_value ){
        if( $input == $key_field && ! empty( $field_value ) ){
            echo       $value = $field_value;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

提交区域之前按审阅顺序显示所需的代码为:

add_action( 'woocommerce_review_order_before_submit', 'review_order_before_submit_state_message' );
function review_order_before_submit_state_message() {
    // HERE set your state code
    $state_code = 'CA';

    if( $selected_state_code = WC()->customer->get_billing_state() ){
        $country_code = WC()->customer->get_billing_country();
        $state_name   = WC()->countries->get_states($country_code)[$state_code];

        if( WC()->customer->get_billing_state() === $state_code ){
            $message = "The billing state <strong>".$state_name."<strong> matches";
            echo '<ul class="woocommerce-info">'.$message.'</ul>';
        } else {
            $message = "The billing state <strong>".$state_name."<strong> don't matches";
            echo '<ul class="woocommerce-alert">'.$message.'</ul>';
        }
    }
}

代码进入您的活动子主题(或活动主题)的function.php文件中。经过测试,可以正常工作。

  

注意:Woocommerce中为避免结帐而进行的字段验证的处理方式有所不同。