PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.6.4
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.6.4
1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.4 1.5.5 1.5.3 1.5.2 1.5.1 1.5.0 1.4.2 1.4.1 1.4.0 1.3.28 1.3.27 1.3.26 1.3.25 1.3.23 1.3.22 1.3.21 1.3.20 1.3.19 trunk 1.2.0 All 47 releases
fluent-cart / database / Seeder / OrderAddressSeeder.php

OrderAddressSeeder.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.6.4, at database/Seeder/OrderAddressSeeder.php

81 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCart\Database\Seeder;
4
5 use FluentCart\Faker\Factory;
6 use FluentCart\App\Models\OrderAddress;
7 use FluentCart\App\Models\Order;
8
9 class OrderAddressSeeder
10 {
11 public static function seed($count)
12 {
13 $faker = Factory::create();
14 $addressLists = [];
15 $orderIds = Order::query()->pluck('id')->toArray();
16
17 if (empty($orderIds)) {
18 echo "No orders found. Skipping order address seeding.\n";
19 return;
20 }
21
22 if (defined('WP_CLI') && WP_CLI) {
23 $progress = \WP_CLI\Utils\make_progress_bar('%CSeeding Order Addresses', $count);
24 }
25
26 $countryCodes = [
27 'US' => ['New York', 'Los Angeles', 'Chicago', 'Houston'],
28 'CA' => ['Toronto', 'Vancouver', 'Montreal', 'Calgary'],
29 'GB' => ['London', 'Manchester', 'Birmingham', 'Liverpool'],
30 'AU' => ['Sydney', 'Melbourne', 'Brisbane', 'Perth'],
31 'DE' => ['Berlin', 'Munich', 'Hamburg', 'Frankfurt'],
32 'FR' => ['Paris', 'Marseille', 'Lyon', 'Toulouse'],
33 'IT' => ['Rome', 'Milan', 'Naples', 'Turin'],
34 'ES' => ['Madrid', 'Barcelona', 'Seville', 'Valencia'],
35 'BR' => ['São Paulo', 'Rio de Janeiro', 'Salvador', 'Brasília'],
36 'JP' => ['Tokyo', 'Osaka', 'Kyoto', 'Sapporo'],
37 'BD' => ['Sylhet', 'Moulvi Bazar', 'Sreemangal', 'Noakhali']
38 ];
39
40 for ($i = 0; $i < $count; $i++) {
41 $country = $faker->randomElement(array_keys($countryCodes));
42 $city = $faker->randomElement($countryCodes[$country]);
43
44 $addressLists[] = [
45 'order_id' => $faker->randomElement($orderIds),
46 'type' => $faker->randomElement(['billing', 'shipping']),
47 'name' => $faker->name(),
48 'address_1' => $faker->streetAddress(),
49 'address_2' => $faker->optional()->secondaryAddress(),
50 'city' => $city,
51 'state' => $faker->state(),
52 'postcode' => $faker->postcode(),
53 'country' => $country, // short code saved here
54 'created_at' => $faker->dateTimeBetween('-700 days', 'now'),
55 'updated_at' => new \DateTime(),
56 ];
57
58 if (defined('WP_CLI') && WP_CLI) {
59 if ($i !== $count - 1) {
60 $progress->tick();
61 }
62 } else {
63 echo wp_kses_post( sprintf(
64 /* translators: %d: order address ID */
65 __('Inserting Order Address%1$s<br>', 'fluent-cart'),
66 esc_html($i + 1)
67 ) );
68 }
69 }
70
71 OrderAddress::query()->insert($addressLists);
72
73 if (defined('WP_CLI') && WP_CLI) {
74 $progress->tick();
75 $progress->finish();
76 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
77 echo \WP_CLI::colorize('%n');
78 }
79 }
80 }
81