Woocommerce - 如果购物车中有特定变体,则隐藏付款方式

2023-12-04

在 Woocommerce 中,如果购物车中有特定产品变体,我想隐藏信用卡付款选项。请帮忙。

Thanks.

这就是我现在的工作。我为每个变体分配了一个单独的运输类别,我想在结帐时禁用特定的付款方式。但如果我可以定位特定的属性值,那就容易多了,这样我就不必分配运输类别。

 <?php


add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);

function conditional_payment_gateways( $available_gateways ) {
   $shipping_class_target = 106; // the shipping class ID assigned to specific variations 
   $in_cart = false;
   foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
      if ( $values[ 'data' ]->get_shipping_class_id() == $shipping_class_target ) {
         $in_cart = true;
         break;
      } 
   }
   if ( $in_cart ) {
       unset($available_gateways['cod']); // unset 'cod' 

   }
   else {
       unset($available_gateways['bacs']); // unset 'bacs' 

   }
   return $available_gateways;
}

如果您要检查购物车中每件商品的变体,则必须查找属性$product->get_attributes()然后循环遍历它们并获取每个数组的键和值。

在这个例子中,我使用了

尺寸 (pa_size) 和小号

add_filter('woocommerce_available_payment_gateways', 'conditional_payment_gateways', 10, 1);
function conditional_payment_gateways( $available_gateways ) {
   $in_cart = false;
   foreach ( WC()->cart->get_cart_contents() as $key => $values ) {
       // See if there is an attribute called 'pa_size' in the cart
       // Replace with whatever attribute you want
       if (array_key_exists('pa_size', (array) $values['data']->get_attributes() ) ) {
       foreach ($values['data']->get_attributes() as $attribute => $variation);
           // Replace 'small' with your value.  
           if ($variation == 'small') $in_cart = true; //edited
      } 
   }
   if ( $in_cart ) {
       unset($available_gateways['cod']); // unset 'cod' 

   }
   else {
       unset($available_gateways['bacs']); // unset 'bacs' 

   }
   return $available_gateways;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Woocommerce - 如果购物车中有特定变体,则隐藏付款方式 的相关文章

随机推荐