I need help with figuring out how to trash multiple product at once. I'm trying to trash all products in a category is its order is successfully created. I.e when i buy 2 or more products from machine category and purchase 1 from shirt category. Then place the order, on success, we get the thank you page. At this point i want all products from the machine category to be trashed immediately while leaving the product from the shirt category untrashed.
Currently i have the below code which works but only trash one out of two from machine category;
function delete_product_on_woocommerce_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
// 1. Get order object
$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['product_id'];
if ( has_term( 'machine', 'product_cat', $product_id ) ) { //Where machine is the custom product category slug
$cat_in_order = true;
break;
}
}
// TRASH if $cat_in_order == true
if ( $cat_in_order ) {
wp_trash_post($product_id);
}
}
add_action( 'woocommerce_thankyou', 'delete_product_on_woocommerce_complete_order', 5 );
but i would like to trash all the products from machine category when its order has been successfully created. Thanks for your help
I need help with figuring out how to trash multiple product at once. I'm trying to trash all products in a category is its order is successfully created. I.e when i buy 2 or more products from machine category and purchase 1 from shirt category. Then place the order, on success, we get the thank you page. At this point i want all products from the machine category to be trashed immediately while leaving the product from the shirt category untrashed.
Currently i have the below code which works but only trash one out of two from machine category;
function delete_product_on_woocommerce_complete_order( $order_id ) {
if ( ! $order_id ) {
return;
}
// 1. Get order object
$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['product_id'];
if ( has_term( 'machine', 'product_cat', $product_id ) ) { //Where machine is the custom product category slug
$cat_in_order = true;
break;
}
}
// TRASH if $cat_in_order == true
if ( $cat_in_order ) {
wp_trash_post($product_id);
}
}
add_action( 'woocommerce_thankyou', 'delete_product_on_woocommerce_complete_order', 5 );
but i would like to trash all the products from machine category when its order has been successfully created. Thanks for your help
Maybe you can just move the trash function to be inside the foreach loop.
foreach ( $items as $item ) {
$product_id = $item['product_id'];
// matching products are trashed
if ( has_term( 'machine', 'product_cat', $product_id ) ) {
wp_trash_post($product_id);
}
}