PluginProbe
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler / 1.4.1
FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler v1.4.1
1.6.6 1.6.5 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 All 49 releases
fluent-cart / app / Http / Controllers / ProductVariationController.php

ProductVariationController.php in FluentCart A New Era of eCommerce – Faster, Lighter, and Simpler 1.4.1, at app/Http/Controllers/ProductVariationController.php

167 lines 5.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\App\Models\TaxClass;
10 use FluentCart\Framework\Http\Request\Request;
11 use FluentCart\Framework\Support\Arr;
12
13 class ProductVariationController extends Controller
14 {
15 public function index(Request $request): array
16 {
17 //
18
19 $parameters = $request->get('params');
20 $variants = ProductVariationResource::get($parameters);
21
22 return [
23 'variants' => $variants['variants'],
24 ];
25 }
26
27 public function find(Request $request, ProductVariation $product): array
28 {
29 return [];
30 }
31
32 public function create(ProductVariationRequest $request)
33 {
34
35 $data = $request->getSafe($request->sanitize());
36 $productId = Arr::get($data, 'variants.post_id');
37
38
39 $product = Product::query()->with('detail')->findOrFail($productId);
40
41 $variationData = Arr::get($data, 'variants', []);
42 $variationData['other_info']['is_bundle_product'] = $product->isBundleProduct()?'yes':'no';
43 $variationData['detail_id'] = Arr::get($product, 'detail.id', null);
44
45 $isCreated = ProductVariationResource::create($variationData);
46
47 if (is_wp_error($isCreated)) {
48 return $isCreated;
49 }
50 return $this->response->sendSuccess($isCreated);
51 }
52
53 public function update(ProductVariationRequest $request, $variantId)
54 {
55
56 $data = $request->getSafe($request->sanitize());
57
58 $productId = Arr::get($data, 'variants.post_id');
59
60 $product = Product::query()->with('detail')->findOrFail($productId);
61
62 $isUpdated = ProductVariationResource::update(
63 Arr::get($data, 'variants', []),
64 $variantId,
65 [
66 'detail_id' => Arr::get($product, 'detail.id', null)
67 ]);
68
69 if (is_wp_error($isUpdated)) {
70 return $isUpdated;
71 }
72 return $this->response->sendSuccess($isUpdated);
73 }
74
75 public function updateTaxSettings(Request $request, $variantId)
76 {
77 $variant = ProductVariation::query()->find($variantId);
78
79 if (!$variant) {
80 return $this->sendError([
81 'message' => __('Variant not found', 'fluent-cart')
82 ]);
83 }
84
85 $taxExempt = sanitize_text_field($request->get('tax_exempt', 'no'));
86 $taxClassSlug = sanitize_text_field($request->get('tax_class', ''));
87 $otherInfo = $variant->other_info ?: [];
88
89 if (!$taxClassSlug) {
90 $taxClassSlug = sanitize_text_field(Arr::get($otherInfo, 'tax_class', 'standard'));
91 }
92
93 if (!$taxClassSlug) {
94 $taxClassSlug = 'standard';
95 }
96
97 if (!TaxClass::query()->where('slug', $taxClassSlug)->exists()) {
98 return $this->sendError([
99 'message' => __('Invalid tax class', 'fluent-cart')
100 ], 422);
101 }
102
103 $otherInfo['tax_exempt'] = $taxExempt === 'yes' ? 'yes' : 'no';
104 $otherInfo['tax_class'] = $taxClassSlug;
105
106 $variant->update([
107 'other_info' => $otherInfo
108 ]);
109
110 return $this->sendSuccess([
111 'message' => $otherInfo['tax_exempt'] === 'yes'
112 ? __('Variation is now tax exempt', 'fluent-cart')
113 : __('Tax will be charged on this variation', 'fluent-cart'),
114 'tax_exempt' => $otherInfo['tax_exempt'],
115 'tax_class' => $otherInfo['tax_class'],
116 'tax_class_slug' => $otherInfo['tax_class']
117 ]);
118 }
119
120 public function delete(Request $request, $variantId)
121 {
122
123 $isDeleted = ProductVariationResource::delete($variantId);
124
125 if (is_wp_error($isDeleted)) {
126 return $isDeleted;
127 }
128 return $this->response->sendSuccess($isDeleted);
129 }
130
131 public function setMedia(Request $request, $variantId)
132 {
133
134 $data = $request->getSafe([
135 'media.*.id' => 'intval',
136 'media.*.title' => 'sanitize_text_field',
137 'media.*.url' => function ($value) {
138 if (empty($value)) {
139 return '';
140 }
141
142 return sanitize_url($value);
143 },
144 ]);
145 $isSetMedia = ProductVariationResource::setImage(Arr::get($data, 'media', []), $variantId);
146
147 if (is_wp_error($isSetMedia)) {
148 return $isSetMedia;
149 }
150 return $this->response->sendSuccess($isSetMedia);
151 }
152
153 public function updatePricingTable(Request $request, $variantId)
154 {
155
156 // Use sanitize_textarea_field to retain newlines
157 $data['description'] = sanitize_textarea_field($request->get('description'));
158
159 $isUpdated = ProductVariationResource::updatePricingTable($data, $variantId);
160
161 if (is_wp_error($isUpdated)) {
162 return $isUpdated;
163 }
164 return $this->response->sendSuccess($isUpdated);
165 }
166 }
167