2013-12-11 67 views
0

我正在处理通过WooCommerce处理车辆租赁的插件。默认的WooCommerce行为是在付款完成后立即减少订单中的物品库存。长话短说,我需要防止这种情况发生(我将实施自定义功能,以减少所选出租日期的库存量)。防止WooCommerce在支付时减少股票

在WC_Order类中,我找到了一个名为payment_complete()(class-wc-order.php,第1278行)的函数。

在这个函数如下:

if (apply_filters('woocommerce_payment_complete_reduce_order_stock', true, $this->id)) 
      $this->reduce_order_stock(); // Payment is complete so reduce stock levels 

这样看来,我认为我只是需要包括

add_filter('woocommerce_payment_complete_reduce_order_stock', '_return_false'); 
在我的插件

为了防止股价从减少付款,但不幸的是这不起作用。我也尝试在一个函数中包装我的add_filter(),它在init上触发,但仍然没有运气。

任何帮助非常感谢。

回答

-1

而不是add_filter,您应该使用remove_filter函数。

+0

你能解释一下吗?当然,我需要在该钩子上添加一个过滤器来返回false并阻止reduce_order_stock()函数运行?我试图改变过滤器,但它不起作用,股票仍然减少付款。 – iainc

+0

您不会删除过滤器,您需要返回“false”值。 – helgatheviking

1

这是旧的,但如果你不想WooCommerce管理股票,你可以告诉整个商店不要管理期权中的股票。或者,单独在每个产品上。

研究WC_Abstract_Order类中的reduce_order_stock()方法显示,在这些情况下库存不会减少。

/** 
* Reduce stock levels 
*/ 
public function reduce_order_stock() { 

    if ('yes' == get_option('woocommerce_manage_stock') && sizeof($this->get_items()) > 0) { 

     // Reduce stock levels and do any other actions with products in the cart 
     foreach ($this->get_items() as $item) { 

      if ($item['product_id'] > 0) { 
       $_product = $this->get_product_from_item($item); 

       if ($_product && $_product->exists() && $_product->managing_stock()) { 
        $qty  = apply_filters('woocommerce_order_item_quantity', $item['qty'], $this, $item); 
        $new_stock = $_product->reduce_stock($qty); 

        $this->add_order_note(sprintf(__('Item #%s stock reduced from %s to %s.', 'woocommerce'), $item['product_id'], $new_stock + $qty, $new_stock)); 
        $this->send_stock_notifications($_product, $new_stock, $item['qty']); 
       } 

      } 

     } 

     do_action('woocommerce_reduce_order_stock', $this); 

     $this->add_order_note(__('Order item stock reduced successfully.', 'woocommerce')); 
    } 
} 

但是,OP的观察是正确的,但包括一个错字。该__return_false()功能由2之前下划线代替1,所以正确的代码是:

add_filter('woocommerce_payment_complete_reduce_order_stock', '__return_false'); 

从那里我将您的自定义股票递减函数添加到woocommerce_payment_complete钩。

0
function filter_woocommerce_can_reduce_order_stock($true, $instance) { 
return false; 
}; 
add_filter('woocommerce_can_reduce_order_stock','filter_woocommerce_can_reduce_order_stock', 10, 2); 

这帮我解决了这个问题!