Skip to content

Commit

Permalink
Validate add-on configs before adding to cart (#1313)
Browse files Browse the repository at this point in the history
* Validate addon configs before adding to cart

* Fix spelling mistake
  • Loading branch information
BelleNottelling committed Jun 12, 2023
1 parent 64d60e2 commit b65a75f
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/modules/Cart/Api/Guest.php
Expand Up @@ -159,12 +159,28 @@ public function add_item($data)
$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();
Expand Down

0 comments on commit b65a75f

Please sign in to comment.