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 *