コード例 #1
0
 /**
  * Changes the quantity of a Product in the Cart
  *
  * @param CoreShopProduct $product
  * @param int $amount
  * @param bool|true $autoAddCartRule
  * @return bool|CoreShopCartItem
  * @throws \Exception
  */
 public function updateQuantity(CoreShopProduct $product, $amount = 0, $autoAddCartRule = true)
 {
     if (!$product instanceof CoreShopProduct) {
         throw new \Exception("\$product must be instance of CoreShopProduct");
     }
     $item = $this->findItemForProduct($product);
     if ($item instanceof CoreShopCartItem) {
         if ($amount <= 0) {
             $this->removeItem($item);
             return false;
         } else {
             $item->setAmount($amount);
             $item->save();
         }
     } else {
         $items = $this->getItems();
         if (!is_array($items)) {
             $items = array();
         }
         $item = new CoreShopCartItem();
         $item->setKey(uniqid());
         $item->setParent($this);
         $item->setAmount($amount);
         $item->setProduct($product);
         $item->setPublished(true);
         $item->save();
         $items[] = $item;
         $this->setItems($items);
         $this->save(true);
     }
     if ($autoAddCartRule) {
         PriceRule::autoAddToCart();
     }
     return $item;
 }