skip to Main Content

i tryed to make a code to verify the category of the product in a order when it is payed, i have tested but it dident work, i tested with credit card, but the status went to completed automatcly.

I want the order go to status "completed" when in the order have a product with the category "pack" and dont have any other diferent category, if have any other product with a different category, i want the order to go to the status "aguardando-envio".

could you help me with this?

add_action( 'woocommerce_payment_complete', function ( $order_id ) {
    
    if( ! $order_id ) return;
    
    $order = wc_get_order( $order_id );
  
    // 2. Initialize $cat_in_order variable
    $cat_in_order = false;
  
    // 3. Get order items and loop through them...
    // ... if product in category, edit $cat_in_order
    $items = $order->get_items(); 
      
    foreach ( $items as $item ) {      
       $product_id = $item->get_product_id();  
       if ( has_term( 'videos-personalizaveis', $product_id ) ) {
          $cat_in_order = true;
          break;
       }
       if ( has_term( 'fotos-personalizaveis', $product_id ) ) {
          $cat_in_order = true;
          break;
       }
       if ( has_term( 'audios-personalizaveis', $product_id ) ) {
          $cat_in_order = true;
          break;
       }
       if ( has_term( 'produtos-pessoais', $product_id ) ) {
          $cat_in_order = true;
          break;
       }
    }
   
    if ( $cat_in_order ) {
        $order->update_status( 'aguardando-envio' );
    }
    
    if ( $cat_in_order == false ) {
        $order->update_status( 'completed' );
    }
}, 10, 3 );

2

Answers


  1. You have a mistake in checking whether the product is in the desired category or not. On the other hand, the order status may not be registered properly. Here you will find everything you need:

    /**
     * Register new status
    */
    add_action( 'init', 'ywp_register_aguardando_envio_shipment_order_status' );
    function ywp_register_aguardando_envio_shipment_order_status() {
        register_post_status( 'wc-aguardando-envio', array(
            'label'                     => 'Aguardando Envio',
            'public'                    => true,
            'exclude_from_search'       => false,
            'show_in_admin_all_list'    => true,
            'show_in_admin_status_list' => true,
            'label_count'               => _n_noop( 'Aguardando Envio (%s)', 'Aguardando Envio (%s)' )
        ) );
    }
    
    /**
     * Add to list of WC Order statuses
    */
    add_filter( 'wc_order_statuses', 'ywp_add_aguardando_envio_shipment_to_order_statuses' );
    function ywp_add_aguardando_envio_shipment_to_order_statuses( $order_statuses ) {
     
        $new_order_statuses = array();
     
        // add new order status after processing
        foreach ( $order_statuses as $key => $status ) {
     
            $new_order_statuses[ $key ] = $status;
     
            if ( 'wc-processing' === $key ) {
                $new_order_statuses['wc-aguardando-envio'] = 'Aguardando Envio';
            }
        }
     
        return $new_order_statuses;
    }
    
    /**
     * Change order status
    */
    add_action( 'woocommerce_thankyou', 'ywp_change_order_status_to_aguardando_envio', 99 );
    function ywp_change_order_status_to_aguardando_envio( $order_id ) {
        if( ! $order_id ) return;
    
        $order = wc_get_order( $order_id );
    
        // 2. Initialize $cat_in_order variable
        $cat_in_order = false;
    
        // 3. Get order items and loop through them...
        // ... if product in category, edit $cat_in_order
        $items = $order->get_items(); 
    
        foreach( $items as $item ) {      
            $product_id = $item->get_product_id();  
    
            if( has_term( array( 'videos-personalizaveis', 'fotos-personalizaveis', 'audios-personalizaveis', 'produtos-pessoais' ), 'product_cat', $product_id ) ) {
                $cat_in_order = true;
                break;
            }
        }
       
        if ( $cat_in_order ) {
            $order->update_status( 'aguardando-envio' );
        }
    }
    

    The code goes in the functions.php file of active theme/child theme. Tested and works.

    Login or Signup to reply.
  2. Try the following instead, that will autocomplete order status for an exclusive product category "pack", other wise it will set the order status to ‘aguardando-envio’:

    add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
    function wc_auto_complete_paid_order( $status, $order_id, $order ) {
        
        $exclusive_category = array("pack"); // <== Here set your exclusive category
        $others_found       = false; // Initializing
        
        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            if( ! has_term( $exclusive_category, 'product_cat', $item->get_product_id() ) ) {
                $others_found = true;
                break;
            }
        }
    
        return $others_found ? 'aguardando-envio' : 'completed';
    }
    

    Code goes in functions.php file of the active child theme (or active theme). It should works.

    Notes:

    • The custom status ‘aguardando-envio’ need to be created before for WooCommerce orders, via some custom code or a plugin.
    • This will not work for "bacs", "cod" or "cheque" payment method Ids, where payment need to be confirmed by shop manager by changing the oder status via admin.

    Related: WooCommerce: Auto complete paid orders

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search