【问题标题】:Auto change order status to completed for specific products in WooCommerce在 WooCommerce 中为特定产品自动更改订单状态以完成
【发布时间】:2021-01-02 21:28:07
【问题描述】:

我有一个虚拟产品,我希望它在付款完成后更改状态。 以下代码将所有购买更改为“已完成”,但我只想更改我的一种产品,而不是全部。 我的产品是可变产品,有 4 个项目,

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
    function custom_woocommerce_auto_complete_order( $order_id ) { 
        if ( ! $order_id ) {
            return;
        }

        $order = wc_get_order( $order_id );

        if( $order->has_status( 'processing' ) ) {
            $order->update_status( 'completed' );
        }
    }

我搜索了很多,但没有找到答案。请帮我。谢谢你

【问题讨论】:

标签: php wordpress woocommerce hook-woocommerce orders


【解决方案1】:

您可以从订单项目中定位特定的产品 ID,以使您的代码仅适用于它们,如下所示:

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    
    $product_ids   = array('23'); // Here set your targeted product(s) Id(s)
    $product_found = false;
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if( array_intersect( $product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
            $product_found = true;
            break;
        }
    }

    if( $order->has_status( 'processing' ) && $product_found ) {
        $order->update_status( 'completed' );
    }
}

如果您想专门定位这些产品,那么您将改用以下内容:

add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) { 
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    
    $product_ids   = array('23'); // Here set your targeted product(s) Id(s)
    $product_found = true;
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if( ! array_intersect( $product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
            $product_found = false;
            break;
        }
    }

    if( $order->has_status( 'processing' ) && $product_found ) {
        $order->update_status( 'completed' );
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。


补充:避免多次通知

您最好使用woocommerce_payment_complete_order_status 钩子,而不是像在WooCommerce: Auto complete paid orders 答案中那样,以避免多个通知。

所以代码将是:

add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
    $product_ids   = array('23'); // Here set your targeted product(s) Id(s)
    $product_found = false;
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if( array_intersect( $product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
            $product_found = true;
            break;
        }
    }

    return $product_found ? 'completed' : $status;
}

专门定位产品:

add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
    $product_ids   = array('23'); // Here set your targeted product(s) Id(s)
    $product_found = true;
    
    // Loop through order items
    foreach ( $order->get_items() as $item ) {
        if( ! array_intersect( $product_ids, array($item->get_product_id(), $item->get_variation_id()) ) ) {
            $product_found = false;
            break;
        }
    }

    return $product_found ? 'completed' : $status;
}

它应该可以工作......

【讨论】:

  • 另外,我只需要为我上面提到的产品发送电子邮件通知。因为“处理中”和“已完成”通知是同时发送的。 (我只想将这些设置应用于一个产品,而不是所有产品)我在您建议的文章中没有找到答案,感谢您的关注。
  • @matin 我用woocommerce_payment_complete_order_status钩子添加了一些代码……
  • 我同时使用了这两个代码并且效果很好。感谢朋友的快速回复
猜你喜欢
  • 2018-08-01
  • 2021-05-23
  • 2016-04-17
  • 2018-10-14
  • 2019-06-01
  • 2017-01-05
  • 2019-01-19
  • 2019-10-09
  • 1970-01-01
相关资源
最近更新 更多