ShoppingCart::add PHP Method

add() public method

Adds an item to the cart
public add ( Buyable $buyable, number $quantity = 1, unknown $filter = [] ) : boolean | OrderItem
$buyable Buyable
$quantity number
$filter unknown
return boolean | OrderItem false or the new/existing item
    public function add(Buyable $buyable, $quantity = 1, $filter = array())
    {
        $order = $this->findOrMake();
        // If an extension throws an exception, error out
        try {
            $order->extend("beforeAdd", $buyable, $quantity, $filter);
        } catch (Exception $exception) {
            return $this->error($exception->getMessage());
        }
        if (!$buyable) {
            return $this->error(_t("ShoppingCart.ProductNotFound", "Product not found."));
        }
        $item = $this->findOrMakeItem($buyable, $quantity, $filter);
        if (!$item) {
            return false;
        }
        if (!$item->_brandnew) {
            $item->Quantity += $quantity;
        } else {
            $item->Quantity = $quantity;
        }
        // If an extension throws an exception, error out
        try {
            $order->extend("afterAdd", $item, $buyable, $quantity, $filter);
        } catch (Exception $exception) {
            return $this->error($exception->getMessage());
        }
        $item->write();
        $this->message(_t("ShoppingCart.ItemAdded", "Item has been added successfully."));
        return $item;
    }

Usage Example

Esempio n. 1
0
 public function testComputesReducedVatForTheRightCountry()
 {
     $shoppingCart = new ShoppingCart(new Austria());
     $article = new Article(new ArticleName('Test'), new ArticleDescription(''), new Euro(1000), new ArticleTypeReduced());
     $shoppingCart->add($article);
     $shoppingCart->add($article);
     $this->assertEquals(new Money(2200, new Currency('EUR')), $shoppingCart->total());
 }
All Usage Examples Of ShoppingCart::add