douwayuan3063 2018-02-27 15:52
浏览 61

Auto-Restock Woocommerce取消了超时订单的订单

So @LoicTheAztec helped me out with some code to get auto-restock working for canceled orders. What I have found is that the code works only when the Admin cancels an order in the panel.

When Payment isn't fulfilled or if the customer cancels his order stock isn't replenished. Have no idea how I would make this work...

Here's the code we are using:

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();
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度