| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\Database\Seeder; |
| 4 |
|
| 5 |
use FluentCart\App\Models\DynamicModel; |
| 6 |
use FluentCart\Faker\Factory; |
| 7 |
use FluentCart\App\Models\Product; |
| 8 |
use FluentCart\App\Models\ProductDetail; |
| 9 |
use FluentCart\App\Models\ProductVariation; |
| 10 |
use FluentCart\App\Helpers\Helper; |
| 11 |
use FluentCart\Framework\Support\Arr; |
| 12 |
use FluentCart\Framework\Support\Str; |
| 13 |
|
| 14 |
class ProductSeeder |
| 15 |
{ |
| 16 |
public static function seed($count, $assoc_args = []) |
| 17 |
{ |
| 18 |
$productTypes = Arr::get($assoc_args, 'product_types', []); |
| 19 |
$faker = Factory::create(); |
| 20 |
$faker->addProvider(new \FluentCart\Database\Seeder\ProductNameProvide($faker)); |
| 21 |
|
| 22 |
if (defined('WP_CLI') && WP_CLI) { |
| 23 |
$progress = \WP_CLI\Utils\make_progress_bar('%CSeeding Products', $count); |
| 24 |
} |
| 25 |
$variationTypes = []; |
| 26 |
|
| 27 |
$lastId = (new DynamicModel([], 'posts'))->newQuery()->latest('ID')->value('ID'); |
| 28 |
|
| 29 |
for ($i = 0; $i <= $count - 1; $i++) { |
| 30 |
$fakeProduct = $faker->getProduct($productTypes); |
| 31 |
$variationTypes[$i] = $fakeProduct['variationType']; |
| 32 |
|
| 33 |
|
| 34 |
$productTitle = $fakeProduct['name']; |
| 35 |
$productName = Str::slug($productTitle, '-', null); |
| 36 |
$createdDate = $faker->dateTimeBetween('-700 days', 'now'); |
| 37 |
$productNameSuffix = $faker->dateTime()->format('d-m-Y-H-i-s'); |
| 38 |
$data = [ |
| 39 |
'post_author' => get_current_user_id(), |
| 40 |
'post_date' => $createdDate, |
| 41 |
'post_date_gmt' => $createdDate, |
| 42 |
'post_content' => '', |
| 43 |
'post_content_filtered' => '', |
| 44 |
'post_title' => $productTitle, |
| 45 |
'post_excerpt' => '', |
| 46 |
'post_status' => 'publish', |
| 47 |
'post_type' => 'fluent-products', |
| 48 |
'comment_status' => 'open', |
| 49 |
'ping_status' => 'closed', |
| 50 |
'post_password' => '', |
| 51 |
'post_name' => $productName . '-' . $productNameSuffix, |
| 52 |
'to_ping' => '', |
| 53 |
'pinged' => '', |
| 54 |
'post_modified' => $createdDate, |
| 55 |
'post_modified_gmt' => $createdDate, |
| 56 |
'post_parent' => 0, |
| 57 |
'menu_order' => 0, |
| 58 |
'post_mime_type' => '', |
| 59 |
'guid' => get_site_url() . '/?items=' . $productName . '-' . $productNameSuffix |
| 60 |
]; |
| 61 |
|
| 62 |
|
| 63 |
Product::query()->insert($data); |
| 64 |
|
| 65 |
if (defined('WP_CLI') && WP_CLI) { |
| 66 |
if ($i !== $count - 1) { |
| 67 |
$progress->tick(); |
| 68 |
} |
| 69 |
} else { |
| 70 |
echo wp_kses_post( sprintf( |
| 71 |
/* translators: %d: product ID */ |
| 72 |
__('Inserting Product %1$s<br>', 'fluent-cart'), |
| 73 |
esc_html($i + 1) |
| 74 |
) ); |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
//$lastProductsIds = Product::select('ID')->orderBy('ID', 'desc')->limit($count)->get()->pluck('ID'); |
| 79 |
$createdProducts = Product::query()->select(['ID', 'post_title']) |
| 80 |
->where('ID', '>', $lastId) |
| 81 |
->orderBy('ID', 'asc')->limit($count)->get(); |
| 82 |
|
| 83 |
|
| 84 |
$productDetails = []; |
| 85 |
|
| 86 |
|
| 87 |
$variationLoop = -1; |
| 88 |
foreach ($createdProducts as $createdProduct) { |
| 89 |
$variationLoop = $variationLoop + 1; |
| 90 |
|
| 91 |
|
| 92 |
$createdPostId = $createdProduct->ID; |
| 93 |
|
| 94 |
$productPrices = []; |
| 95 |
$productComparePrices = []; |
| 96 |
|
| 97 |
if (!empty($createdPostId)) { |
| 98 |
$fulfilmentType = 'physical'; |
| 99 |
$stockStatus = $faker->randomElement(['in-stock', 'out-of-stock']); |
| 100 |
$isSubscribable = $variationTypes[$variationLoop] === 'subscribable'; |
| 101 |
$basePrice = $faker->numberBetween(180, 520); |
| 102 |
|
| 103 |
|
| 104 |
if ($isSubscribable) { |
| 105 |
//ensure if a product type is subscribable, fulfilmentType type should be digital, |
| 106 |
//and its variation type should be simple_variations |
| 107 |
$fulfilmentType = 'digital'; |
| 108 |
$variationType = Helper::PRODUCT_TYPE_SIMPLE_VARIATION; |
| 109 |
$variationCount = 4; |
| 110 |
} else { |
| 111 |
$variationType = $faker->randomElement([Helper::PRODUCT_TYPE_SIMPLE, Helper::PRODUCT_TYPE_SIMPLE_VARIATION]); |
| 112 |
$variationCount = $faker->numberBetween(1, 2); |
| 113 |
} |
| 114 |
$createdDate = $faker->dateTimeBetween('-700 days', 'now'); |
| 115 |
$defaultVariationId = []; |
| 116 |
|
| 117 |
|
| 118 |
for ($j = 0; $j <= $variationCount - 1; $j++) { |
| 119 |
|
| 120 |
if ($isSubscribable) { |
| 121 |
if ($j === 0) { |
| 122 |
$price = $basePrice; |
| 123 |
} else if ($j === 1) { |
| 124 |
$price = ((int) (($basePrice - $basePrice * (.04)) / 52)) + 2; |
| 125 |
} else if ($j === 2) { |
| 126 |
$price = ((int) (($basePrice - $basePrice * (.04)) / 12)) + 1; |
| 127 |
} else { |
| 128 |
$price = (int) (($basePrice - $basePrice * (.04))); |
| 129 |
} |
| 130 |
} else { |
| 131 |
$price = $faker->numberBetween(10, 100); |
| 132 |
} |
| 133 |
|
| 134 |
$productPrices[] = $price; |
| 135 |
|
| 136 |
$comparePrice = $faker->numberBetween($price - 1, $price + wp_rand(10, 100)); |
| 137 |
|
| 138 |
if ($isSubscribable && $comparePrice > $basePrice) { |
| 139 |
$comparePrice = (int) (($basePrice - $basePrice * (.02))) - 1; |
| 140 |
} |
| 141 |
|
| 142 |
$productComparePrices[] = $comparePrice; |
| 143 |
|
| 144 |
$otherInfo = []; |
| 145 |
|
| 146 |
|
| 147 |
//Check if the product supports subscription |
| 148 |
if ($variationTypes[$variationLoop] === 'subscribable' && $j === 0) { |
| 149 |
$paymentType = 'onetime'; |
| 150 |
} else if ($variationTypes[$variationLoop] === 'subscribable' && $j > 0) { |
| 151 |
$paymentType = 'subscription'; |
| 152 |
} else { |
| 153 |
$paymentType = 'onetime'; |
| 154 |
} |
| 155 |
|
| 156 |
if ($paymentType == 'onetime') { |
| 157 |
$otherInfo['payment_type'] = $paymentType; |
| 158 |
} else { |
| 159 |
|
| 160 |
$manageSetupFee = $faker->randomElement(['yes', 'no']); |
| 161 |
$times = $faker->numberBetween(1, 10); |
| 162 |
|
| 163 |
|
| 164 |
// $repeatIUnit = $faker->randomElement(['daily', 'weekly', 'monthly', 'yearly']); |
| 165 |
|
| 166 |
$repeatIUnits = ['lifetime', 'weekly', "monthly", "yearly"]; |
| 167 |
$repeatIUnit = Arr::get($repeatIUnits, $j, 'yearly'); |
| 168 |
// $chargeFee = number_format(($price / $times), 2); |
| 169 |
$billingSummary = "{$price} {$repeatIUnit} for {$times} Times"; |
| 170 |
$otherInfo = [ |
| 171 |
'payment_type' => $paymentType, |
| 172 |
'times' => $times, |
| 173 |
'repeat_interval' => $repeatIUnit, |
| 174 |
'billing_summary' => $billingSummary, |
| 175 |
'manage_setup_fee' => $manageSetupFee, |
| 176 |
]; |
| 177 |
if ($manageSetupFee == 'yes') { |
| 178 |
$setupFeePerItem = $faker->randomElement(['yes', 'no']); |
| 179 |
$otherInfo['signup_fee'] = Helper::toCent($faker->numberBetween(10, 1000)); |
| 180 |
$otherInfo['signup_fee_name'] = $faker->productSignUpFeeName(); |
| 181 |
$otherInfo['setup_fee_per_item'] = $setupFeePerItem; |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
$stock = ($stockStatus === Helper::IN_STOCK) ? 500 : 0; |
| 186 |
|
| 187 |
$productVariationDetails = [ |
| 188 |
'post_id' => $createdPostId, |
| 189 |
'serial_index' => $j + 1, |
| 190 |
'variation_title' => ($j === 0 && $variationCount === 1) ? |
| 191 |
$createdProduct['post_title'] : |
| 192 |
$faker->productVariation( |
| 193 |
$variationTypes[$variationLoop], |
| 194 |
$createdProduct['post_title'], |
| 195 |
$paymentType, |
| 196 |
$j |
| 197 |
), |
| 198 |
'payment_type' => $paymentType, |
| 199 |
'stock_status' => $stockStatus, |
| 200 |
'total_stock' => $stock, |
| 201 |
'available' => $stock, |
| 202 |
'fulfillment_type' => $fulfilmentType, |
| 203 |
'item_status' => 'active', |
| 204 |
'item_price' => Helper::toCent($price), |
| 205 |
'compare_price' => Helper::toCent($comparePrice), |
| 206 |
'other_info' => $otherInfo, |
| 207 |
'created_at' => $createdDate |
| 208 |
]; |
| 209 |
$defaultVariationId[] = ProductVariation::query()->create($productVariationDetails); |
| 210 |
} |
| 211 |
|
| 212 |
//$getProductVariation = ProductVariation::query()->where('post_id', $createdPostId); |
| 213 |
//$price = $getProductVariation->min('item_price'); |
| 214 |
//$comparePrice = $getProductVariation->max('item_price'); |
| 215 |
|
| 216 |
$productDetails[] = [ |
| 217 |
'post_id' => $createdPostId, |
| 218 |
'fulfillment_type' => $fulfilmentType, |
| 219 |
'min_price' => min($productPrices) * 100, |
| 220 |
'max_price' => max($productComparePrices) * 100, |
| 221 |
'default_variation_id' => $defaultVariationId[0]['id'], |
| 222 |
'variation_type' => $variationType, |
| 223 |
'stock_availability' => $stockStatus, |
| 224 |
'created_at' => $createdDate |
| 225 |
]; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
ProductDetail::query()->insert($productDetails); |
| 230 |
//ProductVariation::insert($productVariationDetails); |
| 231 |
|
| 232 |
if (defined('WP_CLI') && WP_CLI) { |
| 233 |
$progress->tick(); |
| 234 |
$progress->finish(); |
| 235 |
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 236 |
echo \WP_CLI::colorize('%0%n'); |
| 237 |
} |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
public static function seedProductWithoutDetails($count) |
| 242 |
{ |
| 243 |
$faker = Factory::create(); |
| 244 |
$faker->addProvider(new \FluentCart\Database\Seeder\ProductNameProvide($faker)); |
| 245 |
|
| 246 |
$productsArray = []; |
| 247 |
for ($i = 0; $i <= $count; $i++) { |
| 248 |
$name = $faker->productName(); |
| 249 |
$productsArray[] = [ |
| 250 |
'post_title' => $name, |
| 251 |
'post_status' => 'publish', |
| 252 |
'post_type' => 'fluent-products', |
| 253 |
]; |
| 254 |
} |
| 255 |
|
| 256 |
return Product::query()->insert($productsArray); |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
//DELETE FROM wp_posts WHERE post_type = 'fluent-products'; |
| 261 |
//DELETE FROM wp_fct_product_details WHERE product_id > 0; |
| 262 |
|
| 263 |
|