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 *

wellness montoforum expert insights on 270ftype diabetes medications and treatment strategies article glucosamine and blood sugar regulation a 6631type comprehensive guide the importance of food to eat to lower blood sugar a comprehensive guide 67 movie otc medications and ad56d8 blood sugar levels a guide for diabetes patients finding the best supplements to lower blood sugar a comprehensive guide 2003 which statins do not raise blood sugar a comprehensive guide 15025 guides the hormonal dance insulin and glucagon fb8737 in blood sugar regulation reviews is applesauce a suitable addition to a bbe1 diabetes diet what to eat when you have low blood sugar a comprehensive guide 27995 questions af3076f4 cold medicines for diabetes patients safe options for symptom relief collections can garlic tablets help lower blood sugar exploring the science ef1eglucose and evidence what to eat in low blood sugar a comprehensive guide to managing hypoglycemia support can you discontinue fb98 diabetes medications is delsym adult cough relief likely to raise blood sugar levels a comprehensive review 67874 support postprandial blood sugar level of 170 mg dl understanding and 987747 managing elevated glucose after meals foods to lower your blood sugar a comprehensive guide to managing diabetes 2446 media understanding blood 8080type glucose levels in diabetes management will cashews raise blood sugar a comprehensive guide to nut consumption for diabetics recognizing the symptoms of low blood sugar vs high a comprehensive guide to managing blood 47516 blood sugar levels vs a1c a comprehensive guide to managing diabetes 597