wordpress 特定类别的变更状态指令

ar5n3qh5  于 2023-01-08  发布在  WordPress
关注(0)|答案(2)|浏览(127)

我想更改状态后,支付所有产品的类别私人订单
我试过这个代码,但它适用于所有产品,我需要它与产品下的类别私人只
'add_action(' woocommerce订单处理状态','待完成处理');
函数处理完成($order_id){

$order = new WC_Order($order_id);
$order->update_status('refunded');

}`
有人能帮我吗?
add_action('woocommerce订单处理状态','待完成处理');
函数处理完成($order_id){

$order = new WC_Order($order_id);
$order->update_status('refunded');

}

fnx2tebb

fnx2tebb1#

您必须检查您的订单中的项目,如果任何项目是从特定类别更改订单状态。

add_action( 'woocommerce_order_status_processing', 'processing_to_completed');
    function processing_to_completed($order_id){
        $order = new WC_Order($order_id);
        foreach ( $order->get_items() as $item_id => $item ) {
            $product_id = $item['product_id'];
            // check if any of the products is in private category
            if( has_term( array( 'private' ), 'product_cat', $product_id )) {
                $order->update_status( 'refunded' ); // your status here
                break;
            }
        }
    }
h7appiyu

h7appiyu2#

add_action( 'woocommerce_thankyou', 'processing_to_completed');
    
    function processing_to_completed($order_id){
      if ( ! $order_id ){
        return;
      }
          
      $order = new WC_Order( $order_id );
        $items = $order->get_items(); 
        foreach ( $items as $item ) {      
          $product_id = $item->get_product_id();  
          if(has_term( 'under-category-private', 'product_cat', $product_id ) ) { //enter in your cat i.e           under-category-private 
            $order->update_status( 'under-completed' ); // your own status here
            break;
          }
          else{
            $order->update_status( 'completed' ); // your other status here part from private category not in order.
            break;
          }  
        }
    }

相关问题