首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >根据收件和交付按钮显示或隐藏WooCommerce结帐字段

根据收件和交付按钮显示或隐藏WooCommerce结帐字段
EN

Stack Overflow用户
提问于 2020-08-24 02:40:48
回答 2查看 549关注 0票数 1

亲爱的们:

我正在使用代码片段插件将我的代码添加到我的电子商务项目中,我在我的交付选项中有提货和交付插件,我试图做的是,一旦我选择了提货选项,客户地址信息字段将被隐藏,这是不符合逻辑的,如果选择了从餐厅提货,则它是必填的。

我尝试添加一些其他的变量,其中一些作品很好,而另一些作品在加载页面后仍然出现(在加载和选择拾取之前,它们消失了(例如街道),加载后街道返回并仍然出现

https://www.order.ramadaencorekuwait.com/checkout-2/

如果代码有任何问题,请让我知道

代码语言:javascript
复制
add_action('wp_footer', 'custom_checkout_js_script');
function custom_checkout_js_script() {
    if( is_checkout() && ! is_wc_endpoint_url() ) :
    ?>
    <script language="javascript">
    jQuery( function($){
        var a = 'input[name="pi_delivery_type"]:checked',
            b = 'input[name="billing_address_4"]';
        var d = 'input[name="billing_address_3"]';
        var f = 'input[name="billing_avenue"]';
        var z = 'input[name="billing_address_2"]';
        var s = 'input[name="select2-billing_state-container"]';
        
        
        // Custom function that show hide specific field, based on radio input value
        function showHideField( value, field ){
            if ( value === 'pickup' ) {
                $(field).parent().parent().hide();
            } else {
                $(field).parent().parent().show();
            }
        }

        // On start after DOM is loaded
        showHideField( $(a).val(), b );
        showHideField( $(a).val(), d );
        showHideField( $(a).val(), f );
        showHideField( $(a).val(), z );
        showHideField( $(a).val(), s );
        // On radio button change live event
        $('form.woocommerce-checkout').on('change', a, function() {
            showHideField( $(this).val(), b );
            
            showHideField( $(this).val(), d );
            showHideField( $(this).val(), f );
            showHideField( $(this).val(), z );
            showHideField( $(this).val(), s );
        });
        
    });
    </script>
    <?php
    endif;
}
EN

回答 2

Stack Overflow用户

发布于 2020-08-24 06:06:13

给b、d、f、z和s分配一个类可能更简单。如果你已经定义了'a',那么你可以这样做:

代码语言:javascript
复制
$(a).change(function() {
   if ($(a).val() == 'pickup') {
      $(".special").hide();
   } else {
      $(".special").show();
   }
});
票数 0
EN

Stack Overflow用户

发布于 2020-08-24 23:53:00

尝试下面的代码,当传递方法是本地拾取时,在任何地方都禁用提交表单。

您应该将(28):"local_pickup:28“替换为您的发货方法的id。

代码语言:javascript
复制
add_action( 'woocommerce_after_checkout_form', 'disable_shipping_local_pickup' );
function disable_shipping_local_pickup( $available_gateways ) {
   // Part 1: Hide shipping based on the static choice @ Cart
   // Note: "#customer_details .woocommerce-shipping-fields" (formerly "#customer_details .col-2", but was too broad & was also hidding additional fields that aren't shipping related that just happened to be in the second column) strictly depends on your theme
   $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
   $chosen_shipping = $chosen_methods[0];
   if ( 0 === strpos( $chosen_shipping, 'local_pickup:28' ) ) {
   ?>
      <script type="text/javascript">
         jQuery('#customer_details .woocommerce-shipping-fields').fadeOut();
      </script>
   <?php
   }
   // Part 2: Hide shipping based on the dynamic choice @ Checkout
   // Note: "#customer_details .woocommerce-shipping-fields" (formerly "#customer_details .col-2", but was too broad & was also hidding additional fields that aren't shipping related that just happened to be in the second column) strictly depends on your theme
   ?>
      <script type="text/javascript">
         jQuery('form.checkout').on('change','input[name^="shipping_method"]',function() {
            var val = jQuery('input[name^="shipping_method"]:checked').val(); // If it changed, then it must be the radio options so check the one that's selected
            if (val.match("^local_pickup:28")) {
              jQuery('#customer_details .woocommerce-shipping-fields').fadeOut();
            } else {
              jQuery('#customer_details .woocommerce-shipping-fields').fadeIn();
            }
         });
         // Also check if the zipcode switched to something where local pickup is the only option (similar to what's done in part 1 above, but happen based on what's currently being entered on the page [watch via ajaxComplete since the options that are available/selected might not be present when the zipcode change happens & it needs to load those in via AJAX])
         jQuery(document).ajaxComplete(function(){
             if(jQuery('input[name^="shipping_method"]').attr('type') === 'hidden'){ // There's only one option so check the hidden input field with the value
                var val = jQuery('input[name^="shipping_method"]').val();
            }else{ // Otherwise, it must be the radio options so check the one that's selected
                var val = jQuery('input[name^="shipping_method"]:checked').val();
            }
            if (val.match("^local_pickup:28")) {
              jQuery('#customer_details .woocommerce-shipping-fields').fadeOut();
            } else {
              jQuery('#customer_details .woocommerce-shipping-fields').fadeIn();
            }
         });
      </script>
   <?php
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63550763

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档