【问题标题】:Require additional notes conditional on shipping type需要根据运输类型的附加说明
【发布时间】:2021-01-24 10:56:59
【问题描述】:

如果选择了 Parcel point 取件运输方式 (flat_rate:47),我正在尝试在结帐时将 Additional notes 设为必填字段。如果我不使用特定 ID (47),它会起作用,并且需要在所有统一费率运输方式上填写订单备注。但我希望它仅适用于 flat_rate:47 运输。到目前为止,这是我的代码,但它不起作用。知道我做错了什么吗?

// Validate mandatory "Order notes" field for "Parcel pickup point" shipping methods
add_action( 'woocommerce_checkout_process', 'flat_rate_order_comment_validation', 20 );
function flat_rate_order_comment_validation() {
    $chosen_shipping = WC()->session->get( 'chosen_shipping_methods' )[0];
    $chosen_shipping = explode(':', $chosen_shipping);
    if ( $chosen_shipping[0] == 'flat_rate:47' && empty($_POST['order_comments']) ){
        wc_add_notice( __( "You need to fill up \"Order notes\" with some details.", "woocommerce" ), 'error' );
    }
}

【问题讨论】:

    标签: methods conditional-statements shipping


    【解决方案1】:

    正确代码:

    // Validate mandatory "Order notes" field for "Parcel pickup point" shipping methods
    add_action( 'woocommerce_checkout_process', 'flat_rate_order_comment_validation', 20 );
    function flat_rate_order_comment_validation() {
        $chosen_shipping = WC()->session->get( 'chosen_shipping_methods' )[0];
           if ( $chosen_shipping == 'flat_rate:47' && empty($_POST['order_comments']) ){
            wc_add_notice( __( "You need to fill up \"Order notes\" with some details.", "woocommerce" ), 'error' );
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多