require cartesian product library, product skus add update

This commit is contained in:
2025-09-05 02:51:43 -07:00
parent 9d9c32f0b9
commit 43a7805b5e
4 changed files with 87 additions and 34 deletions

View File

@@ -4,8 +4,10 @@ declare(strict_types=1);
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;
/**
* ProductSkus Controller
@@ -58,30 +60,52 @@ class ProductSkusController extends AppController
*
* @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
*/
public function add()
public function add($productId = null)
{
$productSku = $this->ProductSkus->newEmptyEntity();
$toGetCartesianProductsFrom = [];
$productSkus = [];
$table = $this->getTable();
$productCategoryVariants = $table->Products->ProductCategoryVariants->find()
->contain(['ProductCategoryVariantOptions'])
->where(['product_id' => $productId])
->toArray();
$optionMapping = Hash::combine($productCategoryVariants, '{n}.product_category_variant_options.{n}.id', '{n}.product_category_variant_options.{n}.variant_value');
$variantNameMapping = Hash::combine($productCategoryVariants, '{n}.id', '{n}.name');
foreach ($productCategoryVariants as $productCategoryVariant) {
$options = Hash::extract($productCategoryVariant->product_category_variant_options ?? [], '{n}.id');
$toGetCartesianProductsFrom[$productCategoryVariant->id] = $options;
}
$numSkusToAdd = count(combinations($toGetCartesianProductsFrom));
for ($i = 0; $i < $numSkusToAdd; $i++) {
$productSkus[$i] = $this->getTable()->newEmptyEntity();
}
if ($this->request->is('post')) {
$postData = $this->request->getData();
$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));
$productSku = $this->ProductSkus->patchEntity($productSku, $postData, $saveOptions);
if ($this->ProductSkus->save($productSku)) {
$this->Flash->success(__('The product sku has been saved.'));
$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']);
}
Log::debug(print_r('$productSku->getErrors() next - failed in productSkus/add', true));
Log::debug(print_r($productSku->getErrors(), true));
$this->Flash->error(__('The product skus could not be saved. Please, try again.'));
$this->Flash->error(__('The product SKU(s) could not be saved. Please, try again.'));
}
$products = $this->ProductSkus->Products->find('list', limit: 200)->all();
$this->set(compact('productSku', 'products'));
$this->set(compact(
'productSkus',
'productCategoryVariants',
'toGetCartesianProductsFrom',
'optionMapping',
'variantNameMapping',
'numSkusToAdd'
));
}
/**