From b65a75fcf70feaf547d414672f78d7cbe8a98e7e Mon Sep 17 00:00:00 2001 From: Belle Aerni Date: Sun, 11 Jun 2023 21:30:20 -0700 Subject: [PATCH] Validate add-on configs before adding to cart (#1313) * Validate addon configs before adding to cart * Fix spelling mistake --- src/modules/Cart/Api/Guest.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/modules/Cart/Api/Guest.php b/src/modules/Cart/Api/Guest.php index 7c11e92af..2992d8c3c 100644 --- a/src/modules/Cart/Api/Guest.php +++ b/src/modules/Cart/Api/Guest.php @@ -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();