根据 Woocommerce 中的特定订单状态更改自动补货产品 [英] Auto restock products on specific order status changes in Woocommerce

查看:79
本文介绍了根据 Woocommerce 中的特定订单状态更改自动补货产品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拿起了这个代码片段,并一直在弄乱它.我遇到了一些语法错误...

I picked up this code snippet and have been messing with it. I'm getting some syntax errors…

这是代码:

class Comfythemes_Woocommerce_Auto_Stock_Restore {

    function __construct() {
        add_action( 'woocommerce_order_status_processing_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
        add_action( 'woocommerce_order_status_completed_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
        add_action( 'woocommerce_order_status_on-hold_to_cancelled', array( $this, 'restore_order_stock' ), 10, 1 );
        add_action( 'woocommerce_order_status_processing_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 );
        add_action( 'woocommerce_order_status_completed_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 );
        add_action( 'woocommerce_order_status_on-hold_to_refunded', array( $this, 'restore_order_stock' ), 10, 1 );
    }
    public function restore_order_stock( $order_id ) {
        $order = new WC_Order( $order_id );

        if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! sizeof( $order->get_items() ) > 0 ) {
            return;
        }
        foreach ( $order->get_items() as $item ) {
            if ( $item['product_id'] > 0 ) {

                $_product = $order->get_product_from_item( $item );

                if ( $_product && $_product->exists() && $_product->managing_stock() ) {

                    $old_stock = $_product->stock;
                    $qty = apply_filters( 'woocommerce_order_item_quantity', $item['qty'], $this, $item );
                    $new_quantity = $_product->increase_stock( $qty );
                    do_action( 'woocommerce_auto_stock_restored', $_product, $item );
                    $order->add_order_note( sprintf( __( 'Item #%s stock incremented from %s to %s.', 'woocommerce' ), $item['product_id'], $old_stock, $new_quantity) );
                    $order->send_stock_notifications( $_product, $new_quantity, $item['qty'] );
                }
            }
        }
    }
}

new Comfythemes_Woocommerce_Auto_Stock_Restore();

我在第 14 行和第 22 行遇到一些错误...无法弄清楚.有什么想法吗?

Im getting some errors on line 14 and 22... couldn't figure this out. Any ideas?

如何在 woocommerce 3 中的特定订单状态更改时获得此自动补货功能?

How to get this Auto restock feature on specific orders status changes in woocommerce 3?

推荐答案

抱歉,您使用的代码完全过时且错误百出.

Sorry, but the code you are using is just completely outdated and full of errors.

以下,基于this使用类似的代码,此类将根据您定义的订单状态更改重新库存产品:

Below, based on this working similar code, this class will re-stock Products based on your defined orders status changes:

if ( ! class_exists( 'WC_Auto_Stock_Restore' ) ) {
    class WC_Auto_Stock_Restore {

        function __construct() {
            add_action( 'woocommerce_order_status_processing_to_cancelled', array( $this, 'restore_order_stock' ), 10, 2 );
            add_action( 'woocommerce_order_status_completed_to_cancelled', array( $this, 'restore_order_stock' ), 10, 2 );
            add_action( 'woocommerce_order_status_on-hold_to_cancelled', array( $this, 'restore_order_stock' ), 10, 2 );
            add_action( 'woocommerce_order_status_processing_to_refunded', array( $this, 'restore_order_stock' ), 10, 2 );
            add_action( 'woocommerce_order_status_completed_to_refunded', array( $this, 'restore_order_stock' ), 10, 2 );
            add_action( 'woocommerce_order_status_on-hold_to_refunded', array( $this, 'restore_order_stock' ), 10, 2 );
        }

        public function restore_order_stock( $order_id, $order ) {
            $items = $order->get_items();

            if ( ! get_option('woocommerce_manage_stock') == 'yes' && ! count( $items ) > 0 )
                    return; // We exit

            foreach ( $order->get_items() as $item ) {
                $product_id = $item->get_product_id();

                if ( $product_id > 0 ) {
                    $product = $item->get_product();

                    if ( $product && $product->exists() && $product->managing_stock() ) {

                        // Get the product initial stock quantity (before update)
                        $initial_stock = $product->get_stock_quantity();

                        $item_qty = apply_filters( 'woocommerce_order_item_quantity', $item->get_quantity(), $this, $item );

                        // Update the product stock quantity
                        // Replace DEPRECATED methods: increase_stock() & discrease_stock()
                        wc_update_product_stock( $product, $item_qty, 'increase' );

                        // Get the product updated stock quantity
                        $updated_stock = $initial_stock + $item_qty;

                        do_action( 'woocommerce_auto_stock_restored', $product, $item );

                        // A unique Order note: Store each order note in an array…
                        $order_note[] = sprintf( __( 'Product ID #%s stock incremented from %s to %s.', 'woocommerce' ), $product_id, $initial_stock, $updated_stock);

                        // DEPRECATED & NO LONGER NEEDED - can be removed
                        //$order->send_stock_notifications( $product, $updated_stock, $item_qty );

                    }
                }
            }
            // Adding a unique composite order note (for multiple items)
            $order_notes = count($order_note) > 1 ? implode(' | ', $order_note) : $order_note[0];
            $order->add_order_note( $order_notes );
        }
    }
    $GLOBALS['wc_auto_stock_restore'] = new WC_Auto_Stock_Restore();
}

代码位于活动子主题(或主题)的 function.php 文件中.

经过测试并有效(仅适用于 WooCommerce 3+).

这篇关于根据 Woocommerce 中的特定订单状态更改自动补货产品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆