/** * Action 'add' du module 'cart' permettant d'ajouter un article * dans le panier virtuel de l'utilisateur * * @param sfWebRequest $request */ public function executeAdd(sfWebRequest $request) { if ($this->hasRequestParameter('id') && $this->hasRequestParameter('quantity')) { // on recupere les parametres de la requête $id = $request->getParameter('id'); $quantity = $request->getParameter('quantity'); // on recupere l'article dans la base de données $item = Doctrine::getTable('IStoreItem')->find($id); if ($item !== null) { // on recupere la panier virtuel stockés dans la session $shoppingCart = $this->getUser()->getShoppingCart(); // si le produit n'existe pas, on l'ajoute au panier $shoppingCartItem = $shoppingCart->getItem($id, self::$_itemClass); if ($shoppingCartItem === null) { $shoppingCartItem = new sfShoppingCartItem(self::$_itemClass, $id); $shoppingCartItem->setQuantity($quantity); $shoppingCartItem->setPrice($item->getUnitCost()); $shoppingCart->addItem($shoppingCartItem); } else { $shoppingCartItem->setQuantity($shoppingCartItem->getQuantity() + $quantity); } } // on redirige vers le panier $this->forward('cart', 'show'); } $this->forward404(); }
/** * Ajoute un article au panier * * @param sfShoppingCart $item article à ajouter au panier */ public function addItem(sfShoppingCartItem $item) { $found = $this->getItemKey($item->getId(), $item->getClass()); if ($found === null) { $this->_items[] = $item; } }
public function executeAdd_to_cart(dmWebRequest $request) { $this->forward404Unless($product = dmDb::table('Product')->findOneById($request->getParameter('id'))); $item = new sfShoppingCartItem('Product', $this->getRequestParameter('id')); $item->setQuantity(1); $item->setPrice($product->getIsInAction() ? $product->getPriceAction() : $product->getPrice()); $item->setName($product->getName()); $shopping_cart = $this->getUser()->getShoppingCart(); $shopping_cart->addItem($item); $this->redirectBack(); }
/** * Test la méthode qui ajoute un article dans le panier */ public function testAddItem() { // on ajoute un item $item = new sfShoppingCartItem(); $item->setId(3); $item->setClass('IStoreShoppingCartItem'); $this->object->addItem($item); // on test si il existe $item = null; $item = $this->object->getItem(3, 'IStoreShoppingCartItem'); $this->assertNotNull($item); }
/** * Test de la méthode qui modifie le poids d'un article */ public function testSetWeight() { $this->object->setWeight(20.5); $value = $this->object->getWeight(); $this->assertEquals($value, 20.5); }