LukePOLO\LaraCart\CartItem::generateHash PHP Method

generateHash() public method

Generates a hash based on the cartItem array.
public generateHash ( boolean $force = false ) : string
$force boolean
return string itemHash
    public function generateHash($force = false)
    {
        if ($this->lineItem === false) {
            $this->itemHash = null;
            $cartItemArray = (array) $this;
            unset($cartItemArray['options']['qty']);
            ksort($cartItemArray['options']);
            $this->itemHash = app(LaraCart::HASH, $cartItemArray);
        } elseif ($force || empty($this->itemHash) === true) {
            $this->itemHash = app(LaraCart::RANHASH);
        }
        app('events')->fire('laracart.updateItem', ['item' => $this, 'newHash' => $this->itemHash]);
        return $this->itemHash;
    }

Usage Example

示例#1
0
 /**
  * Adds the cartItem into the cart session
  *
  * @param CartItem $cartItem
  *
  * @return CartItem
  */
 public function addItem(CartItem $cartItem)
 {
     $itemHash = $cartItem->generateHash();
     if ($this->getItem($itemHash)) {
         $this->getItem($itemHash)->qty += $cartItem->qty;
     } else {
         $this->cart->items[] = $cartItem;
     }
     \Event::fire('laracart.addItem', $cartItem);
     $this->update();
     return $cartItem;
 }