【发布时间】:2020-01-16 05:11:48
【问题描述】:
我目前正在处理 WooCommerce 订单的自动状态更改:当产品库存达到 50% 时,我正在尝试将所有相关的“处理”订单更改为“kundenupdate”订单状态。
我正在使用this answer thread中的函数retrieve_orders_ids_from_a_product_id(),查询状态为“处理中”的订单,在下面的代码中形成一个产品ID:
function retrieve_orders_ids_from_a_product_id( $product_id ) {
global $wpdb;
// Define HERE the orders status for queried orders
$orders_statuses = "'processing'";
# Requesting All defined statuses Orders IDs for a defined product ID
return $wpdb->get_col( "
SELECT DISTINCT woi.order_id
FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim,
{$wpdb->prefix}woocommerce_order_items as woi,
{$wpdb->prefix}posts as p
WHERE woi.order_item_id = woim.order_item_id
AND woi.order_id = p.ID
AND p.post_status IN ( $orders_statuses )
AND woim.meta_key IN ( '_product_id', '_variation_id' )
AND woim.meta_value LIKE '$product_id'
ORDER BY woi.order_item_id DESC"
);
}
/**
* Automated order status changes
*/
add_action('woocommerce_order_status_changed', 'woocommerce_auto_processing_orders');
function woocommerce_auto_processing_orders( $order_id ) {
if ( ! $order_id )
return;
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
//product_id gets tracked automatically
$product = wc_get_product( $item['product_id'] );
$product_id = $item->get_product_id();
$orders_ids_array = retrieve_orders_ids_from_a_product_id( $product_id );
$stock = $product->get_stock_quantity();
//If order is "processing".
foreach ( $orders_ids_array as $order_id_array ) {
if( $order_id_array->has_status( 'processing' ) ) {
if( $stock == 350 ) {
$order_id_array->update_status( 'kundenupdate' );
} elseif( $stock == 140 ) {
$order_id_array->update_status('on-hold');
}
}
}
}
}
如果我只更改一个订单的代码,它会起作用。但是,如果我尝试获取特定 product_id 的所有订单,这些订单都处于“处理”状态,则它不起作用。
我也不知道retrieve_orders_ids_from_a_product_id()函数是否正常工作,因为我不知道如何在php中调试它。
我如何获得所有处于“处理中”状态的特定 product_id 的所有订单的正确数组?
【问题讨论】:
-
您从 this answer thread 获取的函数
retrieve_orders_ids_from_a_product_id()已经过测试,可以完美地从产品 ID 获取所有订单……所以问题出在您的代码中。 -
你能解释一下你的意思吗?如果我只更改一个订单的代码,它可以工作。如果您尝试硬编码 product_id 会起作用吗?
标签: php wordpress woocommerce hook-woocommerce orders