diff --git a/core/lib/Thelia/Action/Cart.php b/core/lib/Thelia/Action/Cart.php index 6ef171c41..46c12cfe5 100755 --- a/core/lib/Thelia/Action/Cart.php +++ b/core/lib/Thelia/Action/Cart.php @@ -23,6 +23,7 @@ namespace Thelia\Action; +use Propel\Runtime\Exception\PropelException; use Symfony\Component\Config\Definition\Exception\Exception; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\HttpFoundation\Request; @@ -38,6 +39,7 @@ use Thelia\Model\CartQuery; use Thelia\Model\Cart as CartModel; use Thelia\Model\ConfigQuery; use Thelia\Model\Customer; +use Thelia\Tools\Redirect; /** * @@ -71,37 +73,45 @@ class Cart implements EventSubscriberInterface { $request = $event->getRequest(); - $form = $this->getAddCartForm($request); + $cartAdd = $this->getAddCartForm($request); + + $form = $cartAdd->getForm(); $form->bind($request); if($form->isValid()) { - $cart = $this->getCart($request); + try { + $cart = $this->getCart($request); - $productSaleElementsId = $form->get("product_sale_elements_id")->getData(); + $productSaleElementsId = $form->get("product_sale_elements_id")->getData(); - $productPrice = ProductPriceQuery::create() - ->filterByProductSaleElementsId($productSaleElementsId) - ->findOne() - ; + $productPrice = ProductPriceQuery::create() + ->filterByProductSaleElementsId($productSaleElementsId) + ->findOne() + ; - $cartItem = new CartItem(); - $cartItem->setDisptacher($event->getDispatcher()); - $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(); - ; + $cartItem = new CartItem(); + $cartItem->setDisptacher($event->getDispatcher()); + $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(); + ; + Redirect::exec($cartAdd->getSuccessUrl()); + } catch (PropelException $e) { + \Thelia\Log\Tlog::getInstance()->error(sptinf("error on adding item to cart with message : %s", $e->getMessage())); + $message = "Impossible to add this article to your cart, please try again"; + } } else { - + $message = "Missing or invalid data"; } } @@ -120,7 +130,7 @@ class Cart implements EventSubscriberInterface ); } - return $cartAdd->getForm(); + return $cartAdd; } diff --git a/core/lib/Thelia/Core/HttpFoundation/Request.php b/core/lib/Thelia/Core/HttpFoundation/Request.php index e2ed963d0..573cfe228 100755 --- a/core/lib/Thelia/Core/HttpFoundation/Request.php +++ b/core/lib/Thelia/Core/HttpFoundation/Request.php @@ -1,12 +1,25 @@ . */ +/* */ +/*************************************************************************************/ namespace Thelia\Core\HttpFoundation; use Symfony\Component\HttpFoundation\Request as BaseRequest; @@ -19,4 +32,20 @@ class Request extends BaseRequest{ return $this->get("product_id"); } + public function getUriAddingParameters(array $parameters = null) + { + $uri = $this->getUri(); + + $additionalQs = ''; + + foreach ($parameters as $key => $value) { + $additionalQs .= sprintf("&%s=%s", $key, $value); + } + + if ('' == $this->getQueryString()) { + $additionalQs = '?'. ltrim($additionalQs, '&'); + } + return $uri . $additionalQs; + } + } \ No newline at end of file diff --git a/core/lib/Thelia/Tests/Core/HttpFoundation/RequestTest.php b/core/lib/Thelia/Tests/Core/HttpFoundation/RequestTest.php new file mode 100644 index 000000000..1745da6f7 --- /dev/null +++ b/core/lib/Thelia/Tests/Core/HttpFoundation/RequestTest.php @@ -0,0 +1,43 @@ +getMock( + "Thelia\Core\HttpFoundation\Request", + array("getUri", "getQueryString") + ); + + $request->expects($this->any()) + ->method("getUri") + ->will($this->onConsecutiveCalls( + "http://localhost/", + "http://localhost/?test=fu" + )); + + $request->expects($this->any()) + ->method("getQueryString") + ->will($this->onConsecutiveCalls( + "", + "test=fu" + )); + + $result = $request->getUriAddingParameters(array("foo" => "bar")); + + $this->assertEquals("http://localhost/?foo=bar", $result); + + $result = $request->getUriAddingParameters(array("foo" => "bar")); + + $this->assertEquals("http://localhost/?test=fu&foo=bar", $result); + + + } + +} \ No newline at end of file