@@ -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
0 commit comments