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 *

bioheal cbd gummies legit unlocking the power of full spectrum cbd for a healthier you experience the ultimate relaxation with cbd magnesium gummies unlock a deeper state of calmness and wana cbd thc gummies review the ultimate guide to blissful relief activgenix cbd gummies review unlocking the full potential of cbd for a healthier you a comprehen former secretary of state mike pompeo shared tips to his 90 pound weight loss achieve sustainable weight loss with oprah winfrey 39 s gummies weight loss a comprehensive review of unlock the secret to a slimmer you exploring the cost of keto gummies and their surprising benefits choice cbd gummies scam reviews exposing the truth behind the popular cbd trend 2024 update revolutionize your weight loss journey with amazon com keto gummies the ultimate solution for a rea where can i buy keto blast gummies near me unlocking the power of a revolutionary weight loss solut revolutionize your keto journey with shark tank keto gummies a comprehensive review en lemon water with chia seeds weight loss reviews finding the best men 39 s multivitamin gummy 2021 a comprehensive guide to top rated supplemen unlocking optimal performance how performance cbd gummies for ed can transform your life the truth about male enhancing pills separating fact from fiction 18051 what male enhancement works the best a comprehensive guide 42975 the best cannabis gummies for sex boosting libido and intimacy with vigorvita cbd gummies reviews erectile dysfunction generic medications a comprehensive guide 8489 finding the best supplements for erectile dysfunction a comprehensive guide to natural trea elevate your love life discover the surprising benefits of cbd gummies for better sex