【问题标题】:Conditionally making WooCommerce product non purchasable有条件地使 WooCommerce 产品不可购买
【发布时间】:2017-01-27 13:00:55
【问题描述】:

我一直在为 WooCommerce 的 sn-p 苦苦挣扎,我想将它添加到我孩子的主题功能中,但我的 PHP 知识已达到极限。

我设置了一个名为 pdt_volume 的自定义属性,每次添加产品时都会填写该属性。当购物车中所有商品的此属性总和达到 max_volume 限制时,我想删除 Add_to_cart 按钮。

比如Max_volume是100,我有4个pdt_volume = 20的产品,所以,add_to_cart按钮对于 pdt_volume 为 25 的产品,应将其删除,以便我无法将其添加到购物车中。

所以我想出了这个 sn-p,在这里和那里都可以找到一些代码。 但是functions.php不会保存它,并且该代码的其他变体已成功注册在functions.php中但网站会给出500错误......

请问,有人知道如何实现这一点吗?
我做错了什么?

谢谢。


EDIT 1 : 好的,所以我让编辑器在 functions.php 中实际注册了这段代码而没有中断,但在网站上,我仍然得到内部服务器错误。好像某事不是计算或某事。


编辑 2(2017 年 1 月 29 日)
我使用了很久以前购买的 ACF 插件,但没有找到任何合适的用途......到目前为止。

这是我能够想出的工作代码。这不是代码杰作,我不会因此获得任何奖励,但它似乎到目前为止还有效。至少,它允许我获得一个 TRUE/FALSE 语句,我可以在 if 条件中使用它来将 add_to_cart 按钮更改为 Your_box_is_full 按钮。 确实,我不需要任何 global $woocommerce$product

function get_cart_volume() {
  $cart_volume = 0;
  foreach( WC()->cart->get_cart() as $cart_item ) {
     $item_id = $cart_item['product_id'];
     $item_volume = get_field('product_volume', $item_id);
     $item_qty = $cart_item['quantity'];
     $vol_by_qty = $item_volume * $item_qty;
     $cart_volume += $vol_by_qty;
   }
  return $cart_volume;
}

function check_if_full($candidate) {
  $max_volume = 100;
  $candidate = $product->id;
  $a_volume = get_field('product_volume', $candidate);
  $b_volume = get_cart_volume();
  $check_volume = $a_volume + $b_volume;
    if ($check_volume > $max_volume)
      return true;
   else
      return false;
}

//Just a function to see if it's working on the cart page fur debugging purpose
add_action( 'woocommerce_check_cart_items', 'current_volume');
function current_volume() {
  if (is_cart() || is_checkout()) {
    global $woocommerce;
    $current_volume = get_cart_volume();
    wc_add_notice( sprintf( '<strong>Volume is %s.</strong>',    $current_volume ),
    'error' );
   }
}

【问题讨论】:

  • candidate_volume() 中,$product 对象没有被定义,所以这肯定是一个致命错误。
  • 您好,感谢您的意见。例如,我应该用“global $product”声明它吗?编辑:哦,我明白你的意思了 Candidate_volume($product)...
  • 是的,将$product 作为candidate_volume() 的参数传递:例如candidate_volume($product) 应该可以工作。
  • 感谢您的帮助,对我帮助很大。我成功了!

标签: php wordpress woocommerce cart product


【解决方案1】:

正如 Helgatheviking 所说,您已经通过钩子函数中的 woocommerce_is_purchasable 钩子,将 $product 对象作为第二个参数。因此,您不需要并且必须删除 global $woocommerce, $product; 才能使其正常工作而不会引发错误。

您的代码将是:

add_filter('woocommerce_is_purchasable', 'if_room_is_purchasable', 10, 2);
function if_room_is_purchasable ($is_purchasable, $product){

    $cart_volume = 0;
    $max_volume = 100;

    foreach( WC()->cart->get_cart() as $cart_item ){
        $item_id = $cart_item['product_id'];
        $terms = get_the_terms( $item_id, 'pa_pdt_volume');
        foreach($terms as $key => $term){
            $cart_volume += $term->name; // May be better with $term->slug;
        }
    }

    $candidate_volume = $cart_volume + $product->get_attribute( 'pa_pdt_volume' );

    if ( $candidate_volume > $max_volume ) 
        return false; 
    else 
        return true;
  }

现在应该可以正常工作了。

【讨论】:

  • 您好 Loic,首先,非常感谢您的代码。我一直在研究一个运行良好的 sn-p,或者看起来是这样。我改用 ACF get_field,因为看起来 $term->name 不起作用。不过我也会测试你的代码,看看哪个在我打算使用函数提供的信息的不同地方有更好的性能和灵活性。
猜你喜欢
  • 1970-01-01
  • 2020-04-26
  • 2019-08-16
  • 1970-01-01
  • 1970-01-01
  • 2014-11-15
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
相关资源
最近更新 更多