我想在csv文件中写入产品库存价值可能发生的任何变化。这包括订单的库存减少。
我已经编写了以下代码,当在管理页面中手动更改库存时,或者当订单被标记为取消或终止时,这些代码可以完美地工作,但当下了新订单时,它不起作用。
为什么是这样?我如何更改代码,以便在下订单时触发代码?
代码为:
add_action( 'woocommerce_product_set_stock', 'save_stock',1 ); function save_stock( $product ) { $sku = $product->get_sku(); $qty = $product->get_stock_quantity(); $file_pathname = '~/stock_v1_log.csv'; $file = fopen($file_pathname, 'a'); $data = array("WEB", $sku, $qty); fputcsv($file, $data); fclose($file); } add_action( 'woocommerce_reduce_order_stock', 'update_in_custom_table', 10, 1 ); add_action( 'woocommerce_restore_order_stock', 'update_in_custom_table', 10, 1 ); add_action( 'woocommerce_checkout_order_processed', 'update_in_custom_table', 10, 1 ); function update_in_custom_table( $order ){ $order_id = $order->get_id(); $order = wc_get_order( $order_id ); foreach ( $order->get_items() as $item ) { if ( ! $item->is_type( 'line_item' ) ) { continue; } $product = $item->get_product(); $sku = $product->get_sku(); $qty = $product->get_stock_quantity(); $file_pathname = '~/stock_v1_log.csv'; $file = fopen($file_pathname, 'a'); $data = array("WEB", $sku, $qty); fputcsv($file, $data); fclose($file); } }