Skip to content

Instantly share code, notes, and snippets.

@shirokoweb
Created May 3, 2019 11:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shirokoweb/6363d50c479a94b6ed878ccc8ba62fd0 to your computer and use it in GitHub Desktop.
Save shirokoweb/6363d50c479a94b6ed878ccc8ba62fd0 to your computer and use it in GitHub Desktop.
Random simple product generation for benchmarking
<?php
/*
DISCLAIMER :
This script is for BENCHMARK, use at your own risk.
DO NOT run this script unless you FULLY understand and know what you are doing.
This script will generate random 1M simple products on your WooCommerce (can be modified @line 43)
*/
if (php_sapi_name() !== 'cli') {
die('This script can only be run through WP CLI');
}
set_time_limit(0);
ini_set('max_execution_time',0);
function find_wordpress_base_path() {
$dir = dirname(__FILE__);
do {
if( file_exists($dir."/wp-config.php") ) {
return $dir;
}
}while($dir = realpath("$dir/.."));
return null;
}
function generateRandomString($length = 7) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[mt_rand(0, $charactersLength - 1)];
}
return $randomString;
}
define('BASE_PATH', find_wordpress_base_path()."/");
define('WP_USE_THEMES', false);
global $wp, $wp_query, $wp_the_query, $wp_rewrite, $wp_did_header;
require(BASE_PATH.'wp-load.php');
// CAREFUL : script will generate 1M products ! Change it to somewhat your server can handle
for($i = 1; $i < 1000000; $i++){
try{
$sku = generateRandomString();
$price = 99.99;
$post_id = wp_insert_post(array(
'post_title' => $sku
, 'post_content' => 'Imported Product Content'
, 'post_status' => 'publish'
, 'post_type' => 'product'
));
wp_set_object_terms($post_id,'simple','product_type');
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', 'no' );
update_post_meta( $post_id, '_regular_price', $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', '' );
update_post_meta( $post_id, '_length', '' );
update_post_meta( $post_id, '_width', '' );
update_post_meta( $post_id, '_height', '' );
update_post_meta( $post_id, '_sku', $sku );
update_post_meta( $post_id, '_sale_price_dates_from', '' );
update_post_meta( $post_id, '_sale_price_dates_to', '' );
update_post_meta( $post_id, '_price', (float)$price );
update_post_meta( $post_id, '_sold_individually', 'yes' );
update_post_meta( $post_id, '_manage_stock', 'yes' );
update_post_meta( $post_id, '_backorders', 'no' );
update_post_meta( $post_id, '_stock', '100' );
echo ($sku." generatedn");
}catch(Exception $e){echo ($e->getMessage()."n");}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment