| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentCart\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentCart\Api\Resource\ProductVariationResource; |
| 6 |
use FluentCart\App\Http\Requests\ProductVariationRequest; |
| 7 |
use FluentCart\App\Models\Product; |
| 8 |
use FluentCart\App\Models\ProductVariation; |
| 9 |
use FluentCart\Framework\Http\Request\Request; |
| 10 |
use FluentCart\Framework\Support\Arr; |
| 11 |
|
| 12 |
class ProductVariationController extends Controller |
| 13 |
{ |
| 14 |
public function index(Request $request): array |
| 15 |
{ |
| 16 |
// |
| 17 |
|
| 18 |
$parameters = $request->get('params'); |
| 19 |
$variants = ProductVariationResource::get($parameters); |
| 20 |
|
| 21 |
return [ |
| 22 |
'variants' => $variants['variants'], |
| 23 |
]; |
| 24 |
} |
| 25 |
|
| 26 |
public function find(Request $request, ProductVariation $product): array |
| 27 |
{ |
| 28 |
// |
| 29 |
} |
| 30 |
|
| 31 |
public function create(ProductVariationRequest $request) |
| 32 |
{ |
| 33 |
|
| 34 |
$data = $request->getSafe($request->sanitize()); |
| 35 |
$productId = Arr::get($data, 'variants.post_id'); |
| 36 |
|
| 37 |
|
| 38 |
$product = Product::query()->with('detail')->findOrFail($productId); |
| 39 |
|
| 40 |
$variationData = Arr::get($data, 'variants', []); |
| 41 |
$variationData['other_info']['is_bundle_product'] = $product->isBundleProduct()?'yes':'no'; |
| 42 |
$variationData['detail_id'] = Arr::get($product, 'detail.id', null); |
| 43 |
|
| 44 |
$isCreated = ProductVariationResource::create($variationData); |
| 45 |
|
| 46 |
if (is_wp_error($isCreated)) { |
| 47 |
return $isCreated; |
| 48 |
} |
| 49 |
return $this->response->sendSuccess($isCreated); |
| 50 |
} |
| 51 |
|
| 52 |
public function update(ProductVariationRequest $request, $variantId) |
| 53 |
{ |
| 54 |
|
| 55 |
$data = $request->getSafe($request->sanitize()); |
| 56 |
|
| 57 |
$productId = Arr::get($data, 'variants.post_id'); |
| 58 |
|
| 59 |
$product = Product::query()->with('detail')->findOrFail($productId); |
| 60 |
|
| 61 |
$isUpdated = ProductVariationResource::update( |
| 62 |
Arr::get($data, 'variants', []), |
| 63 |
$variantId, |
| 64 |
[ |
| 65 |
'detail_id' => Arr::get($product, 'detail.id', null) |
| 66 |
]); |
| 67 |
|
| 68 |
if (is_wp_error($isUpdated)) { |
| 69 |
return $isUpdated; |
| 70 |
} |
| 71 |
return $this->response->sendSuccess($isUpdated); |
| 72 |
} |
| 73 |
|
| 74 |
public function delete(Request $request, $variantId) |
| 75 |
{ |
| 76 |
|
| 77 |
$isDeleted = ProductVariationResource::delete($variantId); |
| 78 |
|
| 79 |
if (is_wp_error($isDeleted)) { |
| 80 |
return $isDeleted; |
| 81 |
} |
| 82 |
return $this->response->sendSuccess($isDeleted); |
| 83 |
} |
| 84 |
|
| 85 |
public function setMedia(Request $request, $variantId) |
| 86 |
{ |
| 87 |
|
| 88 |
$data = $request->getSafe([ |
| 89 |
'media.*.id' => 'intval', |
| 90 |
'media.*.title' => 'sanitize_text_field', |
| 91 |
'media.*.url' => function ($value) { |
| 92 |
if (empty($value)) { |
| 93 |
return ''; |
| 94 |
} |
| 95 |
|
| 96 |
return sanitize_url($value); |
| 97 |
}, |
| 98 |
]); |
| 99 |
$isSetMedia = ProductVariationResource::setImage(Arr::get($data, 'media', []), $variantId); |
| 100 |
|
| 101 |
if (is_wp_error($isSetMedia)) { |
| 102 |
return $isSetMedia; |
| 103 |
} |
| 104 |
return $this->response->sendSuccess($isSetMedia); |
| 105 |
} |
| 106 |
|
| 107 |
public function updatePricingTable(Request $request, $variantId) |
| 108 |
{ |
| 109 |
|
| 110 |
// Use sanitize_textarea_field to retain newlines |
| 111 |
$data['description'] = sanitize_textarea_field($request->get('description')); |
| 112 |
|
| 113 |
$isUpdated = ProductVariationResource::updatePricingTable($data, $variantId); |
| 114 |
|
| 115 |
if (is_wp_error($isUpdated)) { |
| 116 |
return $isUpdated; |
| 117 |
} |
| 118 |
return $this->response->sendSuccess($isUpdated); |
| 119 |
} |
| 120 |
} |
| 121 |
|