在 WooCommerce 结帐时如何自动检查到许多免费送货选择状态之一?

How to auto checked to one of many shipping free choose state when checkout in WooCommerce?

我在制作这个网站时遇到了问题。

有要购买的商品:http://tn.freshbrand.vn/danh-muc/than-khu-mui

结帐:http://tn.freshbrand.vn/checkout

当用户购买超过 2 件产品并选择 "Ngoài HCm" 作为发货地时:
运费显示 2 个可能的单选按钮以供检查。
选项 1:45,000(对于 1 个产品)
选项 2:65,000(不适用于 1 个产品)。

当用户选择状态 (Chọn Quận, huyện) == "Ngoài HCm".
并且购物车中有 1 个产品,运费应自动检查为 45,000(默认)。
并且对于购物车中超过 1 个产品,应该自动检查到 65,000。

以前,当购物车中的产品超过 2 件时,我会检查选项 65,000,但这并不正常。我认为 jQuery 必须在选择状态(交付目的地)时设置。

下面是我的代码形式-billing.php

global $woocommerce;
  $attendee_count = 0;
  if (sizeof($woocommerce->cart->get_cart())>0) :
    foreach ($woocommerce->cart->get_cart() as $item_id => $values) :
      $_product = $values['data'];
      if ($_product->exists() && $values['quantity']>0) :
        if (get_post_meta($_product->id, '_tribe_wooticket_for_event') > 0)
          $attendee_count += $values['quantity'];
      endif;
    endforeach;
  endif; 
  echo "<div class='sl_sp'>Number product in cart: <span class='soluong_sp'>" . $attendee_count . "</span></div>";
  if($attendee_count == 1 ){ // so luong la 1 sp
    ?>
    <style>
    .woocommerce ul#shipping_method li:nth-child(2)
    {display: none;}
    </style>
    <?
  }else{ ?>

  <style>
    .woocommerce ul#shipping_method li:nth-child(2){display: block;}
    .woocommerce ul#shipping_method li:nth-child(1){display: none;}
  </style>
  <script>
  jQuery(document).ready(function() {
    jQuery("input#shipping_method_0_oik_weight_zone_shipping_45").prop( "checked", true );
  });
  </script>

<?php }  ?>

我将默认状态设置为 'Ngoài HCM',当我添加 1 个或多个产品时它会正确显示。 But when chosen another state, and choose 'Ngoài HCm' again, it's wrong (not checked correctly).

这段代码是正确的:

jQuery("input#shipping_method_0_oik_weight_zone_shipping_45").prop( "checked", true );

我在您的结帐页面的控制台中尝试过,它正在运行。
现在,由于此代码是使用 Ajax 在页面中注入的,因此您不需要 "ready" 函数包装器。

页面已经准备就绪。
load 事件将不会发生。

很明显,试试这个:

<script>
  jQuery("input#shipping_method_0_oik_weight_zone_shipping_45").prop( "checked", true );
</script>

而不是这个:

<script>
jQuery(document).ready(function() {
  jQuery("input#shipping_method_0_oik_weight_zone_shipping_45").prop( "checked", true );
});
</script>