【问题标题】:WooCommerce NON EU VAT message on checkout - changing code to include an array instead of single value结帐时的 WooCommerce 非欧盟增值税消息 - 更改代码以包含数组而不是单个值
【发布时间】:2021-12-24 15:46:30
【问题描述】:

我在结帐时使用 BBloomer 的修改后的 sn-p。

我查找所选国家/地区,如果国家/地区是挪威“否”,它会在有关非欧盟国家/地区的 TAX 信息的总额之后回显一条消息。

由于我们现在也向英国和 Switcherland 发货,因此我想在 mu 脚本中包含这些国家/地区。

这意味着 var countryCode = 'NO';应该类似于 var countryCode = array('NO','UK','CH');还是?

其他强大的也需要处理数组而不是单个值。

希望任何人都可以提供帮助。

// The message
 
add_action( 'woocommerce_review_order_after_order_total', 'bbloomer_echo_notice_shipping' );
  
function bbloomer_echo_notice_shipping() {
   echo '<tr class="non-eu-tax-notice" style="display:none">
   <th>'. __( 'Notice', 'woocommerce' ) .'</th>
   <td data-title=" '. __( 'Notice', 'woocommerce' ) .' ">'. __( 'No VAT charged. Please be aware that VAT and customs can be declared in your home country. More info here', 'woocommerce' ) .'</td>
   </tr>';
}
  

// Show or hide message based on billing country
  
add_action( 'woocommerce_checkout_after_order_review', 'bbloomer_show_notice_shipping' );
  
function bbloomer_show_notice_shipping(){
     
   wc_enqueue_js( "
  
      // Set the country code that will display the message
      var countryCode = 'NO';
 
      // Get country code from checkout
      selectedCountry = $('select#billing_country').val();
 
      // Function to toggle message
      function toggle_upsell( selectedCountry ) {   
         if( selectedCountry == countryCode ){
            $('.non-eu-tax-notice').show();
         }
         else {
            $('.non-eu-tax-notice').hide();
         }
      }
 
      // Call function
      toggle_upsell( selectedCountry );
      $('select#billing_country').change(function(){
         toggle_upsell( this.value );         
      });
  
   " );
     
}

【问题讨论】:

    标签: php woocommerce checkout


    【解决方案1】:

    我可以帮忙! :D

    我是WooCommerce: Show Message Upon Country Selection @ Checkout 教程作者。

    在 jQuery/JS 中,你可以定义一个数组:

    var countryCode = [ 'NO', 'GB', 'CH' ];
    

    此外,您可以使用 JS 等效的 PHP 的 in_array() 即 inArray() 来查看元素是否在定义的国家列表中:

    if ( $.inArray(selectedCountry,countryCode) !== -1 ){
    

    最终代码:

    function bbloomer_show_notice_shipping(){
         
       wc_enqueue_js( "
      
          // Set the country code that will display the message
          var countryCode = [ 'NO', 'GB', 'CH' ];
     
          // Get country code from checkout
          selectedCountry = $('select#billing_country').val();
     
          // Function to toggle message
          function toggle_upsell( selectedCountry ) {   
             if ( $.inArray(selectedCountry,countryCode) !== -1 ){
                $('.non-eu-tax-notice').show();
             }
             else {
                $('.non-eu-tax-notice').hide();
             }
          }
     
          // Call function
          toggle_upsell( selectedCountry );
          $('select#billing_country').change(function(){
             toggle_upsell( this.value );         
          });
      
       " );
         
    }
    

    【讨论】:

      猜你喜欢
      • 2022-11-11
      • 1970-01-01
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多