Skip to content

Commit 30b622c

Browse files
physical information box in admin trait
1 parent 0143ca0 commit 30b622c

File tree

2 files changed

+76
-40
lines changed

2 files changed

+76
-40
lines changed

app/base/traits/AdminFormTrait.php

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use App\Base\Abstracts\Models\BaseModel;
1717
use App\Base\Abstracts\Models\FrontendModel;
18+
use App\Base\Interfaces\Model\PhysicalProductInterface;
1819
use Degami\Basics\Exceptions\BasicException;
1920
use Degami\PHPFormsApi as FAPI;
2021
use DI\DependencyException;
@@ -283,4 +284,74 @@ protected function addSeoFormElements(FAPI\Form $form, &$form_state): FAPI\Form
283284

284285
return $form;
285286
}
287+
288+
/**
289+
* adds Physical Product elements to form
290+
*
291+
* @param FAPI\Form $form
292+
* @param $form_state
293+
* @return FAPI\Form
294+
* @throws FAPI\Exceptions\FormException
295+
*/
296+
protected function addPhysicalProductFormElements(FAPI\Form $form, &$form_state): FAPI\Form
297+
{
298+
$object = $this->getObject();
299+
300+
$product_weight = $product_length = $product_width = $product_height = 0.0;
301+
if ($object instanceof PhysicalProductInterface && $object instanceof FrontendModel && $object->isLoaded()) {
302+
$product_weight = $object->weight;
303+
$product_length = $object->length;
304+
$product_width = $object->width;
305+
$product_height = $object->height;
306+
}
307+
308+
/** @var Fieldset $fieldset */
309+
$fieldset = $form->addField('physical', [
310+
'type' => 'fieldset',
311+
'title' => 'Physical Informations',
312+
'collapsible' => true,
313+
'collapsed' => false,
314+
]);
315+
316+
$fieldset
317+
->addField('weight', [
318+
'type' => 'number',
319+
'title' => 'Weight',
320+
'default_value' => $product_weight,
321+
'min' => '0.00',
322+
'max' => '1000000.00',
323+
'step' => '0.01',
324+
'description' => 'in Kilograms',
325+
'validate' => ['required'],
326+
])->addField('length', [
327+
'type' => 'number',
328+
'title' => 'Box Length',
329+
'default_value' => $product_length,
330+
'min' => '0.00',
331+
'max' => '1000000.00',
332+
'step' => '0.01',
333+
'description' => 'in cm',
334+
'validate' => ['required'],
335+
])->addField('width', [
336+
'type' => 'number',
337+
'title' => 'Box Width',
338+
'default_value' => $product_width,
339+
'min' => '0.00',
340+
'max' => '1000000.00',
341+
'step' => '0.01',
342+
'description' => 'in cm',
343+
'validate' => ['required'],
344+
])->addField('height', [
345+
'type' => 'number',
346+
'title' => 'Box Height',
347+
'default_value' => $product_height,
348+
'min' => '0.00',
349+
'max' => '1000000.00',
350+
'step' => '0.01',
351+
'description' => 'in cm',
352+
'validate' => ['required'],
353+
]);
354+
355+
return $form;
356+
}
286357
}

app/site/controllers/Admin/Commerce/Books.php

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -118,42 +118,6 @@ public function getFormDefinition(FAPI\Form $form, array &$form_state): FAPI\For
118118
'tinymce_options' => DEFAULT_TINYMCE_OPTIONS,
119119
'default_value' => $product_content,
120120
'rows' => 20,
121-
])->addField('weight', [
122-
'type' => 'number',
123-
'title' => 'Weight',
124-
'default_value' => $product_weight,
125-
'min' => '0.00',
126-
'max' => '1000000.00',
127-
'step' => '0.01',
128-
'description' => 'in Kilograms',
129-
'validate' => ['required'],
130-
])->addField('length', [
131-
'type' => 'number',
132-
'title' => 'Box Length',
133-
'default_value' => $product_length,
134-
'min' => '0.00',
135-
'max' => '1000000.00',
136-
'step' => '0.01',
137-
'description' => 'in cm',
138-
'validate' => ['required'],
139-
])->addField('width', [
140-
'type' => 'number',
141-
'title' => 'Box Width',
142-
'default_value' => $product_width,
143-
'min' => '0.00',
144-
'max' => '1000000.00',
145-
'step' => '0.01',
146-
'description' => 'in cm',
147-
'validate' => ['required'],
148-
])->addField('height', [
149-
'type' => 'number',
150-
'title' => 'Box Height',
151-
'default_value' => $product_height,
152-
'min' => '0.00',
153-
'max' => '1000000.00',
154-
'step' => '0.01',
155-
'description' => 'in cm',
156-
'validate' => ['required'],
157121
])->addField('price', [
158122
'type' => 'number',
159123
'title' => 'Price',
@@ -170,6 +134,7 @@ public function getFormDefinition(FAPI\Form $form, array &$form_state): FAPI\For
170134
'validate' => ['required'],
171135
]);
172136

137+
$this->addPhysicalProductFormElements($form, $form_state);
173138
$this->addFrontendFormElements($form, $form_state);
174139
$this->addSeoFormElements($form, $form_state);
175140

@@ -230,10 +195,10 @@ public function formSubmitted(FAPI\Form $form, &$form_state): mixed
230195
$product->setLocale($values['frontend']['locale']);
231196
$product->setContent($values['content']);
232197
$product->setWebsiteId($values['frontend']['website_id']);
233-
$product->setWeight((float)$values['weight']);
234-
$product->setHeight((float)$values['height']);
235-
$product->setLength((float)$values['length']);
236-
$product->setWidth((float)$values['width']);
198+
$product->setWeight((float)$values['physical']['weight']);
199+
$product->setHeight((float)$values['physical']['height']);
200+
$product->setLength((float)$values['physical']['length']);
201+
$product->setWidth((float)$values['physical']['width']);
237202
$product->setPrice((float)$values['price']);
238203
$product->setTaxClassId($values['tax_class_id']);
239204

0 commit comments

Comments
 (0)