dispatch event when cart is duplicated
This commit is contained in:
@@ -24,9 +24,12 @@
|
|||||||
namespace Thelia\Action;
|
namespace Thelia\Action;
|
||||||
|
|
||||||
use Symfony\Component\Config\Definition\Exception\Exception;
|
use Symfony\Component\Config\Definition\Exception\Exception;
|
||||||
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
use Thelia\Core\Event\ActionEvent;
|
use Thelia\Core\Event\ActionEvent;
|
||||||
|
use Thelia\Core\Event\CartEvent;
|
||||||
|
use Thelia\Core\Event\TheliaEvents;
|
||||||
use Thelia\Core\HttpFoundation\Session\Session;
|
use Thelia\Core\HttpFoundation\Session\Session;
|
||||||
use Thelia\Form\CartAdd;
|
use Thelia\Form\CartAdd;
|
||||||
use Thelia\Model\CartQuery;
|
use Thelia\Model\CartQuery;
|
||||||
@@ -36,6 +39,13 @@ use Thelia\Model\Customer;
|
|||||||
|
|
||||||
class Cart implements EventSubscriberInterface
|
class Cart implements EventSubscriberInterface
|
||||||
{
|
{
|
||||||
|
protected $dispatcher;
|
||||||
|
|
||||||
|
public function __construct(EventDispatcherInterface $dispatcher)
|
||||||
|
{
|
||||||
|
$this->dispatcher = $dispatcher;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* add an article to cart
|
* add an article to cart
|
||||||
@@ -44,6 +54,7 @@ class Cart implements EventSubscriberInterface
|
|||||||
*/
|
*/
|
||||||
public function addArticle(ActionEvent $event)
|
public function addArticle(ActionEvent $event)
|
||||||
{
|
{
|
||||||
|
var_dump($this);
|
||||||
$request = $event->getRequest();
|
$request = $event->getRequest();
|
||||||
|
|
||||||
if ($request->isMethod("post")) {
|
if ($request->isMethod("post")) {
|
||||||
@@ -66,7 +77,6 @@ class Cart implements EventSubscriberInterface
|
|||||||
if($form->isValid()) {
|
if($form->isValid()) {
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
var_dump($form->createView());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -194,7 +204,10 @@ class Cart implements EventSubscriberInterface
|
|||||||
$newCart = $cart->duplicate($this->generateCookie(), $customer);
|
$newCart = $cart->duplicate($this->generateCookie(), $customer);
|
||||||
$session->setCart($newCart->getId());
|
$session->setCart($newCart->getId());
|
||||||
|
|
||||||
return $newCart;
|
$cartEvent = new CartEvent($newCart);
|
||||||
|
$this->dispatcher->dispatch(TheliaEvents::CART_DUPLICATE, $newCart);
|
||||||
|
|
||||||
|
return $cartEvent->cart;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function generateCookie()
|
protected function generateCookie()
|
||||||
|
|||||||
@@ -14,6 +14,8 @@
|
|||||||
|
|
||||||
<service id="thelia.action.cart" class="Thelia\Action\Cart">
|
<service id="thelia.action.cart" class="Thelia\Action\Cart">
|
||||||
<tag name="kernel.event_subscriber"/>
|
<tag name="kernel.event_subscriber"/>
|
||||||
|
|
||||||
|
<argument type="service" id="event_dispatcher"/>
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="thelia.action.customer" class="Thelia\Action\Customer" scope="request">
|
<service id="thelia.action.customer" class="Thelia\Action\Customer" scope="request">
|
||||||
|
|||||||
@@ -29,30 +29,13 @@ use Thelia\Model\Cart;
|
|||||||
|
|
||||||
class CartEvent extends InternalEvent {
|
class CartEvent extends InternalEvent {
|
||||||
|
|
||||||
protected $cart;
|
public $cart;
|
||||||
protected $modified;
|
|
||||||
|
|
||||||
public function __construct(Cart $cart)
|
public function __construct(Cart $cart)
|
||||||
{
|
{
|
||||||
$this->cart = $cart;
|
$this->cart = $cart;
|
||||||
$this->modified = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function setCart(Cart $cart)
|
|
||||||
{
|
|
||||||
$this->cart = $cart;
|
|
||||||
$this->modified = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getCart()
|
|
||||||
{
|
|
||||||
return $this->cart;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function isModified()
|
|
||||||
{
|
|
||||||
return $this->modified;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -88,4 +88,9 @@ final class TheliaEvents
|
|||||||
* Sent before customer insertion, to allow modules to create a custom customer reference.
|
* Sent before customer insertion, to allow modules to create a custom customer reference.
|
||||||
*/
|
*/
|
||||||
const CREATECUSTOMER_CUSTOMREF = "customer.creation.customref";
|
const CREATECUSTOMER_CUSTOMREF = "customer.creation.customref";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* sent when a new existing cat id duplicated. This append when current customer is different from current cart
|
||||||
|
*/
|
||||||
|
const CART_DUPLICATE = "cart.duplicate";
|
||||||
}
|
}
|
||||||
@@ -8,14 +8,6 @@ use Thelia\Model\Base\ProductSaleElementsQuery;
|
|||||||
|
|
||||||
class Cart extends BaseCart
|
class Cart extends BaseCart
|
||||||
{
|
{
|
||||||
|
|
||||||
protected $dispatcher;
|
|
||||||
|
|
||||||
public function setDispatcher(EventDispatcherInterface $dispatcher)
|
|
||||||
{
|
|
||||||
$this->dispatcher = $dispatcher;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function duplicate($token, Customer $customer = null)
|
public function duplicate($token, Customer $customer = null)
|
||||||
{
|
{
|
||||||
$cartItems = $this->getCartItems();
|
$cartItems = $this->getCartItems();
|
||||||
@@ -50,9 +42,4 @@ class Cart extends BaseCart
|
|||||||
|
|
||||||
return $cart;
|
return $cart;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function dispatchEvent($name)
|
|
||||||
{
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user