start of variant values
This commit is contained in:
@@ -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']);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user