start of variant values

This commit is contained in:
2025-09-10 22:06:12 -07:00
parent f3a6384c55
commit 71c038a9a0
7 changed files with 290 additions and 25 deletions

View File

@@ -50,7 +50,12 @@ class ProductSkusController extends AppController
*/
public function view($id = null)
{
$productSku = $this->ProductSkus->get($id, contain: ['Products']);
$productSku = $this->ProductSkus->get($id, contain: [
'Products',
'ProductSkuVariantValues',
'ProductSkuVariantValues.ProductCategoryVariants',
'ProductSkuVariantValues.ProductCategoryVariantOptions',
]);
$this->set(compact('productSku'));
}
@@ -91,30 +96,43 @@ class ProductSkusController extends AppController
if ($this->request->is('post')) {
$postedSkus = $this->request->getData();
$saveOptions = [
'associated' => [],
'associated' => [
'ProductSkuVariantValues',
],
];
$finalPostData = [];
$postedSkus = Hash::insert($postedSkus, '{n}.product_id', $productId);
foreach ($postedSkus as $postedSkuCnt => $postedSku) {
if ($postedSku['add'] ?? false) {
if (!isset($postedSku['add']) || !$postedSku['add']) {
unset($productSkus[$postedSkuCnt]);
continue;
}
unset($postedSkus[$postedSkuCnt]);
if ($productSkus[$postedSkuCnt] ?? false) {
unset($productSkus[$postedSkuCnt]);
}
$finalPostData[$postedSkuCnt] = $postedSku;
}
if (!$productSkus || !$postedSkus) {
$this->Flash->error('Nothing to save! Add at least one SKU next time.');
return;
}
$productSkus = $table->patchEntities($productSkus, $postedSkus, $saveOptions);
if ($table->saveManyOrFail($productSkus, $saveOptions)) {
$this->Flash->success(__(count($productSkus) . ' New SKUs have been saved.'));
$productSkus = $table->patchEntities($productSkus, $finalPostData, $saveOptions);
$errors = [];
$successes = [];
foreach ($productSkus as $productSkuToSave) {
if (!$table->save($productSkuToSave, $saveOptions)) {
Log::debug(print_r('$productSkuToSave->getErrors()', true));
Log::debug(print_r($productSkuToSave->getErrors(), true));
continue;
}
$successes[] = $productSkuToSave;
}
if ($successes) {
$this->Flash->success(__(count($successes) . ' New SKUs have been saved.'));
return $this->redirect(['action' => 'index']);
}