【问题标题】:Changing WooCommerce processing orders status based on product stock quantity根据产品库存数量更改 WooCommerce 处理订单状态
【发布时间】: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


【解决方案1】:

您的代码中有一些错误和错误:

  • 函数retrieve_orders_ids_from_a_product_id() 工作正常并提供订单ID 数组,但不是WC_Order 对象数组:例如,在您的代码中,方法has_status() 可以不能用于非 WC_Order 对象。
  • 当前的WC_Order 对象已包含在woocommerce_order_status_changed 挂钩中,因此您的函数中缺少该参数。
  • 钩子woocommerce_order_status_changed包含在WC_Orderstatus_transition()方法源代码中,被update_status()使用,所以你不能用这个钩子使用它,因为它使一种无限循环。
  • 还有其他一些事情……

WooCommerce 订单和减少产品库存数量流程说明:

使用挂在这 4 个挂钩中的 wc_maybe_reduce_stock_levels() 函数减少产品库存状态(请参阅 function source code):
- woocommerce_payment_complete - 有 $order_id 作为参数,
- woocommerce_order_status_on-hold - 有 $order_id 和 $order 作为参数,
- woocommerce_order_status_processing - 有 $order_id 和 $order 作为参数,
- woocommerce_order_status_completed - 有 $order_id 和 $order 作为参数。

当订单减少相关产品库存数量时,会为该订单设置一个特定的元数据,元键为_order_stock_reduced和元值为1,避免多次减少。

在下面的代码中,我们将使用这 4 个钩子。

为了避免使用带有这些钩子的 WC_Order update_status() 方法出现问题(如前所述),我们将改用 wp_update_post() WordPress 函数。

您可以尝试以下方法:

function get_processing_orders_from_a_product_id( $product_id ) {
    global $wpdb;

    # Requesting processing 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 = 'wc-processing'
        AND woim.meta_key IN ( '_product_id', '_variation_id' )
        AND woim.meta_value LIKE '$product_id'
        ORDER BY woi.order_item_id DESC"
    );
}


add_action( 'woocommerce_payment_complete', 'orders_status_change_based_on_product_stock', 10, 2 );
add_action( 'woocommerce_order_status_on-hold', 'orders_status_change_based_on_product_stock', 10, 2 );
add_action( 'woocommerce_order_status_processing', 'orders_status_change_based_on_product_stock', 10, 2 );
add_action( 'woocommerce_order_status_completed', 'orders_status_change_based_on_product_stock', 10, 2 );
function orders_status_change_based_on_product_stock( $order_id, $order = '' ) {
    if( ! $order || ! is_a( $order, 'WC_Order') ) {
        $order = wc_get_order( $order_id ); // Get the WC_Order object if it's empty
    }

    // Loop Through order items
    foreach ( $order->get_items() as $item ) {
        $product   = $item->get_product(); // Get the WC_Product object
        $stock_gty = (int) $product->get_stock_quantity(); // the product stock quantity

        // Getting the new order status from stock quantity thresholds
        if ( $stock_gty === 140 ) {
            $new_status = 'on-hold';
        }
        elseif ( $stock_gty === 350 ) {
            $new_status = 'kundenupdate';
        }

        // Updating related processing orders status if any stock quantity threshold is reached
        if( isset($new_status) && $processing_orders = get_processing_orders_from_a_product_id( $item->get_product_id() ) ) {
            // Loop through all related processing orders (IDs)
            foreach ( $processing_orders as $processing_order_id ) {
                // We don't use update_status() WC_Order method to avoid an problems using those hooks
                wp_update_post( array( 'ID' => $processing_order_id, 'post_status' => $new_status ) );
            }
        }
    }
}

代码在您的活动子主题(或活动主题)的functions.php 文件中。它应该更好地工作。

这可能是个问题:如果产品的库存减少 3(例如订单商品数量)并跳过 350 (或140) 库存数量阈值?你可能不得不认为它不同。

【讨论】:

  • 我认为它有效。我刚刚设置了一个新订单 #1146。前几天收到了4个测试订单,都改变了状态。但是我在我的订单中找不到新订单#1146?我将使用屏幕截图编辑我的帖子。
猜你喜欢
  • 2016-08-04
  • 2018-08-01
  • 1970-01-01
  • 2021-04-08
  • 2022-10-05
  • 1970-01-01
  • 1970-01-01
  • 2017-05-24
  • 2019-04-24
相关资源
最近更新 更多