Skip to content

Commit e17884b

Browse files
committed
Add error logging for order generation and coupon application failures
1 parent 553d847 commit e17884b

File tree

1 file changed

+66
-13
lines changed

1 file changed

+66
-13
lines changed

includes/Generator/Order.php

Lines changed: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,16 @@ public static function generate( $save = true, $assoc_args = array() ) {
4646
$order = new \WC_Order();
4747
$customer = self::get_customer();
4848
if ( ! $customer instanceof \WC_Customer ) {
49+
error_log( 'Order generation failed: Could not generate or retrieve customer' );
4950
return false;
5051
}
5152
$products = self::get_random_products( 1, 10 );
5253

54+
if ( empty( $products ) ) {
55+
error_log( 'Order generation failed: No products available to add to order' );
56+
return false;
57+
}
58+
5359
foreach ( $products as $product ) {
5460
$quantity = self::$faker->numberBetween( 1, 10 );
5561
$order->add_product( $product, $quantity );
@@ -135,15 +141,22 @@ public static function generate( $save = true, $assoc_args = array() ) {
135141
if ( $include_coupon ) {
136142
$coupon = self::get_or_create_coupon();
137143
if ( $coupon ) {
138-
$order->apply_coupon( $coupon );
139-
// Recalculate totals after applying coupon
140-
$order->calculate_totals( true );
144+
$apply_result = $order->apply_coupon( $coupon );
145+
if ( is_wp_error( $apply_result ) ) {
146+
error_log( 'Coupon application failed: ' . $apply_result->get_error_message() . ' (Coupon: ' . $coupon->get_code() . ')' );
147+
} else {
148+
// Recalculate totals after applying coupon
149+
$order->calculate_totals( true );
150+
}
141151
}
142152
}
143153

144154
// Orders created before 2024-01-09 represents orders created before the attribution feature was added.
145155
if ( ! ( strtotime( $date ) < strtotime( '2024-01-09' ) ) ) {
146-
OrderAttribution::add_order_attribution_meta( $order, $assoc_args );
156+
$attribution_result = OrderAttribution::add_order_attribution_meta( $order, $assoc_args );
157+
if ( $attribution_result && is_wp_error( $attribution_result ) ) {
158+
error_log( 'Order attribution meta addition failed: ' . $attribution_result->get_error_message() );
159+
}
147160
}
148161

149162
// Set paid and completed dates based on order status.
@@ -159,7 +172,11 @@ public static function generate( $save = true, $assoc_args = array() ) {
159172
}
160173

161174
if ( $save ) {
162-
$order->save();
175+
$save_result = $order->save();
176+
if ( is_wp_error( $save_result ) ) {
177+
error_log( 'Order save failed: ' . $save_result->get_error_message() );
178+
return false;
179+
}
163180

164181
// Handle --refund-ratio parameter for completed orders
165182
if ( isset( $assoc_args['refund-ratio'] ) && 'completed' === $status ) {
@@ -185,7 +202,7 @@ public static function generate( $save = true, $assoc_args = array() ) {
185202
$first_refund = self::create_refund( $order );
186203

187204
// Some partial refunds get a second refund (always partial)
188-
if ( $first_refund && wp_rand( 1, 100 ) <= self::SECOND_REFUND_PROBABILITY ) {
205+
if ( $first_refund && is_object( $first_refund ) && wp_rand( 1, 100 ) <= self::SECOND_REFUND_PROBABILITY ) {
189206
self::create_refund( $order, true, $first_refund );
190207
}
191208
}
@@ -215,13 +232,18 @@ public static function generate( $save = true, $assoc_args = array() ) {
215232
public static function batch( $amount, array $args = array() ) {
216233
$amount = self::validate_batch_amount( $amount );
217234
if ( is_wp_error( $amount ) ) {
235+
error_log( 'Batch generation failed: ' . $amount->get_error_message() );
218236
return $amount;
219237
}
220238

221239
$order_ids = array();
222240

223241
for ( $i = 1; $i <= $amount; $i ++ ) {
224-
$order = self::generate( true, $args );
242+
$order = self::generate( true, $args );
243+
if ( ! $order ) {
244+
error_log( "Batch generation failed: Order {$i} of {$amount} could not be generated" );
245+
continue;
246+
}
225247
$order_ids[] = $order->get_id();
226248
}
227249

@@ -248,6 +270,10 @@ public static function get_customer() {
248270

249271
$customer = Customer::generate( ! $guest );
250272

273+
if ( ! $customer instanceof \WC_Customer ) {
274+
error_log( 'Customer generation failed: Customer::generate() returned invalid result' );
275+
}
276+
251277
return $customer;
252278
}
253279

@@ -322,6 +348,11 @@ protected static function get_random_products( int $min_amount = 1, int $max_amo
322348
AND post_status='publish'"
323349
);
324350

351+
if ( $num_existing_products === 0 ) {
352+
error_log( 'No published products found in database' );
353+
return array();
354+
}
355+
325356
$num_products_to_get = wp_rand( $min_amount, $max_amount );
326357

327358
if ( $num_products_to_get > $num_existing_products ) {
@@ -334,18 +365,32 @@ protected static function get_random_products( int $min_amount = 1, int $max_amo
334365
'orderby' => 'rand',
335366
) );
336367

337-
foreach ( $query->get_products() as $product_id ) {
368+
$product_ids = $query->get_products();
369+
if ( empty( $product_ids ) ) {
370+
error_log( 'WC_Product_Query returned no product IDs' );
371+
return array();
372+
}
373+
374+
foreach ( $product_ids as $product_id ) {
338375
$product = wc_get_product( $product_id );
339376

377+
if ( ! $product ) {
378+
error_log( "Failed to retrieve product with ID: {$product_id}" );
379+
continue;
380+
}
381+
340382
if ( $product->is_type( 'variable' ) ) {
341383
$available_variations = $product->get_available_variations();
342384
if ( empty( $available_variations ) ) {
343385
continue;
344386
}
345387
$index = self::$faker->numberBetween( 0, count( $available_variations ) - 1 );
346-
$products[] = new \WC_Product_Variation( $available_variations[ $index ]['variation_id'] );
388+
$variation = new \WC_Product_Variation( $available_variations[ $index ]['variation_id'] );
389+
if ( $variation && $variation->exists() ) {
390+
$products[] = $variation;
391+
}
347392
} else {
348-
$products[] = new \WC_Product( $product_id );
393+
$products[] = $product;
349394
}
350395
}
351396

@@ -374,10 +419,18 @@ protected static function get_or_create_coupon() {
374419
// Create 3 percentage coupons (5%-25%)
375420
$percent_result = Coupon::batch( 3, array( 'min' => 5, 'max' => 25, 'discount_type' => 'percent' ) );
376421

377-
// If coupon creation failed, return false
378-
if ( is_wp_error( $fixed_result ) || is_wp_error( $percent_result ) ) {
379-
return false;
422+
// If coupon creation failed, return false
423+
if ( is_wp_error( $fixed_result ) || is_wp_error( $percent_result ) ) {
424+
$error_message = 'Coupon creation failed: ';
425+
if ( is_wp_error( $fixed_result ) ) {
426+
$error_message .= 'Fixed coupons error: ' . $fixed_result->get_error_message() . ' ';
380427
}
428+
if ( is_wp_error( $percent_result ) ) {
429+
$error_message .= 'Percentage coupons error: ' . $percent_result->get_error_message();
430+
}
431+
error_log( $error_message );
432+
return false;
433+
}
381434

382435
// Now get a random coupon from the ones we just created
383436
$coupon = Coupon::get_random();

0 commit comments

Comments
 (0)