/** * Add item to the cart, it can be an array or multi dimensional array * * @param string|array $id * @param string $name * @param float $price * @param int $quantity * @param array $attributes * @param CartCondition|array $conditions * * @return $this * * @throws InvalidItemException */ public function add($id, $name = null, $price = null, $quantity = null, $attributes = [], $conditions = []) { // TODO Sift id, name, price and quantity and add remainings to the attirbutes array. // If the first argument is an array, we will need to call add again if (is_array($id)) { // The first argument is an array, now we will need to check if it is a multi dimensional // array, if so, we will iterate through each item and call add again if (isMultiArray($id)) { foreach ($id as $item) { $this->add($item["id"], $item["name"], $item["price"], $item["quantity"], issetAndHasValueOrAssignDefault($item["attributes"], []), issetAndHasValueOrAssignDefault($item["conditions"], [])); } } else { $this->add($id["id"], $id["name"], $id["price"], $id["quantity"], issetAndHasValueOrAssignDefault($id["attributes"], []), issetAndHasValueOrAssignDefault($id["conditions"], [])); } return $this; } // Validate data $item = $this->validate(["id" => $id, "name" => $name, "price" => normalizePrice($price), "quantity" => $quantity, "attributes" => new ItemAttributeCollection($attributes), "conditions" => $conditions]); // Get the cart $cart = $this->getContent(); // If the item is already in the cart we will just update it if ($cart->has($id)) { Event::fire(new Events\ItemsUpdating($this, [$item])); // Check if $item changed $this->update($id, $item); Event::fire(new Events\ItemsUpdated($this, [$item])); } else { Event::fire(new Events\ItemsAdding($this, [$item])); // Check if $item changed $this->addRow($id, $item); Event::fire(new Events\ItemsAdded($this, [$item])); } return $this; }
/** * apply condition * * @param $totalOrSubTotalOrPrice * @param $conditionValue * @return float */ protected function apply($totalOrSubTotalOrPrice, $conditionValue) { // if value has a percentage sign on it, we will get first // its percentage then we will evaluate again if the value // has a minus or plus sign so we can decide what to do with the // percentage, whether to add or subtract it to the total/subtotal/price // if we can't find any plus/minus sign, we will assume it as plus sign if ($this->valueIsPercentage($conditionValue)) { if ($this->valueIsToBeSubtracted($conditionValue)) { $value = normalizePrice($this->cleanValue($conditionValue)); $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); } else { if ($this->valueIsToBeAdded($conditionValue)) { $value = normalizePrice($this->cleanValue($conditionValue)); $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } else { $value = normalizePrice($conditionValue); $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100); $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } } } else { if ($this->valueIsToBeSubtracted($conditionValue)) { $this->parsedRawValue = normalizePrice($this->cleanValue($conditionValue)); $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue); } else { if ($this->valueIsToBeAdded($conditionValue)) { $this->parsedRawValue = normalizePrice($this->cleanValue($conditionValue)); $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } else { $this->parsedRawValue = normalizePrice($conditionValue); $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue); } } } return $result; }