Permalink
Browse files

Adding item add quicklink for carts. Changing whitespace.

  • Loading branch information...
1 parent 78ffb9b commit d1cc934dd4947ad4262a083db67b18d262d76af3 Benjamin Scholler committed Mar 27, 2012
Showing with 83 additions and 56 deletions.
  1. +75 −51 Controller/Frontend/CartController.php
  2. +8 −5 Resources/config/routing/frontend/cart.yml
@@ -30,123 +30,147 @@ class CartController extends ContainerAware
*/
public function showAction()
{
- $cart = $this->container->get('sylius_cart.provider')->getCart();
-
- $form = $this->container->get('form.factory')->create($this->container->get('sylius_cart.form.type.cart'));
- $form->setData($cart);
-
+ $cart = $this->container->get('sylius_cart.provider')->getCart();
+
+ $form = $this->container->get('form.factory')->create($this->container->get('sylius_cart.form.type.cart'));
+ $form->setData($cart);
+
return $this->container->get('templating')->renderResponse('SyliusCartBundle:Frontend/Cart:show.html.' . $this->getEngine(), array(
- 'cart' => $cart,
- 'form' => $form->createView()
+ 'cart' => $cart,
+ 'form' => $form->createView()
));
}
-
- /**
+
+ /**
* Adds item to cart.
*/
public function addItemAction()
{
$itemManager = $this->container->get('sylius_cart.manager.item');
$item = $itemManager->createItem();
-
+
$form = $this->container->get('form.factory')->create($this->container->get('sylius_cart.form.type.item'));
$form->setData($item);
$form->bindRequest($this->container->get('request'));
-
+
if ($form->isValid()) {
$cart = $this->container->get('sylius_cart.provider')->getCart();
$this->container->get('event_dispatcher')->dispatch(SyliusCartEvents::ITEM_ADD, new CartOperationEvent($item, $cart));
-
+
$cartOperator = $this->container->get('sylius_cart.operator');
- $cartOperator->addItem($cart, $item);
+ $cartOperator->addItem($cart, $item);
$cartOperator->refreshCart($cart);
-
+
$cartManager = $this->container->get('sylius_cart.manager.cart');
-
+
$cartManager->persistCart($cart);
$cartManager->flushCarts();
}
-
+
return new RedirectResponse($this->container->get('router')->generate('sylius_cart_show'));
}
-
+
+ /**
+ * Adds item to cart via a link.
+ */
+ public function addItemQuicklinkAction($id)
+ {
+ $itemManager = $this->container->get('sylius_cart.manager.item');
+ $item = $itemManager->findItemById($id);
+ $item->setQuantity(1);
+
+ $cart = $this->container->get('sylius_cart.provider')->getCart();
+ $this->container->get('event_dispatcher')->dispatch(SyliusCartEvents::ITEM_ADD, new CartOperationEvent($item, $cart));
+
+ $cartOperator = $this->container->get('sylius_cart.operator');
+ $cartOperator->addItem($cart, $item);
+ $cartOperator->refreshCart($cart);
+
+ $cartManager = $this->container->get('sylius_cart.manager.cart');
+
+ $cartManager->persistCart($cart);
+ $cartManager->flushCarts();
+
+ return new RedirectResponse($this->container->get('router')->generate('sylius_cart_show'));
+ }
+
/**
* Removes item from cart.
*/
public function removeItemAction($id)
{
$itemManager = $this->container->get('sylius_cart.manager.item');
$item = $itemManager->findItem($id);
-
+
if (!$item) {
throw new NotFoundHttpException('This cart item does not exist.');
}
-
+
$cart = $this->container->get('sylius_cart.provider')->getCart();
if ($item->getCart() !== $cart) {
throw new NotFoundHttpException('This cart item is not accessible.');
}
-
+
$cartOperator = $this->container->get('sylius_cart.operator');
-
+
$this->container->get('event_dispatcher')->dispatch(SyliusCartEvents::ITEM_REMOVE, new CartOperationEvent($item, $cart));
$cartOperator->removeItem($cart, $item);
$cartOperator->refreshCart($cart);
-
+
$itemManager->removeItem($item);
-
+
$this->container->get('sylius_cart.manager.cart')->persistCart($cart);
-
+
return new RedirectResponse($this->container->get('router')->generate('sylius_cart_show'));
}
-
+
/**
* Saves cart.
*/
public function updateAction()
{
$cart = $this->container->get('sylius_cart.provider')->getCart();
-
- $form = $this->container->get('form.factory')->create($this->container->get('sylius_cart.form.type.cart'));
- $form->setData($cart);
- $form->bindRequest($this->container->get('request'));
-
- if ($form->isValid()) {
- $cartOperator = $this->container->get('sylius_cart.operator');
-
- $existingItems = array();
-
+
+ $form = $this->container->get('form.factory')->create($this->container->get('sylius_cart.form.type.cart'));
+ $form->setData($cart);
+ $form->bindRequest($this->container->get('request'));
+
+ if ($form->isValid()) {
+ $cartOperator = $this->container->get('sylius_cart.operator');
+
+ $existingItems = array();
+
foreach ($cart->getItems() as $item) {
$existingItems[] = $item;
}
-
- $cart->clearItems();
-
- foreach($existingItems as $item) {
- $cartOperator->addItem($cart, $item);
- }
-
- $this->container->get('event_dispatcher')->dispatch(SyliusCartEvents::CART_UPDATE, new FilterCartEvent($cart));
- $cartOperator->refreshCart($cart);
-
- $this->container->get('sylius_cart.manager.cart')->persistCart($cart);
- }
-
+
+ $cart->clearItems();
+
+ foreach($existingItems as $item) {
+ $cartOperator->addItem($cart, $item);
+ }
+
+ $this->container->get('event_dispatcher')->dispatch(SyliusCartEvents::CART_UPDATE, new FilterCartEvent($cart));
+ $cartOperator->refreshCart($cart);
+
+ $this->container->get('sylius_cart.manager.cart')->persistCart($cart);
+ }
+
return new RedirectResponse($this->container->get('router')->generate('sylius_cart_show'));
}
-
+
/**
* Removes cart.
*/
public function clearAction()
{
$this->container->get('sylius_cart.manager.cart')->removeCart($this->container->get('sylius_cart.provider')->getCart());
-
+
return new RedirectResponse($this->container->get('router')->generate('sylius_cart_show'));
}
-
+
/**
* Returns templating engine name.
*
@@ -1,24 +1,27 @@
sylius_cart_show:
pattern: /cart
defaults: { _controller: sylius_cart.controller.frontend.cart:showAction }
-
+
sylius_cart_update:
pattern: /cart/update
defaults: { _controller: sylius_cart.controller.frontend.cart:updateAction }
requirements:
_method: POST
-
+
sylius_cart_clear:
pattern: /cart/clear
defaults: { _controller: sylius_cart.controller.frontend.cart:clearAction }
-
+
sylius_cart_item_add:
pattern: /cart/item/add
defaults: { _controller: sylius_cart.controller.frontend.cart:addItemAction }
requirements:
_method: POST
-
+
+sylius_cart_item_add_quicklink:
+ pattern: /cart/item/add/{id}
+ defaults: { _controller: sylius_cart.controller.frontend.cart:addItemQuicklinkAction }
+
sylius_cart_item_remove:
pattern: /cart/item/{id}/remove
defaults: { _controller: sylius_cart.controller.frontend.cart:removeItemAction }
-

0 comments on commit d1cc934

Please sign in to comment.