【问题标题】:Woocommerce - Hide payment method if specific variation is in cartWoocommerce - 如果购物车中有特定变体,则隐藏付款方式
【发布时间】:2020-08-09 14:10:50
【问题描述】:

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

谢谢。

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

 <?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;
}

【问题讨论】:

  • 是的,我做到了。你试过不草率下结论吗?
  • 您的代码在我测试时有效。你确定你有正确的运输类别目标吗?
  • 它也适用于我。但我希望我可以在不使用运输类的情况下使其工作。基本上我不知道如何检查每个购物车项目的产品变化。

标签: wordpress woocommerce


【解决方案1】:

如果您要检查购物车中每个商品的变体,您必须查找属性 $product-&gt;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;
}

【讨论】:

猜你喜欢
  • 2022-06-12
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 2022-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多