Ejemplo n.º 1
0
 /**
  * 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 = array(), $conditions = 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 (Helpers::isMultiArray($id)) {
             foreach ($id as $item) {
                 $this->add($item['id'], $item['name'], $item['price'], $item['quantity'], Helpers::issetAndHasValueOrAssignDefault($item['attributes'], array()), Helpers::issetAndHasValueOrAssignDefault($item['conditions'], array()));
             }
         } else {
             $this->add($id['id'], $id['name'], $id['price'], $id['quantity'], Helpers::issetAndHasValueOrAssignDefault($id['attributes'], array()), Helpers::issetAndHasValueOrAssignDefault($id['conditions'], array()));
         }
         return $this;
     }
     // validate data
     $item = $this->validate(array('id' => $id, 'name' => $name, 'price' => Helpers::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)) {
         $this->events->fire($this->getInstanceName() . '.updating', array($item, $this));
         $this->update($id, $item);
         $this->events->fire($this->getInstanceName() . '.updated', array($item, $this));
     } else {
         $this->events->fire($this->getInstanceName() . '.adding', array($item, $this));
         $this->addRow($id, $item);
         $this->events->fire($this->getInstanceName() . '.added', array($item, $this));
     }
     return $this;
 }
Ejemplo n.º 2
0
 /**
  * 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 = Helpers::normalizePrice($this->cleanValue($conditionValue));
             $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100);
             $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue);
         } else {
             if ($this->valueIsToBeAdded($conditionValue)) {
                 $value = Helpers::normalizePrice($this->cleanValue($conditionValue));
                 $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100);
                 $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
             } else {
                 $value = Helpers::normalizePrice($conditionValue);
                 $this->parsedRawValue = $totalOrSubTotalOrPrice * ($value / 100);
                 $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
             }
         }
     } else {
         if ($this->valueIsToBeSubtracted($conditionValue)) {
             $this->parsedRawValue = Helpers::normalizePrice($this->cleanValue($conditionValue));
             $result = floatval($totalOrSubTotalOrPrice - $this->parsedRawValue);
         } else {
             if ($this->valueIsToBeAdded($conditionValue)) {
                 $this->parsedRawValue = Helpers::normalizePrice($this->cleanValue($conditionValue));
                 $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
             } else {
                 $this->parsedRawValue = Helpers::normalizePrice($conditionValue);
                 $result = floatval($totalOrSubTotalOrPrice + $this->parsedRawValue);
             }
         }
     }
     return $result;
 }