$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>php - How to trash multiple postproduct programmatically|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

php - How to trash multiple postproduct programmatically

matteradmin10PV0评论

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);
        }
    }
Post a comment

comment list (0)

  1. No comments so far