create a listener for adding the cart cookie in the response object
This commit is contained in:
@@ -97,7 +97,7 @@ trait CartTrait
|
|||||||
protected function createCart(Session $session)
|
protected function createCart(Session $session)
|
||||||
{
|
{
|
||||||
$cart = new CartModel();
|
$cart = new CartModel();
|
||||||
$cart->setToken($this->generateCookie());
|
$cart->setToken($this->generateCookie($session));
|
||||||
|
|
||||||
if (null !== $customer = $session->getCustomerUser()) {
|
if (null !== $customer = $session->getCustomerUser()) {
|
||||||
$cart->setCustomer($customer);
|
$cart->setCustomer($customer);
|
||||||
@@ -120,7 +120,7 @@ trait CartTrait
|
|||||||
*/
|
*/
|
||||||
protected function duplicateCart(EventDispatcherInterface $dispatcher, CartModel $cart, Session $session, Customer $customer = null)
|
protected function duplicateCart(EventDispatcherInterface $dispatcher, CartModel $cart, Session $session, Customer $customer = null)
|
||||||
{
|
{
|
||||||
$newCart = $cart->duplicate($this->generateCookie(), $customer, $dispatcher);
|
$newCart = $cart->duplicate($this->generateCookie($session), $customer, $dispatcher);
|
||||||
$session->setCart($newCart->getId());
|
$session->setCart($newCart->getId());
|
||||||
|
|
||||||
$cartEvent = new CartEvent($newCart);
|
$cartEvent = new CartEvent($newCart);
|
||||||
@@ -130,11 +130,13 @@ trait CartTrait
|
|||||||
return $cartEvent->getCart();
|
return $cartEvent->getCart();
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function generateCookie()
|
protected function generateCookie(Session $session)
|
||||||
{
|
{
|
||||||
$id = null;
|
$id = null;
|
||||||
if (ConfigQuery::read("cart.session_only", 0) == 0) {
|
if (ConfigQuery::read("cart.session_only", 0) == 0) {
|
||||||
$id = uniqid('', true);
|
$id = uniqid('', true);
|
||||||
|
$session->set('cart_use_cookie', $id);
|
||||||
|
|
||||||
setcookie(
|
setcookie(
|
||||||
"thelia_cart",
|
"thelia_cart",
|
||||||
$id,
|
$id,
|
||||||
|
|||||||
@@ -96,6 +96,10 @@
|
|||||||
<service id="mailer" class="Thelia\Mailer\MailerFactory">
|
<service id="mailer" class="Thelia\Mailer\MailerFactory">
|
||||||
<argument type="service" id="event_dispatcher"/>
|
<argument type="service" id="event_dispatcher"/>
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
<service id="response.listener" class="Thelia\Core\EventListener\ResponseListener">
|
||||||
|
<tag name="kernel.event_subscriber"/>
|
||||||
|
</service>
|
||||||
</services>
|
</services>
|
||||||
|
|
||||||
</config>
|
</config>
|
||||||
|
|||||||
84
core/lib/Thelia/Core/EventListener/ResponseListener.php
Normal file
84
core/lib/Thelia/Core/EventListener/ResponseListener.php
Normal file
@@ -0,0 +1,84 @@
|
|||||||
|
<?php
|
||||||
|
/*************************************************************************************/
|
||||||
|
/* */
|
||||||
|
/* Thelia */
|
||||||
|
/* */
|
||||||
|
/* Copyright (c) OpenStudio */
|
||||||
|
/* email : info@thelia.net */
|
||||||
|
/* web : http://www.thelia.net */
|
||||||
|
/* */
|
||||||
|
/* This program is free software; you can redistribute it and/or modify */
|
||||||
|
/* it under the terms of the GNU General Public License as published by */
|
||||||
|
/* the Free Software Foundation; either version 3 of the License */
|
||||||
|
/* */
|
||||||
|
/* This program is distributed in the hope that it will be useful, */
|
||||||
|
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
|
||||||
|
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
|
||||||
|
/* GNU General Public License for more details. */
|
||||||
|
/* */
|
||||||
|
/* You should have received a copy of the GNU General Public License */
|
||||||
|
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
|
||||||
|
/* */
|
||||||
|
/*************************************************************************************/
|
||||||
|
|
||||||
|
namespace Thelia\Core\EventListener;
|
||||||
|
|
||||||
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||||
|
use Symfony\Component\HttpFoundation\Cookie;
|
||||||
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
|
||||||
|
use Symfony\Component\HttpKernel\KernelEvents;
|
||||||
|
use Thelia\Model\ConfigQuery;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class ResponseListener
|
||||||
|
* @package Thelia\Core\EventListener
|
||||||
|
* @author Manuel Raynaud <mraynaud@openstudio.fr>
|
||||||
|
*/
|
||||||
|
class ResponseListener implements EventSubscriberInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
public function beforeResponse(FilterResponseEvent $event)
|
||||||
|
{
|
||||||
|
$session = $event->getRequest()->getSession();
|
||||||
|
|
||||||
|
if (null !== $id = $session->get("cart_use_cookie")) {
|
||||||
|
$response = $event->getResponse();
|
||||||
|
$response->headers->setCookie(new Cookie(
|
||||||
|
"thelia_cart",
|
||||||
|
$id,
|
||||||
|
time()+ConfigQuery::read("cart.cookie_lifetime", 60*60*24*365),
|
||||||
|
'/'
|
||||||
|
));
|
||||||
|
|
||||||
|
$session->set("cart_use_cookie", null);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns an array of event names this subscriber wants to listen to.
|
||||||
|
*
|
||||||
|
* The array keys are event names and the value can be:
|
||||||
|
*
|
||||||
|
* * The method name to call (priority defaults to 0)
|
||||||
|
* * An array composed of the method name to call and the priority
|
||||||
|
* * An array of arrays composed of the method names to call and respective
|
||||||
|
* priorities, or 0 if unset
|
||||||
|
*
|
||||||
|
* For instance:
|
||||||
|
*
|
||||||
|
* * array('eventName' => 'methodName')
|
||||||
|
* * array('eventName' => array('methodName', $priority))
|
||||||
|
* * array('eventName' => array(array('methodName1', $priority), array('methodName2'))
|
||||||
|
*
|
||||||
|
* @return array The event names to listen to
|
||||||
|
*
|
||||||
|
* @api
|
||||||
|
*/
|
||||||
|
public static function getSubscribedEvents()
|
||||||
|
{
|
||||||
|
return [
|
||||||
|
KernelEvents::RESPONSE => ['beforeResponse', 128]
|
||||||
|
];
|
||||||
|
}
|
||||||
|
}
|
||||||
Submodule local/modules/Klikandpay updated: 15cc539db9...a72c066e74
Reference in New Issue
Block a user