woocommerce订单退款时自动恢复库存数量挂钩 [英] woocommerce auto restore stock quanitiy hook when order refund

查看:64
本文介绍了woocommerce订单退款时自动恢复库存数量挂钩的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要woocommerce在订单退款时自动恢复库存数量挂钩请对此有任何帮助我尝试了2个钩子

i need woocommerce auto restore stock quanitiy hook when order refund please any one help regarding this I tried with 2 hooks

 add_action( 'woocommerce_product_set_stock','action_woocommerce_variation_set_stock', 10, 1 );
add_action( 'woocommerce_variation_set_stock', 'action_woocommerce_variation_set_stock', 10, 1 );

但是当产品库存量按订单减少时,这些挂钩在前端起作用

but these hook work on frontend when product stock quantity decrease on order

请解决此问题

推荐答案

这是我的代码:

/**
 * WooCommerce - do a custom action on order status change
 */
function jasom_custom_actions_on_order_status_change( $order_id, $checkout = null ) {

    $order = new WC_Order( $order_id );

    if (!get_option( 'woocommerce_manage_stock' ) == 'yes' && !sizeof( $order->get_items() ) > 0) {
        return;
    }

    if ($order->get_status() === 'refunded') {

        $changes = [];

        foreach ($order->get_items() as $item) {

            if ($item[ 'product_id' ] > 0) {

                $product = $item->get_product();

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

                    $qty = $item->get_quantity(); // Get the item quantity

                    $new_quantity = $product->increase_stock( $qty );

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

                    $changes[] = $product->get_formatted_name() . ' ' . ( $new_quantity - $qty ) . '→' . $new_quantity;

                    $order->send_stock_notifications( $product, $new_quantity, $item[ 'qty' ] );
                }
            }
        }

        if ($changes) {
            $order->add_order_note( __( 'Stock levels increased:', 'woocommerce' ) . ' ' . implode( ', ', $changes ) );
        }
    }

}

add_action( 'woocommerce_order_status_changed', 'jasom_custom_actions_on_order_status_change' );

这篇关于woocommerce订单退款时自动恢复库存数量挂钩的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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