我拿起了这段代码片段并且一直在搞乱它。我收到了一些语法错误......
以下是代码:
case
我在第14行和第22行遇到了一些错误......无法解决这个问题。有什么想法吗?
如何在woocommerce 3中的特定订单状态更改中获得此自动补货功能?
答案 0 :(得分:3)
很抱歉,您使用的代码已经完全过时并且充满了错误。
下面,基于this working similar code,此课程将根据您定义的订单状态更改重新定位产品:
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+)。