Merge pull request #344 from bibich/fix-cart-currency

fix cart item prices when not on default currency
This commit is contained in:
Manuel Raynaud
2014-04-28 12:20:58 +02:00
14 changed files with 238 additions and 34 deletions

View File

@@ -15,12 +15,14 @@ namespace Thelia\Action;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Thelia\Core\Event\Cart\CartEvent; use Thelia\Core\Event\Cart\CartEvent;
use Thelia\Core\Event\Currency\CurrencyChangeEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Model\ProductPrice; use Thelia\Model\Base\ProductSaleElementsQuery;
use Thelia\Model\ProductPriceQuery; use Thelia\Model\Currency;
use Thelia\Model\CartItem; use Thelia\Model\CartItem;
use Thelia\Model\CartItemQuery; use Thelia\Model\CartItemQuery;
use Thelia\Model\ConfigQuery; use Thelia\Model\ConfigQuery;
use Thelia\Model\Tools\ProductPriceTools;
/** /**
* *
@@ -32,6 +34,7 @@ use Thelia\Model\ConfigQuery;
*/ */
class Cart extends BaseAction implements EventSubscriberInterface class Cart extends BaseAction implements EventSubscriberInterface
{ {
/** /**
* *
* add an article in the current cart * add an article in the current cart
@@ -44,6 +47,7 @@ class Cart extends BaseAction implements EventSubscriberInterface
$newness = $event->getNewness(); $newness = $event->getNewness();
$append = $event->getAppend(); $append = $event->getAppend();
$quantity = $event->getQuantity(); $quantity = $event->getQuantity();
$currency = $cart->getCurrency();
$productSaleElementsId = $event->getProductSaleElementsId(); $productSaleElementsId = $event->getProductSaleElementsId();
$productId = $event->getProduct(); $productId = $event->getProduct();
@@ -51,13 +55,16 @@ class Cart extends BaseAction implements EventSubscriberInterface
$cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId); $cartItem = $this->findItem($cart->getId(), $productId, $productSaleElementsId);
if ($cartItem === null || $newness) { if ($cartItem === null || $newness) {
$productPrice = ProductPriceQuery::create()
->filterByProductSaleElementsId($productSaleElementsId)
->findOne();
$event->setCartItem( $productSaleElements = ProductSaleElementsQuery::create()
$this->doAddItem($event->getDispatcher(), $cart, $productId, $productPrice->getProductSaleElements(), $quantity, $productPrice) ->findPk($productSaleElementsId);
);
if (null !== $productSaleElements) {
$productPrices = $productSaleElements->getPricesByCurrency($currency);
$event->setCartItem(
$this->doAddItem($event->getDispatcher(), $cart, $productId, $productSaleElements, $quantity, $productPrices)
);
}
} }
if ($append && $cartItem !== null) { if ($append && $cartItem !== null) {
@@ -125,6 +132,45 @@ 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\Cart $cart
* @param \Thelia\Model\Currency $currency
*/
public function updateCartPrices(\Thelia\Model\Cart $cart, Currency $currency)
{
// cart item
foreach ($cart->getCartItems() as $cartItem) {
$productSaleElements = $cartItem->getProductSaleElements();
$productPrice = $productSaleElements->getPricesByCurrency($currency);
$cartItem
->setPrice($productPrice->getPrice())
->setPromoPrice($productPrice->getPromoPrice());
$cartItem->save();
}
// update the currency cart
$cart->setCurrencyId($currency->getId());
$cart->save();
}
/** /**
* Returns an array of event names this subscriber wants to listen to. * Returns an array of event names this subscriber wants to listen to.
* *
@@ -152,6 +198,7 @@ class Cart extends BaseAction implements EventSubscriberInterface
TheliaEvents::CART_DELETEITEM => array("deleteItem", 128), TheliaEvents::CART_DELETEITEM => array("deleteItem", 128),
TheliaEvents::CART_UPDATEITEM => array("changeItem", 128), TheliaEvents::CART_UPDATEITEM => array("changeItem", 128),
TheliaEvents::CART_CLEAR => array("clear", 128), TheliaEvents::CART_CLEAR => array("clear", 128),
TheliaEvents::CHANGE_DEFAULT_CURRENCY => array("updateCart", 128),
); );
} }
@@ -175,15 +222,16 @@ class Cart extends BaseAction implements EventSubscriberInterface
/** /**
* try to attach a new item to an existing cart * try to attach a new item to an existing cart
* *
* @param \Thelia\Model\Cart $cart * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
* @param int $productId * @param \Thelia\Model\Cart $cart
* @param \Thelia\Model\ProductSaleElements $productSaleElements * @param int $productId
* @param float $quantity * @param \Thelia\Model\ProductSaleElements $productSaleElements
* @param ProductPrice $productPrice * @param float $quantity
* @param ProductPriceTools $productPrices
* *
* @return CartItem * @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, ProductPriceTools $productPrices)
{ {
$cartItem = new CartItem(); $cartItem = new CartItem();
$cartItem->setDisptacher($dispatcher); $cartItem->setDisptacher($dispatcher);
@@ -192,8 +240,8 @@ class Cart extends BaseAction implements EventSubscriberInterface
->setProductId($productId) ->setProductId($productId)
->setProductSaleElementsId($productSaleElements->getId()) ->setProductSaleElementsId($productSaleElements->getId())
->setQuantity($quantity) ->setQuantity($quantity)
->setPrice($productPrice->getPrice()) ->setPrice($productPrices->getPrice())
->setPromoPrice($productPrice->getPromoPrice()) ->setPromoPrice($productPrices->getPromoPrice())
->setPromo($productSaleElements->getPromo()) ->setPromo($productSaleElements->getPromo())
->setPriceEndOfLife(time() + ConfigQuery::read("cart.priceEOF", 60*60*24*30)) ->setPriceEndOfLife(time() + ConfigQuery::read("cart.priceEOF", 60*60*24*30))
->save(); ->save();

View File

@@ -89,6 +89,7 @@ trait CartTrait
{ {
$cart = new CartModel(); $cart = new CartModel();
$cart->setToken($this->generateCookie($session)); $cart->setToken($this->generateCookie($session));
$cart->setCurrency($session->getCurrency(true));
if (null !== $customer = $session->getCustomerUser()) { if (null !== $customer = $session->getCustomerUser()) {
$cart->setCustomer($customer); $cart->setCustomer($customer);

View File

@@ -15,7 +15,6 @@ namespace Thelia\Controller\Admin;
use Thelia\Core\Security\AccessManager; use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources; use Thelia\Core\Security\Resource\AdminResources;
/** /**
* Class ConfigurationController * Class ConfigurationController
* @package Thelia\Controller\Admin * @package Thelia\Controller\Admin
@@ -31,4 +30,4 @@ class ConfigurationController extends BaseAdminController
return $this->render('configuration'); return $this->render('configuration');
} }
} }

View File

@@ -15,7 +15,6 @@ namespace Thelia\Controller\Admin;
use Thelia\Core\HttpFoundation\Response; use Thelia\Core\HttpFoundation\Response;
use Thelia\Core\Security\AccessManager; use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources; use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Model\CustomerQuery;
use Thelia\Model\NewsletterQuery; use Thelia\Model\NewsletterQuery;
/** /**
@@ -64,4 +63,4 @@ class CustomerExportController extends BaseAdminController
} }
} }

View File

@@ -15,7 +15,6 @@ namespace Thelia\Controller\Admin;
use Thelia\Core\Security\AccessManager; use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources; use Thelia\Core\Security\Resource\AdminResources;
/** /**
* Class ExportController * Class ExportController
* @package Thelia\Controller\Admin * @package Thelia\Controller\Admin
@@ -32,4 +31,4 @@ class ExportController extends BaseAdminController
return $this->render('export'); return $this->render('export');
} }
} }

View File

@@ -15,7 +15,6 @@ namespace Thelia\Controller\Admin;
use Thelia\Core\Security\AccessManager; use Thelia\Core\Security\AccessManager;
use Thelia\Core\Security\Resource\AdminResources; use Thelia\Core\Security\Resource\AdminResources;
/** /**
* Class ToolsController * Class ToolsController
* @package Thelia\Controller\Admin * @package Thelia\Controller\Admin
@@ -31,4 +30,4 @@ class ToolsController extends BaseAdminController
return $this->render('tools'); return $this->render('tools');
} }
} }

View File

@@ -16,7 +16,6 @@ use Symfony\Component\Finder\Finder;
use Thelia\Core\Security\Resource\AdminResources; use Thelia\Core\Security\Resource\AdminResources;
use Thelia\Core\Security\AccessManager; use Thelia\Core\Security\AccessManager;
use Thelia\Core\Translation\Translator; use Thelia\Core\Translation\Translator;
use Thelia\Log\Tlog;
use Thelia\Model\Module; use Thelia\Model\Module;
use Thelia\Model\ModuleQuery; use Thelia\Model\ModuleQuery;
use Thelia\Core\Template\TemplateHelper; use Thelia\Core\Template\TemplateHelper;
@@ -147,8 +146,7 @@ class TranslationsController extends BaseAdminController
; ;
$hasAdminIncludes = $finder->count() > 0; $hasAdminIncludes = $finder->count() > 0;
} } catch (\InvalidArgumentException $ex) {
catch (\InvalidArgumentException $ex) {
$hasAdminIncludes = false; $hasAdminIncludes = false;
} }

View 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;
}
}

View File

@@ -184,7 +184,7 @@ abstract class BaseLoop
'%name' => $loopName '%name' => $loopName
] ]
); );
} else if ($value === '') { } elseif ($value === '') {
if (!$argument->empty) { if (!$argument->empty) {
/* check if empty */ /* check if empty */
$faultActor[] = $argument->name; $faultActor[] = $argument->name;

View File

@@ -50,11 +50,11 @@ class TemplateHelper
/** /**
* Check if a template definition is the current active template * Check if a template definition is the current active template
* *
* @param TemplateDefinition $tplDefinition * @param TemplateDefinition $tplDefinition
* @return bool true is the given template is the active template * @return bool true is the given template is the active template
*/ */
public function isActive(TemplateDefinition $tplDefinition) { public function isActive(TemplateDefinition $tplDefinition)
{
switch ($tplDefinition->getType()) { switch ($tplDefinition->getType()) {
case TemplateDefinition::FRONT_OFFICE: case TemplateDefinition::FRONT_OFFICE:
$tplVar = 'active-front-template'; $tplVar = 'active-front-template';

View File

@@ -22,7 +22,7 @@ use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\Session; use Symfony\Component\HttpFoundation\Session;
use Thelia\Core\Event\Currency\CurrencyEvent; use Thelia\Core\Event\Currency\CurrencyChangeEvent;
use Thelia\Core\Event\TheliaEvents; use Thelia\Core\Event\TheliaEvents;
use Thelia\Model; use Thelia\Model;
@@ -142,7 +142,7 @@ class TheliaHttpKernel extends HttpKernel
if ($request->query->has("currency")) { if ($request->query->has("currency")) {
$currency = Model\CurrencyQuery::create()->findOneByCode($request->query->get("currency")); $currency = Model\CurrencyQuery::create()->findOneByCode($request->query->get("currency"));
if ($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 { } else {
$currency = $request->getSession()->getCurrency(false); $currency = $request->getSession()->getCurrency(false);

View File

@@ -214,6 +214,7 @@ class Product extends BaseProduct
->setPromoPrice($salePrice) ->setPromoPrice($salePrice)
->setPrice($basePrice) ->setPrice($basePrice)
->setCurrencyId($currencyId) ->setCurrencyId($currencyId)
->setFromDefaultCurrency(false)
->save($con) ->save($con)
; ;

View File

@@ -3,6 +3,7 @@
namespace Thelia\Model; namespace Thelia\Model;
use Thelia\Model\Base\ProductSaleElements as BaseProductSaleElements; use Thelia\Model\Base\ProductSaleElements as BaseProductSaleElements;
use Thelia\Model\Tools\ProductPriceTools;
use Thelia\TaxEngine\Calculator; use Thelia\TaxEngine\Calculator;
class ProductSaleElements extends BaseProductSaleElements class ProductSaleElements extends BaseProductSaleElements
@@ -42,4 +43,51 @@ class ProductSaleElements extends BaseProductSaleElements
return round($taxCalculator->load($this->getProduct(), $country)->getTaxedPrice($this->getPromoPrice()), 2); return round($taxCalculator->load($this->getProduct(), $country)->getTaxedPrice($this->getPromoPrice()), 2);
} }
/**
* Get product prices for a specific currency.
*
* When the currency is not the default currency, the product prices for this currency is :
* - calculated according to the product price of the default currency. It happens when no product price exists for
* the currency or when the `from_default_currency` flag is set to `true`
* - set directly in the product price when `from_default_currency` is set to `false`
*
* @param Currency $currency
* @return ProductPriceTools
* @throws \RuntimeException
*/
public function getPricesByCurrency($currency)
{
$defaultCurrency = Currency::getDefaultCurrency();
$productPrice = ProductPriceQuery::create()
->filterByProductSaleElementsId($this->getId())
->filterByCurrencyId($currency->getId())
->findOne();
$price = 0.0;
$promoPrice = 0.0;
if (null === $productPrice || $productPrice->getFromDefaultCurrency()) {
// need to calculate the prices based on the product prices for the default currency
$productPrice = ProductPriceQuery::create()
->filterByProductSaleElementsId($this->getId())
->filterByCurrencyId($defaultCurrency->getId())
->findOne();
if (null !== $productPrice) {
$price = $productPrice->getPrice() * $currency->getRate() / $defaultCurrency->getRate();
$promoPrice = $productPrice->getPromoPrice() * $currency->getRate() / $defaultCurrency->getRate();
} else {
throw new \RuntimeException('Cannot find product prices for currency id: `' . $currency->getId() . '`');
}
} else {
$price = $productPrice->getPrice();
$promoPrice = $productPrice->getPromoPrice();
}
$productPriceTools = new ProductPriceTools($price, $promoPrice);
return $productPriceTools;
}
} }

View File

@@ -0,0 +1,60 @@
<?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\Model\Tools;
/**
* Utility class used to store price and promo price for a carte item.
*
* Class ProductPriceTools
* @package Thelia\Model\Tools
* @author Julien Chanséaume <jchanseaume@openstudio.fr>
*/
class ProductPriceTools
{
/**
* The value for the price field.
*
* @var double
*/
protected $price;
/**
* The value for the promoPrice field.
*
* @var double
*/
protected $promoPrice;
public function __construct($price, $promoPrice)
{
$this->price = $price;
$this->promoPrice = $promoPrice;
}
/**
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* @return float
*/
public function getPromoPrice()
{
return $this->promoPrice;
}
}