| @@ -28,8 +28,11 @@ | ||
| 28 | 28 | $products = Product::with('variants')->take(100)->get()->toArray(); |
| 29 | 29 | $orderCount = Order::query()->count(); |
| 30 | 30 | $orderItems = []; |
| 31 | 31 | $orderTransactions = []; |
| 32 | + // Accumulated across the whole batch, like the two above: the insert | |
| 33 | + // happens once after the loop. | |
| 34 | + $appliedCouponData = []; | |
| 32 | 35 | $coupons = Coupon::query()->where('status', 'active')->get()->toArray(); |
| 33 | 36 | |
| 34 | 37 | if (defined('WP_CLI') && WP_CLI) { |
| 35 | 38 | $progress = \WP_CLI\Utils\make_progress_bar('%CSeeding Orders', $count); |
| @@ -35,13 +38,22 @@ | ||
| 35 | 38 | $progress = \WP_CLI\Utils\make_progress_bar('%CSeeding Orders', $count); |
| 36 | 39 | } |
| 37 | 40 | |
| 38 | 41 | $storeSettings = new StoreSettings(); |
| 42 | + | |
| 43 | + // How far back seeded orders may be dated. Reports filter on | |
| 44 | + // o.created_at, so `--days=60` puts the whole batch inside a report's | |
| 45 | + // date window. Defaults to the historic 450-day spread. | |
| 46 | + $maxAgeDays = isset($assoc_args['days']) ? absint($assoc_args['days']) : 0; | |
| 47 | + if (!$maxAgeDays) { | |
| 48 | + $maxAgeDays = 450; | |
| 49 | + } | |
| 50 | + | |
| 39 | 51 | for ($i = 0; $i < $count; $i++) { |
| 40 | 52 | $totalPrice = 0; |
| 41 | 53 | $discountTotal = 0; |
| 42 | 54 | $tempOrderItems = []; |
| 43 | - $createdDate = $faker->dateTimeBetween('-450 days', 'now')->format('Y-m-d H:i:s'); | |
| 55 | + $createdDate = $faker->dateTimeBetween('-' . $maxAgeDays . ' days', 'now')->format('Y-m-d H:i:s'); | |
| 44 | 56 | $createdDateGmt = DateTime::anyTimeToGmt($createdDate); |
| 45 | 57 | |
| 46 | 58 | $fulfilmentType = $faker->randomElement(['physical', 'digital']); |
| 47 | 59 | |
| @@ -233,14 +245,16 @@ | ||
| 233 | 245 | 'total' => $totalPrice, |
| 234 | 246 | 'created_at' => $createdDateGmt, |
| 235 | 247 | ]; |
| 236 | 248 | |
| 237 | - $appliedCouponData = []; | |
| 238 | 249 | if (!empty($appliedCoupon) && $discountTotal > 0) { |
| 250 | + // fct_applied_coupons has no customer_id column (see | |
| 251 | + // AppliedCouponsMigrator) — including it made every seed run die | |
| 252 | + // with "Unknown column 'customer_id' in 'field list'" before the | |
| 253 | + // order operations were ever seeded. | |
| 239 | 254 | $appliedCouponData[] = [ |
| 240 | 255 | 'order_id' => $order->id, |
| 241 | 256 | 'coupon_id' => $appliedCoupon['id'], |
| 242 | - 'customer_id' => $customerId, | |
| 243 | 257 | 'code' => $appliedCoupon['code'], |
| 244 | 258 | 'amount' => $discountTotal, |
| 245 | 259 | 'created_at' => $createdDateGmt, |
| 246 | 260 | ]; |
| @@ -267,9 +281,11 @@ | ||
| 267 | 281 | } |
| 268 | 282 | |
| 269 | 283 | OrderTransaction::query()->insert($orderTransactions); |
| 270 | 284 | OrderItem::query()->insert($orderItems); |
| 271 | - AppliedCoupon::query()->insert($appliedCouponData); | |
| 285 | + if ($appliedCouponData) { | |
| 286 | + AppliedCoupon::query()->insert($appliedCouponData); | |
| 287 | + } | |
| 272 | 288 | |
| 273 | 289 | (new CustomerHelper)->calculateCustomerStats(); |
| 274 | 290 | |
| 275 | 291 | if (defined('WP_CLI') && WP_CLI) { |