test fixes for product skus add

This commit is contained in:
2025-09-06 01:11:51 -07:00
parent 99f52422c1
commit f3a6384c55
8 changed files with 92 additions and 43 deletions

View File

@@ -5,7 +5,6 @@ namespace CakeProducts\Controller;
use Cake\Log\Log;
use Cake\Utility\Hash;
use CakeProducts\Controller\AppController;
use CheeseCake\Controller\Traits\OverrideTableTrait;
use function BenTools\CartesianProduct\combinations;
@@ -80,24 +79,6 @@ class ProductSkusController extends AppController
for ($i = 0; $i < $numSkusToAdd; $i++) {
$productSkus[$i] = $this->getTable()->newEmptyEntity();
}
if ($this->request->is('post')) {
$postData = $this->request->getData('skus', []);
$saveOptions = [
'associated' => [],
];
$postData = Hash::insert($postData, '{n}.product_id', $productId);
// Log::debug(print_r('$postData', true));
// Log::debug(print_r($postData, true));
// Log::debug(print_r('$saveOptions', true));
// Log::debug(print_r($saveOptions, true));
$productSkus = $table->patchEntities($productSkus, $postData, $saveOptions);
if ($table->saveManyOrFail($productSkus)) {
$this->Flash->success(__('The product SKU(s) have been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The product SKU(s) could not be saved. Please, try again.'));
}
$this->set(compact(
'productSkus',
'productCategoryVariants',
@@ -106,6 +87,43 @@ class ProductSkusController extends AppController
'variantNameMapping',
'numSkusToAdd'
));
if ($this->request->is('post')) {
$postedSkus = $this->request->getData();
$saveOptions = [
'associated' => [],
];
$postedSkus = Hash::insert($postedSkus, '{n}.product_id', $productId);
foreach ($postedSkus as $postedSkuCnt => $postedSku) {
if ($postedSku['add'] ?? false) {
continue;
}
unset($postedSkus[$postedSkuCnt]);
if ($productSkus[$postedSkuCnt] ?? false) {
unset($productSkus[$postedSkuCnt]);
}
}
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.'));
return $this->redirect(['action' => 'index']);
}
$this->Flash->error(__('The product SKU(s) could not be saved. Please, try again.'));
}
$this->set(compact(
'productSkus'
));
}
/**