skip to Main Content

I want to change status order after payment for all products under category private

I tried this code but it works for all products, I need it works with products under category private only

`add_action( ‘woocommerce_order_status_processing’, ‘processing_to_completed’);

function processing_to_completed($order_id){

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

}`

can any one help me?

add_action( ‘woocommerce_order_status_processing’, ‘processing_to_completed’);

function processing_to_completed($order_id){

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

}

2

Answers


  1. You have to check the items in your order and if any of the items is from specific category to change the order status.

    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;
                }
            }
        }
    
    Login or Signup to reply.
  2.     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;
              }  
            }
        }
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search