apply treatment for append and newness when adding item to cart

This commit is contained in:
Manuel Raynaud
2013-08-07 16:46:25 +02:00
parent 80496641c6
commit 02faa94cb4
2 changed files with 57 additions and 16 deletions

View File

@@ -33,8 +33,10 @@ use Thelia\Core\Event\CartEvent;
use Thelia\Core\Event\TheliaEvents;
use Thelia\Core\HttpFoundation\Session\Session;
use Thelia\Form\CartAdd;
use Thelia\Model\ProductPrice;
use Thelia\Model\ProductPriceQuery;
use Thelia\Model\CartItem;
use Thelia\Model\CartItemQuery;
use Thelia\Model\CartQuery;
use Thelia\Model\Cart as CartModel;
use Thelia\Model\ConfigQuery;
@@ -80,26 +82,29 @@ class Cart extends BaseAction implements EventSubscriberInterface
if($form->isValid()) {
try {
$cart = $this->getCart($request);
$newness = $form->get("newness")->getData();
$append = $form->get("append")->getData();
$quantity = $form->get("quantity")->getData();
$productSaleElementsId = $form->get("product_sale_elements_id")->getData();
$productId = $form->get("product")->getData();
$productPrice = ProductPriceQuery::create()
->filterByProductSaleElementsId($productSaleElementsId)
->findOne()
;
$cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId);
if($cartItem === null || $newness)
{
$productPrice = ProductPriceQuery::create()
->filterByProductSaleElementsId($productSaleElementsId)
->findOne()
;
$this->addItem($cart, $productId, $productSaleElementsId, $quantity, $productPrice);
}
if($append && $cartItem !== null) {
$this->updateQuantity($cartItem, $quantity);
}
$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();
;
$this->redirect($cartAdd->getSuccessUrl($request->getUriAddingParameters(array("addCart" => 1))));
} catch (PropelException $e) {
@@ -118,6 +123,37 @@ class Cart extends BaseAction implements EventSubscriberInterface
$event->setErrorForm($cartAdd);
}
protected function updateQuantity(CartItem $cartItem, $quantity)
{
$cartItem->setDisptacher($this->dispatcher);
$cartItem->addQuantity($quantity)
->save();
}
protected function addItem(\Thelia\Model\Cart $cart, $productId, $productSaleElementsId, $quantity, ProductPrice $productPrice)
{
$cartItem = new CartItem();
$cartItem->setDisptacher($this->dispatcher);
$cartItem
->setCart($cart)
->setProductId($productId)
->setProductSaleElementsId($productSaleElementsId)
->setQuantity($quantity)
->setPrice($productPrice->getPrice())
->setPromoPrice($productPrice->getPromoPrice())
->setPriceEndOfLife(time() + ConfigQuery::read("cart.priceEOF", 60*60*24*30))
->save();
}
protected function findItem($cartId, $productId, $productSaleElementsId)
{
return CartItemQuery::create()
->filterByCartId($cartId)
->filterByProductId($productId)
->filterByProductSaleElementsId($productSaleElementsId)
->findOne();
}
private function getAddCartForm(Request $request)
{
if ($request->isMethod("post")) {

View File

@@ -26,6 +26,11 @@ class CartItem extends BaseCartItem
}
}
public function addQuantity($value)
{
$this->setQuantity($this->getQuantity() + $value);
return $this;
}
}