diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index 949010384..e41309279 100755 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -32,6 +32,8 @@ use Thelia\Core\Event\CartEvent; use Thelia\Core\Event\TheliaEvents; use Thelia\Core\HttpFoundation\Session\Session; use Thelia\Form\CartAdd; +use Thelia\Model\ProductPriceQuery; +use Thelia\Model\CartItem; use Thelia\Model\CartQuery; use Thelia\Model\Cart as CartModel; use Thelia\Model\ConfigQuery; @@ -67,9 +69,43 @@ class Cart implements EventSubscriberInterface */ public function addArticle(ActionEvent $event) { - var_dump($this); $request = $event->getRequest(); + $form = $this->getAddCartForm($request); + + $form->bind($request); + + if($form->isValid()) { + $cart = $this->getCart($request); + + $productSaleElementsId = $form->get("product_sale_elements_id")->getData(); + + $productPrice = ProductPriceQuery::create() + ->filterByProductSaleElementsId($productSaleElementsId) + ->findOne() + ; + + $cartItem = new CartItem(); + $cartItem + ->setCart($cart) + ->setProductId($form->get("product")->getData()) + ->setProductSaleElementsId($productSaleElementsId) + ->setQuantity($form->get("quantity")->getData()) + ->setPrice($productPrice->getPrice()) + ->setPromoPrice($productPrice->getPromoPrice()) + ->setPriceEndOfLife(time() + ConfigQuery::read("cart.priceEOF", 60*60*24*30)) + ->save(); + ; + + + } else { + + + } + } + + private function getAddCartForm(Request $request) + { if ($request->isMethod("post")) { $cartAdd = new CartAdd($request); } else { @@ -83,14 +119,7 @@ class Cart implements EventSubscriberInterface ); } - $form = $cartAdd->getForm(); - - $form->bind($request); - - if($form->isValid()) { - - } else { - } + return $cartAdd->getForm(); } diff --git a/core/lib/Thelia/Core/Event/TheliaEvents.php b/core/lib/Thelia/Core/Event/TheliaEvents.php index 883ea5aff..41c5694c6 100755 --- a/core/lib/Thelia/Core/Event/TheliaEvents.php +++ b/core/lib/Thelia/Core/Event/TheliaEvents.php @@ -93,4 +93,14 @@ final class TheliaEvents * sent when a new existing cat id duplicated. This append when current customer is different from current cart */ const CART_DUPLICATE = "cart.duplicate"; + + /** + * sent when a new item is added to current cart + */ + const CART_ADDITEM = "cart.addItem"; + + /** + * sent when a cart item is modify + */ + const CART_MODIFYITEM = "cart.modifyItem"; } \ No newline at end of file diff --git a/core/lib/Thelia/Model/CartItem.php b/core/lib/Thelia/Model/CartItem.php index 4b7e2971c..b58323e07 100644 --- a/core/lib/Thelia/Model/CartItem.php +++ b/core/lib/Thelia/Model/CartItem.php @@ -2,9 +2,30 @@ namespace Thelia\Model; +use Propel\Runtime\Connection\ConnectionInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Thelia\Core\Event\CartEvent; +use Thelia\Core\Event\TheliaEvents; use Thelia\Model\Base\CartItem as BaseCartItem; class CartItem extends BaseCartItem { + protected $dispatcher; + + public function setDisptacher(EventDispatcherInterface $dispatcher) + { + $this->dispatcher = $dispatcher; + } + + public function postInsert(ConnectionInterface $con = null) + { + if ($this->dispatcher) { + $cartEvent = new CartEvent($this->getCart()); + + $this->dispatcher->dispatch(TheliaEvents::CART_ADDITEM, $cartEvent); + } + } + + }