根据批准状态和特定订单项更改WooCommerce订单状态 [英] Change WooCommerce order status based on approved status and specific order item

查看:82
本文介绍了根据批准状态和特定订单项更改WooCommerce订单状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当当前状态为已批准"并且订单包含特定产品(id = 10)时,我尝试将WooCommerce订单状态更改为正在处理".

I try to change a WooCommerce order status to "Processing" when the current status is "Approved" and the order includes a specific product ( id = 10).

我尝试了以下代码,但无法正常工作.我是php的新手,不胜感激!

I have attempted the code below but it is not working. I am quite new to php and would appreciate any guidance!

add_action('woocommerce_order_status_changed', 'ts_auto_complete_business_cards');

function ts_auto_complete_business_cards($order_id)
{

    if ( ! $order_id ) {
        return;
    }

    global $product;
    $order = wc_get_order( $order_id );

    if ($order->data['status'] == 'approved') {
        $items=$order->get_items();
        foreach ( $items as $item ) {
            $product_id = $item->get_product_id();
            if ($product_id!="10")
            {
                $order->update_status( 'completed' );
            }

        }

    }
}

推荐答案

  • woocommerce_order_status_changed 具有4个参数
  • 这行-> 如果($ product_id!="10")表示不等于,则还与字符串而不是与数值进行比较
    • woocommerce_order_status_changed has 4 parameters
    • This line -> if ($product_id!="10") says NOT equal, you also compare with a string and not with a numerical value
    • 尝试一下

      function action_woocommerce_order_status_changed( $order_id, $old_status, $new_status, $order ) {
      
          // Compare
          if( $old_status === 'approved' ) {
              // Get items
              $items = $order->get_items();
      
              foreach ( $items as $item ) {
                  // Get product id
                  $product_id = $item->get_product_id();
      
                  if ($product_id == 10 ) {
                      $order->update_status( 'processing' );
                      break;
                  }
              }
          }
      }
      add_action( 'woocommerce_order_status_changed', 'action_woocommerce_order_status_changed', 10, 4 );
      

      这篇关于根据批准状态和特定订单项更改WooCommerce订单状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆