initialize cart item add

This commit is contained in:
Manuel Raynaud
2013-08-06 12:32:22 +02:00
parent ea81889aea
commit 7343e7ee02
3 changed files with 69 additions and 9 deletions

View File

@@ -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();
}

View File

@@ -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";
}

View File

@@ -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);
}
}
}