【问题标题】:Prevent WooCommerce from decrementing stock on payment防止 WooCommerce 在付款时减少库存
【发布时间】:2013-12-29 15:52:46
【问题描述】:

我正在开发一个通过 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() 包装在一个在初始化时触发的函数中,但仍然没有运气。

非常感谢任何帮助。

【问题讨论】:

    标签: wordpress woocommerce


    【解决方案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 挂钩。

    【讨论】:

      【解决方案2】:
      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 ); 
      

      这帮助我解决了问题!

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 你能解释一下吗?当然,我需要在该钩子上添加一个过滤器以返回 false 并阻止 reduce_order_stock() 函数运行?我尝试更改以删除过滤器,但它不起作用,付款后库存仍然减少。
        • 您不会删除过滤器,您需要返回一个值false
        猜你喜欢
        • 2023-03-09
        • 1970-01-01
        • 2016-07-14
        • 2012-07-30
        • 2017-12-12
        • 2015-11-05
        • 2017-05-31
        • 2017-11-27
        • 2015-10-07
        相关资源
        最近更新 更多