php - How to trash multiple postproduct programmatically

admin2025-06-02  8

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

Share asked Feb 23, 2019 at 16:39 Kolawole Emmanuel IzzyKolawole Emmanuel Izzy 2031 silver badge8 bronze badges
 | 

1 Answer 1

Reset to default 4

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);
        }
    }
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748873903a314433.html

最新回复(0)