This is a simple guide on how to add test products and orders dynamically to your WooCommerce shop. This functionality can be useful to test the limits of your website setup and of your server by allowing you to add 1000s of products and orders automatically.

Please note that although you can set whatever number of products and orders you wish, this code has not been optimised to bypass the PHP time limit. This means that the number of products/orders you can create in one go (before refreshing and running it again) is dependent on your server settings. Try starting with smaller numbers.

This snippet goes into the functions.php file of your child theme.

Resources to learn more about creating a child theme:

https://developer.wordpress.org/themes/advanced-topics/child-themes/
https://docs.woocommerce.com/document/set-up-and-use-a-child-theme/

<?php add_action( 'template_redirect', 'createProducts' ); function createProducts(){ // Set number of products to create $number_of_products = 1000; for ($i=1; $i <= $number_of_products; $i++) { // First we create the product post so we can grab it's ID $post_id = wp_insert_post( array( 'post_title' => 'Product ' . $i, 'post_type' => 'product', 'post_status' => 'publish' ) ); // Then we use the product ID to set all the posts meta wp_set_object_terms( $post_id, 'simple', 'product_type' ); // set product is simple/variable/grouped update_post_meta( $post_id, '_visibility', 'visible' ); update_post_meta( $post_id, '_stock_status', 'instock'); update_post_meta( $post_id, 'total_sales', '0' ); update_post_meta( $post_id, '_downloadable', 'no' ); update_post_meta( $post_id, '_virtual', 'yes' ); update_post_meta( $post_id, '_regular_price', '' ); update_post_meta( $post_id, '_sale_price', '' ); update_post_meta( $post_id, '_purchase_note', '' ); update_post_meta( $post_id, '_featured', 'no' ); update_post_meta( $post_id, '_weight', '11' ); update_post_meta( $post_id, '_length', '11' ); update_post_meta( $post_id, '_width', '11' ); update_post_meta( $post_id, '_height', '11' ); update_post_meta( $post_id, '_sku', 'SKU11' ); update_post_meta( $post_id, '_product_attributes', array() ); update_post_meta( $post_id, '_sale_price_dates_from', '' ); update_post_meta( $post_id, '_sale_price_dates_to', '' ); update_post_meta( $post_id, '_price', '11' ); update_post_meta( $post_id, '_sold_individually', '' ); update_post_meta( $post_id, '_manage_stock', 'yes' ); // activate stock management wc_update_product_stock($post_id, 1000, 'set'); // set 1000 in stock update_post_meta( $post_id, '_backorders', 'no' ); } // Once the products have been created, now can create the orders createOrders(); } function createOrders(){ // Set numbers of orders to create $number_of_orders = 100; for ($i=0; $i < $number_of_orders; $i++) { global $woocommerce; // Get a random number of products, between 1 and 10, for the order $products = get_posts( array( 'post_type' => 'product', 'posts_per_page' => rand(1,10), 'orderby' => 'rand' ) ); // Set order address $address = array( 'first_name' => 'John ' . rand(1,200), 'last_name' => 'Doe', 'company' => 'WPHarvest.com', 'email' => 'john@testing.com', 'phone' => '760-555-1212', 'address_1' => '123 Main st.', 'address_2' => '104', 'city' => 'San Diego', 'state' => 'Ca', 'postcode' => '92121', 'country' => 'US' ); // Now we create the order $order = wc_create_order(); // Add products randomly selected above to the order foreach ($products as $product) { $order->add_product( wc_get_product($product->ID), 1); // This is an existing SIMPLE product } $order->set_address( $address, 'billing' ); $order->calculate_totals(); $order->update_status("Completed", 'Imported order', TRUE); } }
Code language: PHP (php)

Hope you found this guide useful and, as always, if you have any questions or suggestions, please feel free to leave a comment below.

Published by Dragos Micu

WooCommerce

Leave a Reply

Your email address will not be published. Required fields are marked *

discover the power of biocore cbd gummies relief from anxiety stress and chronic pain unlocking the full potential of wellness peak cbd gummies a comprehensive review of the best cbd gu experience the bliss of cbd gummies with no thc a comprehensive guide to unlocking its benefits say goodbye to stress and anxiety with adult gummies cbd a natural solution for a harmonious life understanding the benefits of kangaroo cbd gummies 5000 mg for overall wellness 2672 indulge in the sweet bliss of wyld cannabis infused pear gummies a game changing snack for a chill top cbd gummies 2025 the ultimate guide to finding the best product for your needs achieving restful nights with deep sleep cbd gummies a comprehensive guide 29916 the benefits of cbd gummies for prostate health a comprehensive guide to cbd gummies prosta 32567 unlock the power of cbd gummies for women 39 s health and wellness benefits uses and more experience blissful relaxation with blissful aurora cbd gummies a game changing solution for a heal dr oz gummy cbd the revolutionary wellness solution for a healthier you experience the best of both worlds groupon cbd gummies for a healthy and happy you the best cbd gummies to quit smoking a comprehensive guide the benefits and uses of hemp gummies a comprehensive guide 26977 green acre cbd gummies shark tank discover the revolutionary pain relief solution that 39 s taking the does bioheal cbd work expert reviews user feedback and scientific research say goodbye to chronic pain with cbd gummies without thc a natural and effective solution 43438 discover the relief you 39 ve been searching for cbd gummies dispensary for a healthier you comprehensive analysis of farm alliance cbd gummies reviews benefits ingredients and efficacy fa