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 *

top 5g male performance enhancer reviews expert insights user experiences obtain a prescription for erectile dysfunction treatment discover the benefits of bio heal cbd gummies at cvs unlock a healthier you tonight experience the bliss of nextevo cbd gummies unlock relaxation natural wellness the ultimate pain relief solution full spectrum cbd gummies for natural holistic pain management lifeheal cbd gummies a physician s guide to unlocking wellness benefits discover calm sunmed cbd gummies for anxiety relief unlock the bliss of cbd gummies your guide to finding the perfect product for optimal wellness unlock the bliss of cbd gummies in las vegas your wellness journey how to get your doctor to prescribe weight loss medication quick weight loss pills australia natural effective weight loss solutions for fast results revolutionizing weight loss the ultimate guide to transform your body with science backed pills revolutionary keto gummies the breakthrough formula for effective weight loss revolutionary fast acting weight loss pills achieve natural results in just weeks with our science backed diet supplement guarantee mushroom supplements for weight loss exploring their potential benefits and scientific evidence top natural pills for quick weight loss fast safe and effective transform your keto journey with keto one gummies amazon ultimate guide turmeric curcumin and blood sugar evidence based insights on potential benefits balanced low carb nutrition for hypoglycemia management advanced blood sugar regulation pill