fix cart item prices when not on default currency
This commit is contained in:
@@ -15,7 +15,12 @@ namespace Thelia\Action;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
use Thelia\Core\Event\Cart\CartEvent;
|
||||
use Thelia\Core\Event\Currency\CurrencyChangeEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Core\HttpFoundation\Request;
|
||||
use Thelia\Core\HttpFoundation\Session\Session;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Model\Currency;
|
||||
use Thelia\Model\ProductPrice;
|
||||
use Thelia\Model\ProductPriceQuery;
|
||||
use Thelia\Model\CartItem;
|
||||
@@ -32,6 +37,7 @@ use Thelia\Model\ConfigQuery;
|
||||
*/
|
||||
class Cart extends BaseAction implements EventSubscriberInterface
|
||||
{
|
||||
|
||||
/**
|
||||
*
|
||||
* add an article in the current cart
|
||||
@@ -44,19 +50,41 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
$newness = $event->getNewness();
|
||||
$append = $event->getAppend();
|
||||
$quantity = $event->getQuantity();
|
||||
$currency = $cart->getCurrency();
|
||||
$defaultCurrency = Currency::getDefaultCurrency();
|
||||
|
||||
$productSaleElementsId = $event->getProductSaleElementsId();
|
||||
$productId = $event->getProduct();
|
||||
|
||||
$cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId);
|
||||
|
||||
$price = 0.0;
|
||||
$promoPrice = 0.0;
|
||||
|
||||
if ($cartItem === null || $newness) {
|
||||
|
||||
$productPrice = ProductPriceQuery::create()
|
||||
->filterByProductSaleElementsId($productSaleElementsId)
|
||||
->filterByCurrencyId($cart->getCurrencyId())
|
||||
->findOne();
|
||||
|
||||
if (null === $productPrice || $productPrice->getFromDefaultCurrency()) {
|
||||
// need to calculate the prices
|
||||
$productPrice = ProductPriceQuery::create()
|
||||
->filterByProductSaleElementsId($productSaleElementsId)
|
||||
->filterByCurrencyId($defaultCurrency->getId())
|
||||
->findOne();
|
||||
if (null !== $productPrice) {
|
||||
$price = $productPrice->getPrice() * $currency->getRate();
|
||||
$promoPrice = $productPrice->getPromoPrice() * $currency->getRate();
|
||||
}
|
||||
} else {
|
||||
$price = $productPrice->getPrice();
|
||||
$promoPrice = $productPrice->getPromoPrice();
|
||||
}
|
||||
|
||||
$event->setCartItem(
|
||||
$this->doAddItem($event->getDispatcher(), $cart, $productId, $productPrice->getProductSaleElements(), $quantity, $productPrice)
|
||||
$this->doAddItem($event->getDispatcher(), $cart, $productId, $productPrice->getProductSaleElements(), $quantity, $price, $promoPrice)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -125,6 +153,87 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
}
|
||||
}
|
||||
|
||||
public function updateCart(CurrencyChangeEvent $event)
|
||||
{
|
||||
$session = $event->getRequest()->getSession();
|
||||
$cart = $session->getCart();
|
||||
if (null !== $cart) {
|
||||
$this->updateCartPrices($cart, $event->getCurrency());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* Refresh article's price
|
||||
*
|
||||
* @param \Thelia\Model\Base\Cart $cart
|
||||
* @param \Thelia\Model\Base\Currency $currency
|
||||
*/
|
||||
public function updateCartPrices(\Thelia\Model\Base\Cart $cart, Currency $currency)
|
||||
{
|
||||
|
||||
// get default currency
|
||||
$defaultCurrency = Currency::getDefaultCurrency();
|
||||
|
||||
$rate = 1.0;
|
||||
if ($currency !== $defaultCurrency) {
|
||||
$rate = $currency->getRate();
|
||||
}
|
||||
|
||||
//Tlog::getInstance()->addDebug("UPDATE_CURRENCY : " . $rate . ' :: ' . $currency->getId() . ' :: ' . $defaultCurrency->getId() );
|
||||
|
||||
$price = 0.0;
|
||||
$promoPrice = 0.0;
|
||||
|
||||
// cart item
|
||||
foreach ($cart->getCartItems() as $cartItem) {
|
||||
$productSaleElementsId = $cartItem->getProductSaleElementsId();
|
||||
$productPrice = ProductPriceQuery::create()
|
||||
->filterByCurrencyId($currency->getId())
|
||||
->filterByProductSaleElementsId($productSaleElementsId)
|
||||
->findOne();
|
||||
|
||||
if (null === $productPrice || $productPrice->getFromDefaultCurrency()) {
|
||||
// we take the default currency and apply the taxe rate
|
||||
$productPrice = ProductPriceQuery::create()
|
||||
->filterByCurrencyId($defaultCurrency->getId())
|
||||
->filterByProductSaleElementsId($productSaleElementsId)
|
||||
->findOne();
|
||||
|
||||
if (null === $productPrice) {
|
||||
//Tlog::getInstance()->addDebug("BOOM");
|
||||
continue;
|
||||
}
|
||||
//Tlog::getInstance()->addDebug("UPDATE_CURRENCY DYNAMIC ");
|
||||
$price = $productPrice->getPrice() * $rate;
|
||||
$promoPrice = $productPrice->getPromoPrice() * $rate;
|
||||
|
||||
} else {
|
||||
// product price for default currency or manual price for other currency
|
||||
//Tlog::getInstance()->addDebug("UPDATE_CURRENCY REAL ");
|
||||
$price = $productPrice->getPrice();
|
||||
$promoPrice = $productPrice->getPromoPrice();
|
||||
}
|
||||
|
||||
//Tlog::getInstance()->addDebug("UPDATE_CURRENCY : " . $price . " :: " . $promoPrice);
|
||||
|
||||
// We have
|
||||
$cartItem
|
||||
->setPrice($price)
|
||||
->setPromoPrice($promoPrice);
|
||||
|
||||
$cartItem->save();
|
||||
|
||||
}
|
||||
|
||||
// update the currency cart
|
||||
$cart->setCurrencyId($currency->getId());
|
||||
$cart->save();
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns an array of event names this subscriber wants to listen to.
|
||||
*
|
||||
@@ -152,6 +261,7 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
TheliaEvents::CART_DELETEITEM => array("deleteItem", 128),
|
||||
TheliaEvents::CART_UPDATEITEM => array("changeItem", 128),
|
||||
TheliaEvents::CART_CLEAR => array("clear", 128),
|
||||
TheliaEvents::CHANGE_DEFAULT_CURRENCY => array("updateCart", 128),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -179,11 +289,12 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
* @param int $productId
|
||||
* @param \Thelia\Model\ProductSaleElements $productSaleElements
|
||||
* @param float $quantity
|
||||
* @param ProductPrice $productPrice
|
||||
* @param float $price
|
||||
* @param float $promoPrice
|
||||
*
|
||||
* @return CartItem
|
||||
*/
|
||||
protected function doAddItem(EventDispatcherInterface $dispatcher, \Thelia\Model\Cart $cart, $productId, \Thelia\Model\ProductSaleElements $productSaleElements, $quantity, ProductPrice $productPrice)
|
||||
protected function doAddItem(EventDispatcherInterface $dispatcher, \Thelia\Model\Cart $cart, $productId, \Thelia\Model\ProductSaleElements $productSaleElements, $quantity, $price, $promoPrice)
|
||||
{
|
||||
$cartItem = new CartItem();
|
||||
$cartItem->setDisptacher($dispatcher);
|
||||
@@ -192,8 +303,8 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
->setProductId($productId)
|
||||
->setProductSaleElementsId($productSaleElements->getId())
|
||||
->setQuantity($quantity)
|
||||
->setPrice($productPrice->getPrice())
|
||||
->setPromoPrice($productPrice->getPromoPrice())
|
||||
->setPrice($price)
|
||||
->setPromoPrice($promoPrice)
|
||||
->setPromo($productSaleElements->getPromo())
|
||||
->setPriceEndOfLife(time() + ConfigQuery::read("cart.priceEOF", 60*60*24*30))
|
||||
->save();
|
||||
@@ -219,4 +330,14 @@ class Cart extends BaseAction implements EventSubscriberInterface
|
||||
->findOne();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the session from the current request
|
||||
*
|
||||
* @return \Thelia\Core\HttpFoundation\Session\Session
|
||||
*/
|
||||
protected function getSession()
|
||||
{
|
||||
return $this->request->getSession();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -89,6 +89,7 @@ trait CartTrait
|
||||
{
|
||||
$cart = new CartModel();
|
||||
$cart->setToken($this->generateCookie($session));
|
||||
$cart->setCurrency($session->getCurrency(true));
|
||||
|
||||
if (null !== $customer = $session->getCustomerUser()) {
|
||||
$cart->setCustomer($customer);
|
||||
|
||||
@@ -15,7 +15,6 @@ namespace Thelia\Controller\Admin;
|
||||
use Thelia\Core\Security\AccessManager;
|
||||
use Thelia\Core\Security\Resource\AdminResources;
|
||||
|
||||
|
||||
/**
|
||||
* Class ConfigurationController
|
||||
* @package Thelia\Controller\Admin
|
||||
@@ -31,4 +30,4 @@ class ConfigurationController extends BaseAdminController
|
||||
|
||||
return $this->render('configuration');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ namespace Thelia\Controller\Admin;
|
||||
use Thelia\Core\HttpFoundation\Response;
|
||||
use Thelia\Core\Security\AccessManager;
|
||||
use Thelia\Core\Security\Resource\AdminResources;
|
||||
use Thelia\Model\CustomerQuery;
|
||||
use Thelia\Model\NewsletterQuery;
|
||||
|
||||
/**
|
||||
@@ -64,4 +63,4 @@ class CustomerExportController extends BaseAdminController
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ namespace Thelia\Controller\Admin;
|
||||
use Thelia\Core\Security\AccessManager;
|
||||
use Thelia\Core\Security\Resource\AdminResources;
|
||||
|
||||
|
||||
/**
|
||||
* Class ExportController
|
||||
* @package Thelia\Controller\Admin
|
||||
@@ -32,4 +31,4 @@ class ExportController extends BaseAdminController
|
||||
|
||||
return $this->render('export');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ namespace Thelia\Controller\Admin;
|
||||
use Thelia\Core\Security\AccessManager;
|
||||
use Thelia\Core\Security\Resource\AdminResources;
|
||||
|
||||
|
||||
/**
|
||||
* Class ToolsController
|
||||
* @package Thelia\Controller\Admin
|
||||
@@ -31,4 +30,4 @@ class ToolsController extends BaseAdminController
|
||||
|
||||
return $this->render('tools');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -16,7 +16,6 @@ use Symfony\Component\Finder\Finder;
|
||||
use Thelia\Core\Security\Resource\AdminResources;
|
||||
use Thelia\Core\Security\AccessManager;
|
||||
use Thelia\Core\Translation\Translator;
|
||||
use Thelia\Log\Tlog;
|
||||
use Thelia\Model\Module;
|
||||
use Thelia\Model\ModuleQuery;
|
||||
use Thelia\Core\Template\TemplateHelper;
|
||||
@@ -147,8 +146,7 @@ class TranslationsController extends BaseAdminController
|
||||
;
|
||||
|
||||
$hasAdminIncludes = $finder->count() > 0;
|
||||
}
|
||||
catch (\InvalidArgumentException $ex) {
|
||||
} catch (\InvalidArgumentException $ex) {
|
||||
$hasAdminIncludes = false;
|
||||
}
|
||||
|
||||
|
||||
52
core/lib/Thelia/Core/Event/Currency/CurrencyChangeEvent.php
Normal file
52
core/lib/Thelia/Core/Event/Currency/CurrencyChangeEvent.php
Normal file
@@ -0,0 +1,52 @@
|
||||
<?php
|
||||
/*************************************************************************************/
|
||||
/* This file is part of the Thelia package. */
|
||||
/* */
|
||||
/* Copyright (c) OpenStudio */
|
||||
/* email : dev@thelia.net */
|
||||
/* web : http://www.thelia.net */
|
||||
/* */
|
||||
/* For the full copyright and license information, please view the LICENSE.txt */
|
||||
/* file that was distributed with this source code. */
|
||||
/*************************************************************************************/
|
||||
|
||||
namespace Thelia\Core\Event\Currency;
|
||||
|
||||
use Thelia\Core\HttpFoundation\Request;
|
||||
use Thelia\Model\Currency;
|
||||
|
||||
/**
|
||||
* Class CurrencyChangeEvent
|
||||
* @package Thelia\Core\Event\Currency
|
||||
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
|
||||
*/
|
||||
class CurrencyChangeEvent extends CurrencyEvent
|
||||
{
|
||||
/** @var Request $request */
|
||||
protected $request;
|
||||
|
||||
public function __construct(Currency $currency = null, Request $request = null)
|
||||
{
|
||||
parent::__construct($currency);
|
||||
$this->setRequest($request);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
*/
|
||||
public function setRequest(Request $request)
|
||||
{
|
||||
$this->request = $request;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Request
|
||||
*/
|
||||
public function getRequest()
|
||||
{
|
||||
return $this->request;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -184,7 +184,7 @@ abstract class BaseLoop
|
||||
'%name' => $loopName
|
||||
]
|
||||
);
|
||||
} else if ($value === '') {
|
||||
} elseif ($value === '') {
|
||||
if (!$argument->empty) {
|
||||
/* check if empty */
|
||||
$faultActor[] = $argument->name;
|
||||
|
||||
@@ -50,11 +50,11 @@ class TemplateHelper
|
||||
/**
|
||||
* Check if a template definition is the current active template
|
||||
*
|
||||
* @param TemplateDefinition $tplDefinition
|
||||
* @return bool true is the given template is the active template
|
||||
* @param TemplateDefinition $tplDefinition
|
||||
* @return bool true is the given template is the active template
|
||||
*/
|
||||
public function isActive(TemplateDefinition $tplDefinition) {
|
||||
|
||||
public function isActive(TemplateDefinition $tplDefinition)
|
||||
{
|
||||
switch ($tplDefinition->getType()) {
|
||||
case TemplateDefinition::FRONT_OFFICE:
|
||||
$tplVar = 'active-front-template';
|
||||
|
||||
@@ -22,7 +22,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
||||
use Symfony\Component\HttpFoundation\Session;
|
||||
|
||||
use Thelia\Core\Event\Currency\CurrencyEvent;
|
||||
use Thelia\Core\Event\Currency\CurrencyChangeEvent;
|
||||
use Thelia\Core\Event\TheliaEvents;
|
||||
use Thelia\Model;
|
||||
|
||||
@@ -142,7 +142,7 @@ class TheliaHttpKernel extends HttpKernel
|
||||
if ($request->query->has("currency")) {
|
||||
$currency = Model\CurrencyQuery::create()->findOneByCode($request->query->get("currency"));
|
||||
if ($currency) {
|
||||
$this->container->get("event_dispatcher")->dispatch(TheliaEvents::CHANGE_DEFAULT_CURRENCY, new CurrencyEvent($currency));
|
||||
$this->container->get("event_dispatcher")->dispatch(TheliaEvents::CHANGE_DEFAULT_CURRENCY, new CurrencyChangeEvent($currency, $request));
|
||||
}
|
||||
} else {
|
||||
$currency = $request->getSession()->getCurrency(false);
|
||||
|
||||
@@ -214,6 +214,7 @@ class Product extends BaseProduct
|
||||
->setPromoPrice($salePrice)
|
||||
->setPrice($basePrice)
|
||||
->setCurrencyId($currencyId)
|
||||
->setFromDefaultCurrency(false)
|
||||
->save($con)
|
||||
;
|
||||
|
||||
|
||||
@@ -31,7 +31,7 @@
|
||||
<thead>
|
||||
<tr>
|
||||
<th class="image"> </th>
|
||||
<th class="product">
|
||||
<th class="product">getPrice
|
||||
<span class="hidden-xs">{intl l="Product Name"}</span>
|
||||
<span class="visible-xs">{intl l="Name"}</span>
|
||||
</th>
|
||||
|
||||
Reference in New Issue
Block a user