| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Helpers; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\ProductDownloadResource; |
| 6 |
use FluentCart\Api\Resource\ProductVariationResource; |
| 7 |
use FluentCart\App\Models\AttributeRelation; |
| 8 |
use FluentCart\App\Models\AttributeTerm; |
| 9 |
use FluentCart\App\Models\ProductVariation; |
| 10 |
use FluentCart\App\Services\Helpers; |
| 11 |
use FluentCart\App\Vite; |
| 12 |
use FluentCart\Framework\Support\Arr; |
| 13 |
|
| 14 |
class ProductAdminHelper |
| 15 |
{ |
| 16 |
|
| 17 |
/** |
| 18 |
* |
| 19 |
* @param $details |
| 20 |
* @param $variants |
| 21 |
*/ |
| 22 |
public static function syncProduct($details, $variants) |
| 23 |
{ |
| 24 |
$variationIds = []; |
| 25 |
$variationType = Arr::get($details, 'variation_type', ''); |
| 26 |
$postId = Arr::get($details, 'post_id'); |
| 27 |
$variants = Arr::except($variants, ['*']); |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
foreach ($variants as $index => $variant) { |
| 32 |
$variant['serial_index'] = $index + 1; |
| 33 |
$variant['fulfillment_type'] = Arr::get( |
| 34 |
$variant, 'fulfillment_type', Arr::get($details, 'fulfillment_type') |
| 35 |
); |
| 36 |
$variantId = Arr::get($variant, 'id'); |
| 37 |
if (empty($variantId)) { |
| 38 |
$result = ProductVariationResource::create($variant); |
| 39 |
} else { |
| 40 |
$result = ProductVariationResource::update($variant, $variantId); |
| 41 |
} |
| 42 |
|
| 43 |
$variationIds[] = Arr::get($result, 'data.id'); |
| 44 |
$variants[$index]['id'] = Arr::get($result, 'data.id'); |
| 45 |
if ($variationType === \FluentCart\App\Helpers\Helper::PRODUCT_TYPE_SIMPLE) { |
| 46 |
break; |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
self::deleteOrphanVariant($postId, $variationIds); |
| 51 |
|
| 52 |
ProductDownloadResource::delete(null, ['type' => 'byProduct', 'post_id' => $postId]); |
| 53 |
return ProductVariation::query()->where('post_id', $postId)->get(); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Syncing advance variations |
| 58 |
* |
| 59 |
* @param $srcDetails |
| 60 |
* @param $variations |
| 61 |
* @param array $variantProductDetails |
| 62 |
* @return mixed |
| 63 |
*/ |
| 64 |
public static function syncAdvanceVariations($srcDetails, $variations, array $variantProductDetails = []) |
| 65 |
{ |
| 66 |
$formattedVariations = []; |
| 67 |
|
| 68 |
foreach ($variations as $variation) { |
| 69 |
if (!empty($variation['variants'])) { |
| 70 |
$formattedVariations[] = $variation['variants']; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
$variants = self::generateVariationSets($formattedVariations); |
| 75 |
|
| 76 |
$variantIds = []; |
| 77 |
|
| 78 |
$srcDetails->load('product'); |
| 79 |
|
| 80 |
$variationTitle = $srcDetails->product->post_title; |
| 81 |
|
| 82 |
foreach ($variants as $index => $variant) { |
| 83 |
|
| 84 |
asort($variant, SORT_NUMERIC); |
| 85 |
|
| 86 |
$variationIdentifier = implode('_', $variant); |
| 87 |
|
| 88 |
$variantData = [ |
| 89 |
'post_id' => $srcDetails->post_id, |
| 90 |
'serial_index' => $index + 1, |
| 91 |
'stock' => 100, |
| 92 |
'item_price' => 0, |
| 93 |
'fulfillment_type' => 'physical', |
| 94 |
'variation_title' => $variationTitle, |
| 95 |
'variation_identifier' => $variationIdentifier, |
| 96 |
'other_info' => [ |
| 97 |
'variant' => array_values($variant), |
| 98 |
], |
| 99 |
]; |
| 100 |
|
| 101 |
$exist = ProductVariation::query()->where('post_id', $srcDetails->post_id) |
| 102 |
->where('variation_identifier', $variationIdentifier) |
| 103 |
->first(); |
| 104 |
|
| 105 |
if ($exist) { |
| 106 |
|
| 107 |
$exist->serial_index = $index + 1; |
| 108 |
$exist->save(); |
| 109 |
|
| 110 |
} else { |
| 111 |
|
| 112 |
$exist = ProductVariation::create($variantData); |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
foreach ($variant as $termId) { |
| 117 |
$term = AttributeTerm::find($termId); |
| 118 |
|
| 119 |
$relation = [ |
| 120 |
'term_id' => $term->id, |
| 121 |
'object_id' => $exist->id, |
| 122 |
'group_id' => $term->group_id, |
| 123 |
]; |
| 124 |
|
| 125 |
AttributeRelation::firstOrCreate($relation); |
| 126 |
} |
| 127 |
|
| 128 |
$variantIds[] = $exist->id; |
| 129 |
} |
| 130 |
|
| 131 |
/* |
| 132 |
* Remove orphan variants |
| 133 |
*/ |
| 134 |
self::deleteOrphanVariant($srcDetails->post_id, $variantIds); |
| 135 |
|
| 136 |
return ProductVariation::query()->whereIn('id', $variantIds)->get(); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
/** |
| 141 |
* |
| 142 |
* @param $productId |
| 143 |
* @param array $childrenIdsWeWantToKeepSafe |
| 144 |
* @return mixed |
| 145 |
*/ |
| 146 |
public static function deleteOrphanVariant($productId, array $childrenIdsWeWantToKeepSafe = []) |
| 147 |
{ |
| 148 |
$variations = ProductVariation::query() |
| 149 |
->select('variation_title') |
| 150 |
->where('post_id', $productId) |
| 151 |
->whereNotIn('id', $childrenIdsWeWantToKeepSafe) |
| 152 |
->get(); |
| 153 |
|
| 154 |
$variationTitles = $variations->pluck('variation_title') |
| 155 |
->join(',', __(' and ', 'fluent-cart')); |
| 156 |
|
| 157 |
if ($variations->count()) { |
| 158 |
fluent_cart_success_log( |
| 159 |
sprintf( |
| 160 |
/* translators: %s is the number of variations */ |
| 161 |
__('%s Pricing deleted', 'fluent-cart'), |
| 162 |
$variations->count() |
| 163 |
), |
| 164 |
sprintf( |
| 165 |
/* translators: %s is the variation titles */ |
| 166 |
_n( |
| 167 |
"%s Pricing is deleted, while product variation is changed to 'Simple'", |
| 168 |
"%s Pricing's are deleted, while product variation is changed to 'Simple'", |
| 169 |
$variations->count(), |
| 170 |
'fluent-cart' |
| 171 |
), |
| 172 |
$variationTitles |
| 173 |
), |
| 174 |
[ |
| 175 |
'module_name' => 'Product', |
| 176 |
'module_id' => 0, |
| 177 |
'module_type' => ProductVariation::class, |
| 178 |
] |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
return ProductVariation::query() |
| 184 |
->select('id') |
| 185 |
->where('post_id', $productId) |
| 186 |
->whereNotIn('id', $childrenIdsWeWantToKeepSafe) |
| 187 |
->delete(); |
| 188 |
} |
| 189 |
|
| 190 |
public static function generateVariationSets($formattedVariations, $i = 0) |
| 191 |
{ |
| 192 |
if (!isset($formattedVariations[$i])) { |
| 193 |
return []; |
| 194 |
} |
| 195 |
|
| 196 |
$result = []; |
| 197 |
|
| 198 |
/** |
| 199 |
* Fix: With only one variation group it does not give proper result. |
| 200 |
* |
| 201 |
*/ |
| 202 |
if ($i === 0 && count($formattedVariations) === 1 && is_array($formattedVariations[0])) { |
| 203 |
|
| 204 |
foreach ($formattedVariations[0] as $item) { |
| 205 |
$result[] = [$item]; |
| 206 |
} |
| 207 |
|
| 208 |
return $result; |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
if ($i == count($formattedVariations) - 1) { |
| 213 |
return $formattedVariations[$i]; |
| 214 |
} |
| 215 |
|
| 216 |
// get combinations from subsequent arrays |
| 217 |
$tmp = self::generateVariationSets($formattedVariations, $i + 1); |
| 218 |
|
| 219 |
|
| 220 |
// concat each array from tmp with each element from $arrays[$i] |
| 221 |
foreach ($formattedVariations[$i] as $v) { |
| 222 |
foreach ($tmp as $t) { |
| 223 |
$result[] = is_array($t) ? |
| 224 |
array_merge([$v], $t) : |
| 225 |
[$v, $t]; |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
return $result; |
| 230 |
} |
| 231 |
|
| 232 |
public static function getFeaturedMedia($featuredMedia): string |
| 233 |
{ |
| 234 |
return !empty($featuredMedia) ? Arr::get($featuredMedia, 'url') : Vite::getAssetUrl('images/placeholder.svg'); |
| 235 |
} |
| 236 |
} |
| 237 |
|