亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

根據提貨和送貨按鈕顯示或隱藏 WooCommerce 結帳字段

根據提貨和送貨按鈕顯示或隱藏 WooCommerce 結帳字段

PHP
浮云間 2023-10-22 22:08:49
我正在使用代碼片段插件將我的代碼添加到我的電子商務項目中,我的送貨選項中有取貨和送貨插件,我想做的是,一旦我選擇取貨選項,客戶地址信息字段將被隱藏如果選擇從餐廳取貨,則保持其顯示和強制性是不合邏輯的。我嘗試添加一些其他變量,其中一些工作正常,其他變量在加載頁面后仍然出現(在加載和選擇皮卡之前,它們消失了(例如街道),在加載街道返回后仍然出現https://www.order.ramadaencorekuwait.com/checkout-2/如果代碼有任何問題,請告訴我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;}
查看完整描述

2 回答

?
至尊寶的傳說

TA貢獻1789條經驗 獲得超10個贊

將類分配給 b、d、f、z 和 s 可能會更簡單。如果您已經定義了“a”,那么您可以執行以下操作:


$(a).change(function() {

   if ($(a).val() == 'pickup') {

      $(".special").hide();

   } else {

      $(".special").show();

   }

});


查看完整回答
反對 回復 2023-10-22
?
楊魅力

TA貢獻1811條經驗 獲得超6個贊

嘗試使用以下代碼,當交付方式為本地取貨時,該代碼會在任何地方禁用提交表單。


您應該將 (28): "local_pickup:28" 替換為您的送貨方式的 ID。


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

}


查看完整回答
反對 回復 2023-10-22
  • 2 回答
  • 0 關注
  • 162 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號