refactor update item eventListener
This commit is contained in:
@@ -49,7 +49,7 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
* add an article in the current cart
|
||||
* @param \Thelia\Core\Event\CartEvent $event
|
||||
*/
|
||||
public function addArticle(CartEvent $event)
|
||||
public function addItem(CartEvent $event)
|
||||
{
|
||||
|
||||
$cart = $event->cart;
|
||||
@@ -67,7 +67,7 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
->filterByProductSaleElementsId($productSaleElementsId)
|
||||
->findOne();
|
||||
|
||||
$this->addItem($cart, $productId, $productSaleElementsId, $quantity, $productPrice);
|
||||
$this->doAddItem($cart, $productId, $productSaleElementsId, $quantity, $productPrice);
|
||||
}
|
||||
|
||||
if ($append && $cartItem !== null) {
|
||||
@@ -81,7 +81,7 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
*
|
||||
* @param \Thelia\Core\Event\CartEvent $event
|
||||
*/
|
||||
public function deleteArticle(CartEvent $event)
|
||||
public function deleteItem(CartEvent $event)
|
||||
{
|
||||
if (null !== $cartItemId = $event->cartItem) {
|
||||
$cart = $event->cart;
|
||||
@@ -101,25 +101,18 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
*
|
||||
* @param \Thelia\Core\Event\CartEvent $event
|
||||
*/
|
||||
public function modifyArticle(CartEvent $event)
|
||||
public function changeItem(CartEvent $event)
|
||||
{
|
||||
$request = $event->getRequest();
|
||||
if ((null !== $cartItemId = $event->cartItem) && (null !== $quantity = $event->quantity)) {
|
||||
$cart = $event->cart;
|
||||
|
||||
if (null !== $cartItemId = $request->get("cartItem") && null !== $quantity = $request->get("quantity")) {
|
||||
|
||||
try {
|
||||
$cart = $event->getCart($request);
|
||||
|
||||
$cartItem = CartItemQuery::create()
|
||||
->filterByCartId($cart->getId())
|
||||
->filterById($cartItemId)
|
||||
->findOne();
|
||||
|
||||
if ($cartItem) {
|
||||
$this->updateQuantity($cartItem, $quantity);
|
||||
}
|
||||
} catch (PropelException $e) {
|
||||
\Thelia\Log\Tlog::getInstance()->error(sprintf("error during updating cartItem with message : %s", $e->getMessage()));
|
||||
$cartItem = CartItemQuery::create()
|
||||
->filterByCartId($cart->getId())
|
||||
->filterById($cartItemId)
|
||||
->findOne();
|
||||
|
||||
if ($cartItem) {
|
||||
$this->updateQuantity($cartItem, $quantity);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -147,9 +140,9 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
public static function getSubscribedEvents()
|
||||
{
|
||||
return array(
|
||||
"action.addArticle" => array("addArticle", 128),
|
||||
"action.deleteArticle" => array("deleteArticle", 128),
|
||||
"action.changeArticle" => array("modifyArticle", 128),
|
||||
"action.addArticle" => array("addItem", 128),
|
||||
"action.deleteArticle" => array("deleteItem", 128),
|
||||
"action.changeArticle" => array("changeItem", 128),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -175,7 +168,7 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
* @param float $quantity
|
||||
* @param ProductPrice $productPrice
|
||||
*/
|
||||
protected function addItem(\Thelia\Model\Cart $cart, $productId, $productSaleElementsId, $quantity, ProductPrice $productPrice)
|
||||
protected function doAddItem(\Thelia\Model\Cart $cart, $productId, $productSaleElementsId, $quantity, ProductPrice $productPrice)
|
||||
{
|
||||
$cartItem = new CartItem();
|
||||
$cartItem->setDisptacher($this->getDispatcher());
|
||||
|
||||
@@ -20,11 +20,17 @@
|
||||
</route>
|
||||
|
||||
<route id="cart.add.process" path="/cart/add">
|
||||
<default key="_controller">Thelia\Controller\Front\CartController::addArticle</default>
|
||||
<default key="_controller">Thelia\Controller\Front\CartController::addItem</default>
|
||||
<default key="_view">cart</default>
|
||||
</route>
|
||||
|
||||
<route id="cart.change.process" path="/cart/delete">
|
||||
<default key="_controller">Thelia\Controller\Front\CartController::changeArticle</default>
|
||||
<route id="cart.change.process" path="/cart/delete/{cart_item}">
|
||||
<default key="_controller">Thelia\Controller\Front\CartController::deleteItem</default>
|
||||
<default key="_view">cart</default>
|
||||
</route>
|
||||
|
||||
<route id="cart.update.quantity" path="/cart/update">
|
||||
<default key="_controller">Thelia\Controller\Front\CartController::changeItem</default>
|
||||
<default key="_view">cart</default>
|
||||
</route>
|
||||
</routes>
|
||||
|
||||
@@ -33,7 +33,7 @@ class CartController extends BaseFrontController
|
||||
{
|
||||
use \Thelia\Cart\CartTrait;
|
||||
|
||||
public function addArticle()
|
||||
public function addItem()
|
||||
{
|
||||
$request = $this->getRequest();
|
||||
|
||||
@@ -67,28 +67,37 @@ class CartController extends BaseFrontController
|
||||
}
|
||||
}
|
||||
|
||||
public function changeArticle()
|
||||
public function changeItem()
|
||||
{
|
||||
$cartEvent = $this->getCartEvent();
|
||||
$cartEvent->cartItem = $this->getRequest()->get("cart_item");
|
||||
$cartEvent->quantity = $this->getRequest()->get("quantity");
|
||||
|
||||
$this->dispatch(TheliaEvents::CART_CHANGEITEM, $cartEvent);
|
||||
try {
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::CART_CHANGEITEM, $cartEvent);
|
||||
|
||||
$this->redirectSuccess();
|
||||
} catch(PropelException $e) {
|
||||
$this->getParserContext()->setGeneralError($e->getMessage());
|
||||
}
|
||||
|
||||
$this->redirectSuccess();
|
||||
}
|
||||
|
||||
public function deleteArticle()
|
||||
public function deleteItem()
|
||||
{
|
||||
$cartEvent = $this->getCartEvent();
|
||||
$cartEvent->cartItem = $this->getRequest()->get("cartItem");
|
||||
$cartEvent->cartItem = $this->getRequest()->get("cart_item");
|
||||
|
||||
try {
|
||||
$this->getDispatcher()->dispatch(TheliaEvents::CART_DELETEITEM, $cartEvent);
|
||||
} catch (PropelException $e)
|
||||
{
|
||||
|
||||
$this->redirectSuccess();
|
||||
} catch (PropelException $e) {
|
||||
\Thelia\Log\Tlog::getInstance()->error(sprintf("error during deleting cartItem with message : %s", $e->getMessage()));
|
||||
$this->getParserContext()->setGeneralError($e->getMessage());
|
||||
}
|
||||
|
||||
$this->redirectSuccess();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -58,6 +58,11 @@ class ParserContext implements \IteratorAggregate
|
||||
$this->set('error_form', $form);
|
||||
}
|
||||
|
||||
public function setGeneralError($error)
|
||||
{
|
||||
$this->set('general_error', $error);
|
||||
}
|
||||
|
||||
public function getErrorForm()
|
||||
{
|
||||
return $this->get('error_form', null);
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
<h1>{intl l='cart'}</h1>
|
||||
<ul>
|
||||
{loop name="cart" type="cart"}
|
||||
<li>Item {$LOOP_COUNT}/{$LOOP_TOTAL} : #TITLE - quantity : #QUANTITY</li>
|
||||
<li>Item {$LOOP_COUNT}/{$LOOP_TOTAL} : #ITEM_ID #TITLE - quantity : #QUANTITY</li>
|
||||
{/loop}
|
||||
</ul>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user