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
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:
The code goes in the
functions.php
file of active theme/child theme. Tested and works.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’:
Code goes in functions.php file of the active child theme (or active theme). It should works.
Notes:
Related: WooCommerce: Auto complete paid orders