Skip to content

Commit d370d0f

Browse files
authored
Merge branch 'trunk' into shipping-billing-addresses-feature
2 parents aa25d99 + 7e9ff1e commit d370d0f

File tree

9 files changed

+118
-63
lines changed

9 files changed

+118
-63
lines changed

changelog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
*** Changelog ***
22

3+
2022-06-30 - version 1.0.5
4+
* Fix - Lower version requirement from PHP 8.0.2 to PHP 7.1.
5+
36
2021-12-15 - version 1.0.4
47
* Add - coupon generator and a new option for orders to allow for coupon generation.
58
* Add - use product name to generate more realistic product term names.

composer.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
"prefer-stable": true,
88
"minimum-stability": "dev",
99
"require": {
10-
"php": ">=8.0.2",
10+
"php": "^7.1 || ^8.0",
11+
"psr/container": "1.0.0",
1112
"composer/installers": "~1.2",
1213
"fakerphp/faker": "^1.17.0",
1314
"jdenticon/jdenticon": "^0.10.0",
@@ -59,5 +60,11 @@
5960
"!vendor/mbezhanov",
6061
"!vendor/symfony"
6162
]
63+
},
64+
"config": {
65+
"allow-plugins": {
66+
"composer/installers": true,
67+
"dealerdirect/phpcodesniffer-composer-installer": true
68+
}
6269
}
6370
}

includes/CLI.php

Lines changed: 2 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static function orders( $args, $assoc_args ) {
9090
}
9191

9292
if ( $amount > 0 ) {
93-
static::disable_emails();
93+
Generator\Order::disable_emails();
9494
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating orders', $amount );
9595
for ( $i = 1; $i <= $amount; $i++ ) {
9696
Generator\Order::generate( true, $assoc_args );
@@ -121,7 +121,7 @@ public static function orders( $args, $assoc_args ) {
121121
public static function customers( $args, $assoc_args ) {
122122
list( $amount ) = $args;
123123

124-
static::disable_emails();
124+
Generator\Customer::disable_emails();
125125
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating customers', $amount );
126126
for ( $i = 1; $i <= $amount; $i++ ) {
127127
Generator\Customer::generate();
@@ -131,40 +131,6 @@ public static function customers( $args, $assoc_args ) {
131131
WP_CLI::success( $amount . ' customers generated.' );
132132
}
133133

134-
/**
135-
* Disable sending WooCommerce emails when generating objects.
136-
*/
137-
protected static function disable_emails() {
138-
$email_actions = array(
139-
'woocommerce_low_stock',
140-
'woocommerce_no_stock',
141-
'woocommerce_product_on_backorder',
142-
'woocommerce_order_status_pending_to_processing',
143-
'woocommerce_order_status_pending_to_completed',
144-
'woocommerce_order_status_processing_to_cancelled',
145-
'woocommerce_order_status_pending_to_failed',
146-
'woocommerce_order_status_pending_to_on-hold',
147-
'woocommerce_order_status_failed_to_processing',
148-
'woocommerce_order_status_failed_to_completed',
149-
'woocommerce_order_status_failed_to_on-hold',
150-
'woocommerce_order_status_cancelled_to_processing',
151-
'woocommerce_order_status_cancelled_to_completed',
152-
'woocommerce_order_status_cancelled_to_on-hold',
153-
'woocommerce_order_status_on-hold_to_processing',
154-
'woocommerce_order_status_on-hold_to_cancelled',
155-
'woocommerce_order_status_on-hold_to_failed',
156-
'woocommerce_order_status_completed',
157-
'woocommerce_order_fully_refunded',
158-
'woocommerce_order_partially_refunded',
159-
'woocommerce_new_customer_note',
160-
'woocommerce_created_customer',
161-
);
162-
163-
foreach ( $email_actions as $action ) {
164-
remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ), 10, 10 );
165-
}
166-
}
167-
168134
/**
169135
* Generate coupons.
170136
*

includes/GenerateBackgroundProcess.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,40 @@
1010
/**
1111
* Calls generator for object type.
1212
*
13-
* @param string $type Type of object to generate.
13+
* @param string $type Type of object to generate.
14+
* @param int $count Number of objects to generate.
1415
*
1516
* @return false If task was successful.
1617
*/
17-
function wc_smooth_generate_object( $type ) {
18-
// Check what generation task to perform.
19-
switch ( $type ) {
20-
case 'order':
21-
Generator\Order::generate();
22-
break;
23-
case 'product':
24-
Generator\Product::generate();
25-
break;
26-
case 'customer':
27-
Generator\Customer::generate();
28-
break;
29-
case 'coupon':
30-
Generator\Coupon::generate();
31-
break;
32-
default:
33-
return false;
34-
}
35-
18+
function wc_smooth_generate_object( $type, $count = 1 ) {
19+
// Check what generation task to perform
20+
$i = 0;
21+
do {
22+
// Check what generation task to perform.
23+
switch ( $type ) {
24+
case 'order':
25+
Generator\Order::disable_emails();
26+
Generator\Order::generate();
27+
break;
28+
case 'product':
29+
Generator\Product::generate();
30+
break;
31+
case 'customer':
32+
Generator\Customer::disable_emails();
33+
Generator\Customer::generate();
34+
break;
35+
case 'coupon':
36+
Generator\Coupon::generate();
37+
break;
38+
default:
39+
return false;
40+
}
41+
} while( $i++ < intval( $count ) );
42+
3643
return false;
3744
}
3845

39-
add_action( 'wc_smooth_generate_object', 'wc_smooth_generate_object' );
46+
add_action( 'wc_smooth_generate_object', 'wc_smooth_generate_object' , 10, 2 );
4047

4148
/**
4249
* Schedule async actions for generation of objects.

includes/Generator/Customer.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,4 +144,18 @@ public static function generate( $save = true ) {
144144
return $customer;
145145
}
146146

147+
148+
/**
149+
* Disable sending WooCommerce emails when generating objects.
150+
*/
151+
public static function disable_emails() {
152+
$email_actions = array(
153+
'woocommerce_new_customer_note',
154+
'woocommerce_created_customer',
155+
);
156+
157+
foreach ( $email_actions as $action ) {
158+
remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ), 10, 10 );
159+
}
160+
}
147161
}

includes/Generator/Generator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ abstract class Generator {
1717
/**
1818
* Holds the faker factory object.
1919
*
20-
* @var \Faker\Factory Factory object.
20+
* @var \Faker\Generator Factory object.
2121
*/
2222
protected static $faker;
2323

includes/Generator/Order.php

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,30 @@ public static function generate( $save = true, $assoc_args = array() ) {
6161
$order->set_shipping_state( $customer->get_shipping_state() );
6262
$order->set_shipping_country( $customer->get_shipping_country() );
6363
$order->set_shipping_company( $customer->get_shipping_company() );
64-
$order->set_status( self::get_status( $assoc_args ) );
64+
65+
// 20% chance
66+
if ( rand( 0, 100 ) <= 20 ) {
67+
$country_code = $order->get_shipping_country();
68+
69+
$calculate_tax_for = array(
70+
'country' => $country_code,
71+
'state' => '',
72+
'postcode' => '',
73+
'city' => '',
74+
);
75+
76+
$fee = new \WC_Order_Item_Fee();
77+
$randomAmount = self::$faker->randomFloat( 2, 0.05, 100 );
78+
79+
$fee->set_name( 'Extra Fee' );
80+
$fee->set_amount( $randomAmount );
81+
$fee->set_tax_class( '' );
82+
$fee->set_tax_status( 'taxable' );
83+
$fee->set_total( $randomAmount );
84+
$fee->calculate_taxes( $calculate_tax_for );
85+
$order->add_item( $fee );
86+
}
87+
$order->set_status( self::get_status( $assoc_args ) );
6588
$order->calculate_totals( true );
6689

6790
$date = self::get_date_created( $assoc_args );
@@ -93,15 +116,50 @@ public static function get_customer() {
93116
$existing = (bool) wp_rand( 0, 1 );
94117

95118
if ( $existing ) {
96-
$user_id = (int) $wpdb->get_var( "SELECT ID FROM {$wpdb->users} ORDER BY rand() LIMIT 1" ); // phpcs:ignore
119+
$total_users = (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->users}" );
120+
$offset = wp_rand( 0, $total_users );
121+
$user_id = (int) $wpdb->get_var( "SELECT ID FROM {$wpdb->users} ORDER BY rand() LIMIT $offset, 1" ); // phpcs:ignore
97122
return new \WC_Customer( $user_id );
98123
}
99124

125+
Customer::disable_emails();
100126
$customer = Customer::generate( ! $guest );
101127

102128
return $customer;
103129
}
104130

131+
/**
132+
* Disable sending WooCommerce emails when generating objects.
133+
*/
134+
public static function disable_emails() {
135+
$email_actions = array(
136+
'woocommerce_low_stock',
137+
'woocommerce_no_stock',
138+
'woocommerce_product_on_backorder',
139+
'woocommerce_order_status_pending_to_processing',
140+
'woocommerce_order_status_pending_to_completed',
141+
'woocommerce_order_status_processing_to_cancelled',
142+
'woocommerce_order_status_pending_to_failed',
143+
'woocommerce_order_status_pending_to_on-hold',
144+
'woocommerce_order_status_failed_to_processing',
145+
'woocommerce_order_status_failed_to_completed',
146+
'woocommerce_order_status_failed_to_on-hold',
147+
'woocommerce_order_status_cancelled_to_processing',
148+
'woocommerce_order_status_cancelled_to_completed',
149+
'woocommerce_order_status_cancelled_to_on-hold',
150+
'woocommerce_order_status_on-hold_to_processing',
151+
'woocommerce_order_status_on-hold_to_cancelled',
152+
'woocommerce_order_status_on-hold_to_failed',
153+
'woocommerce_order_status_completed',
154+
'woocommerce_order_fully_refunded',
155+
'woocommerce_order_partially_refunded',
156+
);
157+
158+
foreach ( $email_actions as $action ) {
159+
remove_action( $action, array( 'WC_Emails', 'send_transactional_email' ), 10, 10 );
160+
}
161+
}
162+
105163
/**
106164
* Returns a date to use as the order date. If no date arguments have been passed, this will
107165
* return the current date. If a `date-start` argument is provided, a random date will be chosen

phpcs.xml.dist

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
<!-- Configs -->
2424
<config name="minimum_supported_wp_version" value="5.0"/>
25-
<config name="testVersion" value="7.0-"/>
25+
<config name="testVersion" value="7.1-"/>
2626

2727
<!-- Rules -->
2828
<rule ref="WooCommerce-Core">

wc-smooth-generator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* Author URI: https://woocommerce.com
99
*
1010
* Tested up to: 5.7
11-
* Requires PHP: 8.0.2
11+
* Requires PHP: 7.1
1212
* WC requires at least: 5.0.0
1313
* WC tested up to: 6.0.0
1414
* Woo: 000000:0a0a0a0a0a0a0a0a0a0a0a0a0a0a0a0

0 commit comments

Comments
 (0)