| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Database\Seeder; |
| 4 |
|
| 5 |
use FluentCart\Faker\Factory; |
| 6 |
use FluentCart\App\Models\Customer; |
| 7 |
use FluentCart\App\Models\CustomerAddresses; |
| 8 |
|
| 9 |
class CustomerAddressSeeder |
| 10 |
{ |
| 11 |
public static function seed($count, $assoc_args = []) |
| 12 |
{ |
| 13 |
$faker = Factory::create(); |
| 14 |
$allCustomers = Customer::query()->pluck('id')->toArray(); |
| 15 |
|
| 16 |
if (empty($allCustomers)) { |
| 17 |
echo "No customers found to seed addresses.\n"; |
| 18 |
return; |
| 19 |
} |
| 20 |
|
| 21 |
$addressList = []; |
| 22 |
|
| 23 |
if (defined('WP_CLI') && WP_CLI) { |
| 24 |
$progress = \WP_CLI\Utils\make_progress_bar('%CSeeding Customer Addresses', count($allCustomers) * 2); |
| 25 |
} |
| 26 |
|
| 27 |
$countryCodes = [ |
| 28 |
'US' => ['New York', 'Los Angeles', 'Chicago', 'Houston'], |
| 29 |
'CA' => ['Toronto', 'Vancouver', 'Montreal', 'Calgary'], |
| 30 |
'GB' => ['London', 'Manchester', 'Birmingham', 'Liverpool'], |
| 31 |
'AU' => ['Sydney', 'Melbourne', 'Brisbane', 'Perth'], |
| 32 |
'DE' => ['Berlin', 'Munich', 'Hamburg', 'Frankfurt'], |
| 33 |
'FR' => ['Paris', 'Marseille', 'Lyon', 'Toulouse'], |
| 34 |
'IT' => ['Rome', 'Milan', 'Naples', 'Turin'], |
| 35 |
'ES' => ['Madrid', 'Barcelona', 'Seville', 'Valencia'], |
| 36 |
'BR' => ['São Paulo', 'Rio de Janeiro', 'Salvador', 'Brasília'], |
| 37 |
'JP' => ['Tokyo', 'Osaka', 'Kyoto', 'Sapporo'], |
| 38 |
'BD' => ['Sylhet', 'Moulvi Bazar', 'Sreemangal', 'Noakhali'] |
| 39 |
]; |
| 40 |
|
| 41 |
foreach ($allCustomers as $customerId) { |
| 42 |
foreach (['shipping', 'billing'] as $type) { |
| 43 |
$gender = $faker->randomElement(['male', 'female']); |
| 44 |
$country = $faker->randomElement(array_keys($countryCodes)); |
| 45 |
$city = $faker->randomElement($countryCodes[$country]); |
| 46 |
|
| 47 |
$addressList[] = [ |
| 48 |
'customer_id' => $customerId, |
| 49 |
'is_primary' => ($type === 'shipping') ? 1 : 0, |
| 50 |
'type' => $type, |
| 51 |
'status' => $faker->randomElement(['active', 'inactive']), |
| 52 |
'label' => $faker->word(), |
| 53 |
'name' => $faker->name($gender), |
| 54 |
'address_1' => $faker->streetAddress(), |
| 55 |
'address_2' => $faker->secondaryAddress(), |
| 56 |
'city' => $city, |
| 57 |
'state' => $faker->state(), |
| 58 |
'postcode' => $faker->postcode(), |
| 59 |
'phone' => $faker->phoneNumber(), |
| 60 |
'email' => $faker->email(), |
| 61 |
'country' => $country, |
| 62 |
'created_at' => $faker->dateTimeBetween('-700 days', 'now')->format('Y-m-d H:i:s'), |
| 63 |
'updated_at' => gmdate('Y-m-d H:i:s'), |
| 64 |
]; |
| 65 |
|
| 66 |
if (defined('WP_CLI') && WP_CLI) { |
| 67 |
$progress->tick(); |
| 68 |
} else { |
| 69 |
echo wp_kses_post( sprintf( |
| 70 |
/* translators: 1: Address type, 2: Customer ID */ |
| 71 |
__('Inserting %1$s address for customer %2$s<br>', 'fluent-cart'), |
| 72 |
esc_html($type), |
| 73 |
esc_html($customerId) |
| 74 |
) ); |
| 75 |
|
| 76 |
} |
| 77 |
|
| 78 |
if (isset($count) && count($addressList) >= $count) { |
| 79 |
break 2; |
| 80 |
} |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
CustomerAddresses::query()->insert($addressList); |
| 85 |
|
| 86 |
if (defined('WP_CLI') && WP_CLI) { |
| 87 |
$progress->finish(); |
| 88 |
//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 89 |
echo \WP_CLI::colorize('%n'); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|