【问题标题】:Mandatory custom radio button choice on Woocommerce checkoutWoocommerce 结帐时强制自定义单选按钮选择
【发布时间】:2020-02-01 01:05:09
【问题描述】:

我有这个自定义代码可以在结帐中添加几个单选按钮:

add_action( 'woocommerce_review_order_before_payment', 'display_extra_fields_after_billing_address' , 10, 1 );
function display_extra_fields_after_billing_address () { ?>
    <h3 class="delivery-options-heading">Delivery Options <sup>*</sup></h3>
    <div class="delivery-options">
        <p><input type="radio" name="delivery_option" value="Have the courier leave a card." required /> Have the courier leave a card.</p>
        <p><input type="radio" name="delivery_option" value="Leave the consignment without a signature." required /> Leave the consignment without a signature.</p>
    </div>
<?php 
}

add_action( 'woocommerce_checkout_update_order_meta', 'add_delivery_option_to_order' , 10, 1);
function add_delivery_option_to_order ( $order_id ) {

    if ( isset( $_POST ['delivery_option'] ) &&  '' != $_POST ['delivery_option'] ) {
        add_post_meta( $order_id, '_delivery_option',  sanitize_text_field( $_POST ['delivery_option'] ) );
    }
}


add_filter( 'woocommerce_email_order_meta_fields', 'add_delivery_option_to_emails' , 10, 3 );

function add_delivery_option_to_emails ( $fields, $sent_to_admin, $order ) {
    if( version_compare( get_option( 'woocommerce_version' ), '3.0.0', ">=" ) ) {            
        $order_id = $order->get_id();
    } else {
        $order_id = $order->id;
    }

    $delivery_option = get_post_meta( $order_id, '_delivery_option', true );

    if ( '' != $delivery_option ) {
    $fields[ 'Delivery Date' ] = array(
        'label' => __( 'Delivery Option', 'delivery_option' ),
        'value' => $delivery_option,
    );
    }
    return $fields;
}

除了单选按钮上的required 属性外,所有部分都可以正常工作,让用户无需做出选择即可结帐,这并不理想。出于某种原因,required 属性被忽略了。

如何强制用户在结帐前选择其中一个单选按钮?

【问题讨论】:

    标签: wordpress forms woocommerce


    【解决方案1】:

    请将此验证挂钩与您的代码一起添加,以在结帐时验证您的选项

    function action_woocommerce_after_checkout_validation( $data, $errors ) { 
        if( !isset( $data['delivery_option'] ) || empty( $data['delivery_option'] ) ) :
            $errors->add( 'required-field', __( 'No delivery option has been selected. Please choose delivery option.', 'woocommerce' ) );
        endif; //Endif
    }
    // add the action 
    add_action( 'woocommerce_after_checkout_validation', 'action_woocommerce_after_checkout_validation', 10, 2 );
    

    【讨论】:

    • 差不多了。但即使我选择任何一个单选按钮,它也会显示验证错误。
    • 嗨@User_FTW请添加以下钩子,现在可以使用function filter_woocommerce_checkout_posted_data($data){$data['delivery_option'] = isset( $_POST['delivery_option'] ) ? $_POST['delivery_option'] : '';return $data;}add_filter('woocommerce_checkout_posted_data', 'filter_woocommerce_checkout_posted_data');
    【解决方案2】:

    这是解决方案,与Krunal Prajapati 建议的略有不同。唯一的区别是第二行:

    function action_woocommerce_after_checkout_validation( $data, $errors ) { 
        if ( empty( $_POST['delivery_option'] ) ) :
            $errors->add( 'required-field', __( 'No delivery option has been selected. Please choose delivery option.', 'woocommerce' ) );
        endif; //Endif
    }
    // add the action 
    add_action( 'woocommerce_after_checkout_validation', 'action_woocommerce_after_checkout_validation', 10, 2 );
    

    【讨论】:

      猜你喜欢
      • 2017-03-21
      • 1970-01-01
      • 2020-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 2020-01-11
      相关资源
      最近更新 更多