How to delete products from WooCommerce store after a time period? – Best of 2023

Delete products from WooCommerce store

Have you desired to delete products from WooCommerce store after a specific time period? If yes! Then how can you delete products from WooCommerce store? To know this, come along with us here we are performing the same. before diving in let us first discuss about WooCommerce store. As WooCommerce is widely popular e-commerce platform is home to nearly 400,000 sites.

WooComerce setup lets you create an e-commerce business (store), manage inventory, accept payments, and much more. It comes with almost all the functionalities that you require to open an online store. And through its extension system, it also posses many advanced capabilities. It helps you sell products or services online from your WordPress site at a very affordable and accessible manner. 

However, WooCommerce users often require a functionality to use the products that can be made available on the shop page for a specific time period. Thus, after the specific time period has passed by there should be a functionality to auto delete product.

For quick WooCommerce Setup feel free to contact us. Dial our Toll-Free number +1-855-945-3219. We will be pleased to help you.

Let’s get started on how to delete products from WooCommerce store?

When will you need to delete products from WooCommerce store?

This kind of requirement may be generated for the subscription-based multi-vendor store. Where a WooCommerce multi vendor store is a platform where you can sell and buy varieties of products. Depending upon the user perspective, you can sell your products as a vendor and buy products as a buyer.

Therefore, for example, once you have a multi-seller WooCommerce website, vendors can sell their products from your website by subscribing to various packages which may be available on your site. By using the subscription method, the products sold by the respective vendors consist in certain limits with the availability of the packages provided by the site owner.

Hence, what happens after the end of particular seller’s subscription? The seller should no longer and to be able to sell any product from your website. For this purpose, it will be necessary to either remove products from the shop page or save it as a draft to be accessed later from the dashboard. As after the subscription the product automatically gets removed from the website. 

delete-product-after-time
delete products from WooCommerce store

Steps to Automatically Delete Products from WooCommerce Store

So, the question is still remain how will we remove the products from the shop page after a specific time interval. As to delete products from WooCommerce store. In this process, the following three steps will be involved as shown below:

1. Set Time After which Product should be Deleted

  • The most important step is to decide after a time period. After that, the products should be removed from the system. However, this step will apply when adding a product to the store page.
  • For this, the code will be written in a function called ‘wp_insert_code’ on the hook. Thus, to determine the time the code will be shown below:
function set_delete_product_meta($id) {
global $wpdb,$current_user;
$posttype = get_post_type($id);
if ( $posttype ==’revision’ )
return;
if($posttype==’product’)
{
$val=get_post_meta( $id, ‘wdm_set_expiry’,true);
if($val!=1)
{
$month = date(‘m’);
$day = date(‘d’)+X;
$year = date(‘Y’);
$hour = date(‘H’);
$minute =date(‘i’);
$dt = get_gmt_from_date(“$year-$month-$day $hour:$minute:0”,’U’);
$vars = $id;
wdm_delete_product($dt,$id,$vars);
update_post_meta($id,’wdm_set_expiry’,1);
}
}
else
return;
}add_action(‘wp_insert_post’,’set_delete_product_meta’);
  • The variables ‘X’ at line 12 you have to replace with the real duration. After that, you want to remove the product from the shop page.

2. Schedule a Cron Job for Product Deletion

(Cron Job: A cron job is a Linux command, for scheduling a task for executed sometime later in the future. It can usually use for scheduling a job that can execute periodically. For example, to send information every morning.) To delete products from WooCommerce store

  • However, we have to remove product page from the shop page after the specific time. Also, it is set to the next step. That will scheduling a Cron Job to actually start the extinction process after the set time.
  • In this step, this first checks that cron job has already set for the product. And if it has done. After that, there are no changes made to the schedule time of current cron job.
  • However, in the above code, ‘wdm_delete_product’ has made to the function on line 18.
  • Hence, this is such a function that will actually schedule cron jobs. And we need the below code to achieve this functionality.
function wdm_delete_product($remove_date,$id,$vars) {
if (wp_next_scheduled(‘wdm_remove_product’,array($id)) !== false)
wp_clear_scheduled_hook(‘wdm_remove_product’,array($id));
wp_schedule_single_event($ts,’wdm_remove_product’,array($id));
// Update Post Meta
update_post_meta($id, ‘wdm_product_remove_date’, $remove_date);
update_post_meta($id, ‘wdm_product_remove_date-value’, $vars);
}

Make sure that Cron Job runs as expected

  • As you already know, wp-cron.php triggers cron is on your WordPress website. However, this file does not always execute as expected. However, it only executes when someone visits your site. And this can be a problem.
  • Thus, due to the inability of WP Cron. It cannot execute according to your wish that you have set to delete a product.
  • Therefore, to ensure that the cron is executing and after that, you can use the service like EasyCron.
  • Also, EasyCron service ensures that wp-cron.php executes on a timely basis. Consequently, make sure that we can call this function by ‘wdm_delete_product’ function. After that, we can remove the product.

3. Delete Products after Set Time has Passed By

  • Now, here when the deletion process has started to delete products from WooCommerce store. We will need to add a function that will remove from the shop page after the deletion process begins.
  • However, as you can see in the above code, a function is ‘wdm_remove_product’. This function will remove the products from the shop page. And will be call on the ‘wdm_remove_product’ hook which is define in the ‘wdm_delete_product’ function in step two.
function wdm_remove_product($id){
global $wpdb;
if (empty($id)){
return false;
}
if (is_null(get_post($id))){
return false;
}
$pr_value = get_post_meta($id,’wdm_product_remove_date-value’,true);
// wp_delete_post($pr_value) ; //This will delete product
$wpdb->query(“Update $wpdb->posts set post_status=’draft’ where ID=’$id'”);// Change status to draft
}
add_action(‘wdm_remove_product’,’wdm_remove_product’);
  • Hence, all the above code is necessary to add to the functions of your theme. Alternatively, you can create a separate plugin folder and add this code to a php file in this folder.
Delete products from WooCommerce store

CONCLUSION

I suggest you, use the second approach to delete products from WooCommerce store. Because any customizations done in the theme files will be lost in case of updating the theme. WPglobalSupport provides excellent support and service for any customizations. And with it, you can successfully implement the functionality to automatically delete products from your WooCommerce store.

And this leads to the end of the blog!

We hope you found this blog is helpful to you. You can use the comments section for any problem that you may have.

If you want fast and best support then talk to our WordPress Customer Service Team, dial +1-855-945-3219(Toll-Free).



Leave a Reply