/** * Checks and prepares variables for a quantity change (add, edit, remove) for an Order Item. * @param Boolean $mustBeExistingItems - if false, the Order Item gets created if it does not exist - if TRUE the order item is searched for and an error shows if there is no Order item. * @param DataObject $buyable - the buyable (generally a product) being added to the cart * @param Integer $quantity - number of items add. * @param Array $parameters - array of parameters to target a specific order item. eg: group=1, length=5* * @return boolean | DataObject ($orderItem) */ protected function prepareOrderItem($mustBeExistingItem = true, $buyable, $parameters = array()) { if (!$buyable) { user_error("No buyable was provided", E_USER_WARNING); } if (!$buyable->canPurchase()) { $item = $this->getExistingItem($buyable, $parameters); if ($item && $item->exists()) { $item->delete(); $item->destroy(); } return false; } $item = null; if ($mustBeExistingItem) { $item = $this->getExistingItem($buyable, $parameters); } else { $item = $this->findOrMakeItem($buyable, $parameters); //find existing order item or make one } if (!$item) { //check for existence of item return false; } return $item; }