仅当Woocommerce结帐中未隐藏订单备注字段时,才需要

时间:2019-03-21 08:57:37

标签: php jquery wordpress woocommerce checkout

在Woocommerce结帐页面中,使用Show hide Order notes field based on a checkbox in Woocommerce checkout” 为我的问题之一回答代码。

我希望仅在可见时才需要“订单注释”字段。
在可见(不隐藏)时,如何使此“订单注释”字段必填,并对其进行验证?

欢迎任何帮助。

1 个答案:

答案 0 :(得分:0)

以下代码仅在未隐藏“订购说明”字段时才需要它,并且在这种情况下将启用字段验证。

  

注释(强制):“订单注释”字段必须是可选的(就像默认的Woocommerce中一样),否则,代码将无法正常工作。

完整代码:

// Remove "(optional)" label on "Order notes" field
add_filter( 'woocommerce_form_field' , 'remove_order_comments_optional_fields_label', 10, 4 );
function remove_order_comments_optional_fields_label( $field, $key, $args, $value ) {
    // Only on checkout page for Order notes field
    if( 'order_comments' === $key && is_checkout() ) {
        $optional = '&nbsp;<span class="optional">(' . esc_html__( 'optional', 'woocommerce' ) . ')</span>';
        $field = str_replace( $optional, '', $field );
    }
    return $field;
}

// The jQuery script that show hide "Order notes" field
add_action( 'woocommerce_after_order_notes', 'custom_checkout_scripts_js_script', 10, 1 );
function custom_checkout_scripts_js_script( $checkout ) {
    $required = esc_html__( 'required', 'woocommerce' );

    ?>
    <script type="text/javascript">
    (function($){
        var required     = '<abbr class="required" title="<?php echo $required; ?>">*</abbr>',
            notesField   = '#order_comments_field',
            shipCheckbox = '#ship-to-different-address-checkbox';

        function actionRequire( actionToDo='yes', selector='' ){
            if ( actionToDo == 'yes' ) {
                $(selector).addClass("validate-required");
                $(selector+' label').append(required);
            } else {
                $(selector).removeClass("validate-required");
                $(selector+' label > .required').remove();
            }
            $(selector).removeClass("woocommerce-validated");
            $(selector).removeClass("woocommerce-invalid woocommerce-invalid-required-field");
        }

        // On load and on form change
        $('form.checkout').on( 'change', shipCheckbox, function(){
            if( $(this).prop('checked') === true ) {
                $(notesField).hide(); // Show
                actionRequire( 'no' ,notesField );
            } else {
                $(notesField).show(); // Hide
                actionRequire( 'yes' ,notesField );
            }
        });
    })(jQuery);
    </script>
    <?php
}

// "Order notes" field validation
add_action('woocommerce_checkout_process', 'order_comments_field_validation');
function order_comments_field_validation() {
    if ( isset($_POST['order_comments']) && empty($_POST['ship_to_different_address']) )
        wc_add_notice( __( 'Please fill in "Order notes" required field.' ), 'error' );
}

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


类似:Make Woocommerce checkout phone field optional based on shipping country
相关:Show hide Order notes field based on a checkbox in Woocommerce checkout