Validate add-on configs before adding to cart (#1313)

* Validate addon configs before adding to cart

* Fix spelling mistake
This commit is contained in:
Belle Aerni authored and GitHub committed 2023-06-12 06:30:20 +02:00
1 parent 64d60e254a
commit b65a75fcf7
1 file changed
+16
+16
View File
@@ -159,12 +159,28 @@ class Guest extends \Api_Abstract
$required = [
'id' => 'Product id not passed',
];
$this->di['validator']->checkRequiredParamsForArray($required, $data);
$cart = $this->getService()->getSessionCart();
$product = $this->di['db']->getExistingModelById('Product', $data['id'], 'Product not found');
if ($product->is_addon) {
throw new \Box_Exception('Addon products cannot be added separately.');
}
$validAddons = json_decode($product->addons ?? '');
if(empty($validAddons)){
$validAddons = [];
}
foreach ($data['addons'] as $addon => $properties) {
if($properties['selected'] && !in_array($addon, $validAddons)){
throw new \Box_Exception('One or more of your selected addons are not valid for the associated product.');
}
}
// reset cart by default
if (!isset($data['multiple']) || !$data['multiple']) {
$this->reset();