【问题标题】:WooCommerce hide billing address if Cash on Delivery Chosen如果选择货到付款,WooCommerce 隐藏帐单地址
【发布时间】:2020-04-04 22:53:22
【问题描述】:

在 WooCommerce 中,我希望能够在选择货到付款时隐藏帐单地址字段,因为无需填写地址。但如果客户选择信用卡,帐单地址应该会出现。

我真的不知道从哪里开始,所以我没有任何代码。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案1】:

    我能够对上面的代码进行一些扩展...

    // Make Billing Address not required
    add_filter( 'woocommerce_default_address_fields' , 'filter_default_address_fields', 20, 1 );
    function filter_default_address_fields( $address_fields ) {
        // Only on checkout page
        if( ! is_checkout() ) return $address_fields;
    
        // All field keys in this array
        $key_fields = array('country','company','address_1','address_2','city','state','postcode');
    
        // Loop through each address fields (billing and shipping)
        foreach( $key_fields as $key_field )
            $address_fields[$key_field]['required'] = false;
    
        return $address_fields;
    }
    

    如果是货到付款则隐藏帐单地址,如果是信用卡则显示

    jQuery(function(){
        jQuery( 'body' )
        .on( 'updated_checkout', function() {
              usingGateway();
    
            jQuery('input[name="payment_method"]').change(function(){
                console.log("payment method changed");
                  usingGateway();
    
            });
        });
    });
    
    
    function usingGateway(){
        console.log(jQuery("input[name='payment_method']:checked").val());
        if(jQuery('form[name="checkout"] input[name="payment_method"]:checked').val() == 'cod'){
            jQuery('.woocommerce-billing-fields #billing_company_field, .woocommerce-billing-fields #billing_country_field, .woocommerce-billing-fields #billing_address_1_field, .woocommerce-billing-fields #billing_address_2_field, .woocommerce-billing-fields #billing_city_field, .woocommerce-billing-fields #billing_state_field, .woocommerce-billing-fields #billing_postcode_field, .woocommerce-shipping-fields').hide();
            //Etc etc
        } else {
                    jQuery('.woocommerce-billing-fields #billing_company_field, .woocommerce-billing-fields #billing_country_field, .woocommerce-billing-fields #billing_address_1_field, .woocommerce-billing-fields #billing_address_2_field, .woocommerce-billing-fields #billing_city_field, .woocommerce-billing-fields #billing_state_field, .woocommerce-billing-fields #billing_postcode_field, .woocommerce-shipping-fields').show();
        }
    }  
    

    【讨论】:

    • 你有解决方案来做这个 PHP 让没有 JS 的用户不会被提示输入帐单地址吗?
    【解决方案2】:

    首先,账单地址是 woo Commerce 的必填项,因此,即使您隐藏它们,也不会允许您结帐。所以首先你需要让它“不需要”它可以使用钩子来完成

    Make checkout addresses fields not required in WooCommerce

    然后就可以运行查询了

    jQuery(function(){
        jQuery( 'body' )
        .on( 'updated_checkout', function() {
              usingGateway();
    
            jQuery('input[name="payment_method"]').change(function(){
                console.log("payment method changed");
                  usingGateway();
    
            });
        });
    });
    
    
    function usingGateway(){
        console.log(jQuery("input[name='payment_method']:checked").val());
        if(jQuery('form[name="checkout"] input[name="payment_method"]:checked').val() == 'COD'){
            $('.woocommerce-billing-fields').hide();
            //Etc etc
        }
    }  
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-31
      • 1970-01-01
      • 2016-04-13
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      • 2021-05-28
      • 1970-01-01
      相关资源
      最近更新 更多