product attributes on edit product
CI / testsuite (mysql, 8.2, ) (push) Waiting to run Details
CI / testsuite (mysql, 8.4, ) (push) Waiting to run Details
CI / testsuite (sqlite, 8.2, prefer-lowest) (push) Waiting to run Details
CI / Coding Standard & Static Analysis (push) Waiting to run Details

This commit is contained in:
Brandon Shipley 2025-09-19 23:34:04 -07:00
parent f0f9ebeb8c
commit 25cb12d810
Signed by: bmfs
GPG Key ID: 14E38571D8BB0DE4
3 changed files with 74 additions and 7 deletions

View File

@ -107,9 +107,16 @@ class ProductsController extends AppController
public function edit($id = null) public function edit($id = null)
{ {
$productsTable = $this->getTable(); $productsTable = $this->getTable();
$product = $productsTable->get($id, contain: []); $product = $productsTable->get($id, contain: [
'ProductAttributes',
'ProductAttributes.ProductCategoryAttributes',
'ProductAttributes.ProductCategoryAttributes.ProductCategoryAttributeOptions',
]);
if ($this->request->is(['patch', 'post', 'put'])) { if ($this->request->is(['patch', 'post', 'put'])) {
$product = $productsTable->patchEntity($product, $this->request->getData()); $saveOptions = [
'associated' => ['ProductAttributes'],
];
$product = $productsTable->patchEntity($product, $this->request->getData(), $saveOptions);
if ($productsTable->save($product)) { if ($productsTable->save($product)) {
$this->Flash->success(__('The product has been saved.')); $this->Flash->success(__('The product has been saved.'));
@ -122,6 +129,7 @@ class ProductsController extends AppController
$productCategory = $product->product_category_id ? $productsTable->ProductCategories->find()->where(['internal_id' => $product->product_category_id])->first() : null; $productCategory = $product->product_category_id ? $productsTable->ProductCategories->find()->where(['internal_id' => $product->product_category_id])->first() : null;
$productCatalogs = $productsTable->ProductCategories->ProductCatalogs->find('list')->toArray(); $productCatalogs = $productsTable->ProductCategories->ProductCatalogs->find('list')->toArray();
$this->set(compact('product', 'productCatalogs', 'productCategory')); $this->set(compact('product', 'productCatalogs', 'productCategory'));
} }
/** /**

View File

@ -0,0 +1,42 @@
<?php
/**
* @var int $cnt
* @var \App\Model\Entity\ProductCategoryAttribute|\Cake\Datasource\EntityInterface $productCategoryAttribute
* @var \App\Model\Entity\ProductAttribute|\Cake\Datasource\EntityInterface|null $productAttribute
*/
use Cake\Utility\Hash;
use CakeProducts\Model\Enum\ProductCategoryAttributeTypeId;
if (($productAttribute ?? false)) {
echo $this->Form->hidden('product_attributes.' . $cnt . '.id', ['value' => $productAttribute->id]);
}
echo $this->Form->hidden('product_attributes.' . $cnt . '.product_category_attribute_id', [
'value' => $productCategoryAttribute->id,
]);
$inputType = 'text';
$options = [];
if ($productCategoryAttribute->attribute_type_id === ProductCategoryAttributeTypeId::Integer) {
$inputType = 'number';
}
if ($productCategoryAttribute->attribute_type_id === ProductCategoryAttributeTypeId::Constrained) {
echo $this->Form->hidden('product_attributes.' . $cnt . '.attribute_value', [
'value' => '',
]);
$options = !empty($productCategoryAttribute->product_category_attribute_options) ?
Hash::combine($productCategoryAttribute->product_category_attribute_options, '{n}.id', '{n}.attribute_label') :
[];
echo $this->Form->control('product_attributes.' . $cnt . '.product_category_attribute_option_id', [
'type' => 'select',
'label' => $productCategoryAttribute->name,
'options' => $options,
'value' => $productAttribute->product_category_attribute_option_id ?? '',
]);
} else {
echo $this->Form->control('product_attributes.' . $cnt . '.attribute_value', [
'type' => $inputType,
'label' => $productCategoryAttribute->name,
]);
}
?>

View File

@ -40,15 +40,32 @@ echo $this->Form->control('product_type_id');
?> ?>
<hr> <hr>
<h4 class="mb-4">Product Attributes</h4> <h4 class="mb-4">Product Attributes</h4>
<div id="product-attributes-container" class="container"> <div
<?php if (false && $product->hasValue('product_attributes')) : ?> id="product-attributes-container"
class="container"
<?php if ($product->isNew()) : ?>
hx-get="<?= $this->Url->build([
'controller' => 'ProductCategoryAttributes',
'action' => 'form',
]); ?>"
hx-trigger="change target:#product_category_id from:body"
hx-target="this"
hx-include="#form, #product_category_id"
hx-swap="innerHTML"
<?php endif; ?>
>
<?php if ($product->hasValue('product_attributes')) : ?>
<?php <?php
$cnt = 0; $cnt = 0;
foreach ($product->product_attributes as $productAttribute) { foreach ($product->product_attributes as $productAttribute) {
$prefix = 'product_attributes.' . $cnt . '.'; $prefix = 'product_attributes.' . $cnt . '.';
echo '<hr class="my-2">'; echo $this->element('ProductCategoryAttributes/product_form', [
'productCategoryAttribute' => $productAttribute->hasValue('product_category_attribute') ? $productAttribute->product_category_attribute : null,
'productAttribute' => $productAttribute,
'cnt' => $cnt,
]);
$cnt++; $cnt++;
} ?> } ?>
<?php endif; ?> <?php endif; ?>