start of variant values
This commit is contained in:
parent
f3a6384c55
commit
71c038a9a0
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
use Migrations\BaseMigration;
|
||||
|
||||
class CreateProductSkuVariantValues extends BaseMigration
|
||||
{
|
||||
/**
|
||||
* Change Method.
|
||||
*
|
||||
* More information on this method is available here:
|
||||
* https://book.cakephp.org/migrations/4/en/migrations.html#the-change-method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function change(): void
|
||||
{
|
||||
$table = $this->table('product_sku_variant_values', ['id' => false, 'primary_key' => ['id']]);
|
||||
$table->addColumn('id', 'uuid', [
|
||||
'default' => null,
|
||||
'null' => false,
|
||||
]);
|
||||
$table->addColumn('product_sku_id', 'uuid', [
|
||||
'default' => null,
|
||||
'null' => false,
|
||||
]);
|
||||
$table->addColumn('product_category_variant_id', 'uuid', [
|
||||
'default' => null,
|
||||
'null' => false,
|
||||
]);
|
||||
$table->addColumn('product_category_variant_option_id', 'uuid', [
|
||||
'default' => null,
|
||||
'null' => false,
|
||||
]);
|
||||
$table->create();
|
||||
}
|
||||
}
|
|
@ -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']);
|
||||
}
|
||||
|
|
|
@ -0,0 +1,39 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeProducts\Model\Entity;
|
||||
|
||||
use Cake\ORM\Entity;
|
||||
|
||||
/**
|
||||
* ProductSkuVariantValue Entity
|
||||
*
|
||||
* @property string $id
|
||||
* @property string $product_sku_id
|
||||
* @property string $product_category_variant_id
|
||||
* @property string $product_category_variant_option_id
|
||||
*
|
||||
* @property ProductSku $product_sku
|
||||
* @property ProductCategoryVariant $product_category_variant
|
||||
* @property ProductCategoryVariantOption $product_category_variant_option
|
||||
*/
|
||||
class ProductSkuVariantValue extends Entity
|
||||
{
|
||||
/**
|
||||
* Fields that can be mass assigned using newEntity() or patchEntity().
|
||||
*
|
||||
* Note that when '*' is set to true, this allows all unspecified fields to
|
||||
* be mass assigned. For security purposes, it is advised to set '*' to false
|
||||
* (or remove it), and explicitly make individual fields accessible as needed.
|
||||
*
|
||||
* @var array<string, bool>
|
||||
*/
|
||||
protected array $_accessible = [
|
||||
'product_sku_id' => true,
|
||||
'product_category_variant_id' => true,
|
||||
'product_category_variant_option_id' => true,
|
||||
'product_skus' => true,
|
||||
'product_category_variant' => true,
|
||||
'product_category_variant_option' => true,
|
||||
];
|
||||
}
|
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeProducts\Model\Table;
|
||||
|
||||
use Cake\Datasource\EntityInterface;
|
||||
use Cake\Datasource\ResultSetInterface;
|
||||
use Cake\ORM\Association\BelongsTo;
|
||||
use Cake\ORM\Query\SelectQuery;
|
||||
use Cake\ORM\RulesChecker;
|
||||
use Cake\ORM\Table;
|
||||
use Cake\Validation\Validator;
|
||||
use CakeProducts\Model\Entity\ProductSkuVariantValue;
|
||||
use Closure;
|
||||
use Psr\SimpleCache\CacheInterface;
|
||||
|
||||
/**
|
||||
* ProductSkuVariantValues Model
|
||||
*
|
||||
* @property ProductSkusTable&BelongsTo $ProductSkus
|
||||
* @property ProductCategoryVariantsTable&BelongsTo $ProductCategoryVariants
|
||||
* @property ProductCategoryVariantOptionsTable&BelongsTo $ProductCategoryVariantOptions
|
||||
*
|
||||
* @method ProductSkuVariantValue newEmptyEntity()
|
||||
* @method ProductSkuVariantValue newEntity(array $data, array $options = [])
|
||||
* @method array<ProductSkuVariantValue> newEntities(array $data, array $options = [])
|
||||
* @method ProductSkuVariantValue get(mixed $primaryKey, array|string $finder = 'all', CacheInterface|string|null $cache = null, Closure|string|null $cacheKey = null, mixed ...$args)
|
||||
* @method ProductSkuVariantValue findOrCreate($search, ?callable $callback = null, array $options = [])
|
||||
* @method ProductSkuVariantValue patchEntity(EntityInterface $entity, array $data, array $options = [])
|
||||
* @method array<ProductSkuVariantValue> patchEntities(iterable $entities, array $data, array $options = [])
|
||||
* @method ProductSkuVariantValue|false save(EntityInterface $entity, array $options = [])
|
||||
* @method ProductSkuVariantValue saveOrFail(EntityInterface $entity, array $options = [])
|
||||
* @method iterable<ProductSkuVariantValue>|ResultSetInterface<ProductSkuVariantValue>|false saveMany(iterable $entities, array $options = [])
|
||||
* @method iterable<ProductSkuVariantValue>|ResultSetInterface<ProductSkuVariantValue> saveManyOrFail(iterable $entities, array $options = [])
|
||||
* @method iterable<ProductSkuVariantValue>|ResultSetInterface<ProductSkuVariantValue>|false deleteMany(iterable $entities, array $options = [])
|
||||
* @method iterable<ProductSkuVariantValue>|ResultSetInterface<ProductSkuVariantValue> deleteManyOrFail(iterable $entities, array $options = [])
|
||||
*/
|
||||
class ProductSkuVariantValuesTable extends Table
|
||||
{
|
||||
/**
|
||||
* Initialize method
|
||||
*
|
||||
* @param array<string, mixed> $config The configuration for the Table.
|
||||
* @return void
|
||||
*/
|
||||
public function initialize(array $config): void
|
||||
{
|
||||
parent::initialize($config);
|
||||
|
||||
$this->setTable('product_sku_variant_values');
|
||||
$this->setDisplayField('id');
|
||||
$this->setPrimaryKey('id');
|
||||
|
||||
$this->belongsTo('ProductSkus', [
|
||||
'className' => 'CakeProducts.ProductSkus',
|
||||
'foreignKey' => 'product_sku_id',
|
||||
'joinType' => 'INNER',
|
||||
]);
|
||||
$this->belongsTo('ProductCategoryVariants', [
|
||||
'className' => 'CakeProducts.ProductCategoryVariants',
|
||||
'foreignKey' => 'product_category_variant_id',
|
||||
'joinType' => 'INNER',
|
||||
]);
|
||||
$this->belongsTo('ProductCategoryVariantOptions', [
|
||||
'className' => 'CakeProducts.ProductCategoryVariantOptions',
|
||||
'foreignKey' => 'product_category_variant_option_id',
|
||||
'joinType' => 'INNER',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Default validation rules.
|
||||
*
|
||||
* @param Validator $validator Validator instance.
|
||||
* @return Validator
|
||||
*/
|
||||
public function validationDefault(Validator $validator): Validator
|
||||
{
|
||||
$validator
|
||||
->uuid('product_sku_id')
|
||||
->notEmptyString('product_sku_id');
|
||||
|
||||
$validator
|
||||
->uuid('product_category_variant_id')
|
||||
->notEmptyString('product_category_variant_id');
|
||||
|
||||
$validator
|
||||
->uuid('product_category_variant_option_id')
|
||||
->notEmptyString('product_category_variant_option_id');
|
||||
|
||||
return $validator;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a rules checker object that will be used for validating
|
||||
* application integrity.
|
||||
*
|
||||
* @param RulesChecker $rules The rules object to be modified.
|
||||
* @return RulesChecker
|
||||
*/
|
||||
public function buildRules(RulesChecker $rules): RulesChecker
|
||||
{
|
||||
$rules->add($rules->existsIn(['product_sku_id'], 'ProductSkus'), ['errorField' => 'product_sku_id']);
|
||||
$rules->add($rules->existsIn(['product_category_variant_id'], 'ProductCategoryVariants'), ['errorField' => 'product_category_variant_id']);
|
||||
$rules->add($rules->existsIn(['product_category_variant_option_id'], 'ProductCategoryVariantOptions'), ['errorField' => 'product_category_variant_option_id']);
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
|
@ -4,7 +4,11 @@ use function BenTools\CartesianProduct\combinations;
|
|||
|
||||
/**
|
||||
* @var \App\View\AppView $this
|
||||
* @var \App\Model\Entity\ProductSku[] $productSkus
|
||||
* @var \App\Model\Entity\ProductSku $productSku
|
||||
* @var \Cake\Collection\CollectionInterface|string[] $products
|
||||
* @var array $optionMapping
|
||||
* @var array $variantNameMapping
|
||||
* @var array $toGetCartesianProductsFrom
|
||||
*/
|
||||
|
||||
|
||||
|
@ -52,8 +56,8 @@ use function BenTools\CartesianProduct\combinations;
|
|||
$variantCnt = 0;
|
||||
foreach ($variantNameMapping as $singleVariantId => $singleVariantName) : ?>
|
||||
<td>
|
||||
<?= $this->Form->hidden($cnt . '.product_skus_variant_values.' . $variantCnt . '.product_category_variant_id', ['value' => $singleVariantId ?? null]); ?>
|
||||
<?= $this->Form->hidden($cnt . '.product_skus_variant_values.' . $variantCnt . '.product_category_variant_option_id', ['value' => $combination[$singleVariantId] ?? null]); ?>
|
||||
<?= $this->Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_category_variant_id', ['value' => $singleVariantId ?? null]); ?>
|
||||
<?= $this->Form->hidden($cnt . '.product_sku_variant_values.' . $variantCnt . '.product_category_variant_option_id', ['value' => $combination[$singleVariantId] ?? null]); ?>
|
||||
<?= $optionMapping[$combination[$singleVariantId]]; ?>
|
||||
</td>
|
||||
<?php
|
||||
|
|
|
@ -8,10 +8,10 @@
|
|||
<aside class="column">
|
||||
<div class="side-nav">
|
||||
<h4 class="heading"><?= __('Actions') ?></h4>
|
||||
<?= $this->Html->link(__('Edit Product Skus'), ['action' => 'edit', $productSku->id], ['class' => 'side-nav-item']) ?>
|
||||
<?= $this->Form->postLink(__('Delete Product Skus'), ['action' => 'delete', $productSku->id], ['confirm' => __('Are you sure you want to delete # {0}?', $productSku->id), 'class' => 'side-nav-item']) ?>
|
||||
<?= $this->Html->link(__('List Product Skus'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
|
||||
<?= $this->Html->link(__('New Product Skus'), ['action' => 'add'], ['class' => 'side-nav-item']) ?>
|
||||
<?= $this->Html->link(__('Edit Product SKU'), ['action' => 'edit', $productSku->id], ['class' => 'side-nav-item']) ?>
|
||||
<?= $this->Form->postLink(__('Delete Product SKU'), ['action' => 'delete', $productSku->id], ['confirm' => __('Are you sure you want to delete # {0}?', $productSku->id), 'class' => 'side-nav-item']) ?>
|
||||
<?= $this->Html->link(__('List Product SKU'), ['action' => 'index'], ['class' => 'side-nav-item']) ?>
|
||||
<?= $this->Html->link(__('New Product SKU'), ['action' => 'add'], ['class' => 'side-nav-item']) ?>
|
||||
</div>
|
||||
</aside>
|
||||
<div class="column column-80">
|
||||
|
@ -19,12 +19,8 @@
|
|||
<h3><?= h($productSku->sku) ?></h3>
|
||||
<table>
|
||||
<tr>
|
||||
<th><?= __('Id') ?></th>
|
||||
<td><?= h($productSku->id) ?></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><?= __('Product Id') ?></th>
|
||||
<td><?= h($productSku->product_id) ?></td>
|
||||
<th><?= __('Product') ?></th>
|
||||
<td><?= $productSku->hasValue('product') ? $this->Html->link($productSku->product->name, ['controller' => 'Products', 'action' => 'view', $productSku->product->id]) : '' ?></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th><?= __('Sku') ?></th>
|
||||
|
@ -55,6 +51,31 @@
|
|||
<td><?= h($productSku->deleted) ?></td>
|
||||
</tr>
|
||||
</table>
|
||||
<div class="related">
|
||||
<h4><?= __('Product Attributes') ?></h4>
|
||||
<?php if (!empty($productSku->product_sku_variant_values)) : ?>
|
||||
<div class="table-responsive">
|
||||
<table>
|
||||
<?php foreach ($productSku->product_sku_variant_values as $variantValue) : ?>
|
||||
<?php
|
||||
if (!$variantValue->hasValue('product_category_variant')) {
|
||||
continue;
|
||||
}
|
||||
if (!$variantValue->hasValue('product_category_variant_option')) {
|
||||
continue;
|
||||
}
|
||||
?>
|
||||
|
||||
<tr>
|
||||
<th><?= h($variantValue->product_category_variant->name) ?></th>
|
||||
<td><?= h($variantValue->product_category_variant_option->variant_label ?? $variantValue->product_category_variant_option->variant_value) ?></td>
|
||||
</tr>
|
||||
|
||||
<?php endforeach; ?>
|
||||
</table>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace CakeProducts\Test\Fixture;
|
||||
|
||||
use Cake\TestSuite\Fixture\TestFixture;
|
||||
|
||||
/**
|
||||
* ProductSkuVariantValuesFixture
|
||||
*/
|
||||
class ProductSkuVariantValuesFixture extends TestFixture
|
||||
{
|
||||
/**
|
||||
* Table name
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public string $table = 'product_sku_variant_values';
|
||||
|
||||
/**
|
||||
* Init method
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function init(): void
|
||||
{
|
||||
$this->records = [
|
||||
[
|
||||
'id' => '98b609d8-1d4f-484c-a13a-6adb7102da56',
|
||||
'product_sku_id' => '3a477e3e-7977-4813-81f6-f85949613979',
|
||||
'product_category_variant_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d93',
|
||||
'product_category_variant_option_id' => '5a386e9f-6e7a-4ae7-9360-c8e529f78d23',
|
||||
],
|
||||
];
|
||||
parent::init();
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue