From d4391971330d84631ed7bb1366cbd82adb7f83e1 Mon Sep 17 00:00:00 2001 From: Manuel Raynaud Date: Fri, 30 May 2014 17:07:55 +0200 Subject: [PATCH] translate product in car. Fix #416 --- core/lib/Thelia/Core/Template/Loop/Cart.php | 4 ++-- core/lib/Thelia/Model/CartItem.php | 18 ++++++++++++++++++ core/lib/Thelia/Model/Lang.php | 12 +++++++++--- 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/core/lib/Thelia/Core/Template/Loop/Cart.php b/core/lib/Thelia/Core/Template/Loop/Cart.php index 4f635ddeb..91c22a7b4 100644 --- a/core/lib/Thelia/Core/Template/Loop/Cart.php +++ b/core/lib/Thelia/Core/Template/Loop/Cart.php @@ -82,9 +82,9 @@ class Cart extends BaseLoop implements ArraySearchLoopInterface public function parseResults(LoopResult $loopResult) { $taxCountry = $this->container->get('thelia.taxEngine')->getDeliveryCountry(); - + $locale = $this->request->getSession()->getLang()->getLocale(); foreach ($loopResult->getResultDataCollection() as $cartItem) { - $product = $cartItem->getProduct(); + $product = $cartItem->getProduct(null, $locale); $productSaleElement = $cartItem->getProductSaleElements(); $loopResultRow = new LoopResultRow(); diff --git a/core/lib/Thelia/Model/CartItem.php b/core/lib/Thelia/Model/CartItem.php index 7818f63d2..ad25ec13e 100644 --- a/core/lib/Thelia/Model/CartItem.php +++ b/core/lib/Thelia/Model/CartItem.php @@ -8,6 +8,7 @@ use Thelia\Core\Event\TheliaEvents; use Thelia\Model\Base\CartItem as BaseCartItem; use Thelia\Core\Event\Cart\CartEvent; +use Thelia\Model\ConfigQuery; use Thelia\TaxEngine\Calculator; class CartItem extends BaseCartItem @@ -89,6 +90,23 @@ class CartItem extends BaseCartItem return $this->getPromo() == 1 ? $this->getPromoPrice() : $this->getPrice(); } + public function getProduct(ConnectionInterface $con = null, $locale = null) + { + $product = parent::getProduct($con); + + $translation = $product->getTranslation($locale); + + if ($translation->isNew()) { + if (ConfigQuery::getDefaultLangWhenNoTranslationAvailable()) { + $locale = Lang::getDefaultLanguage()->getLocale(); + } + } + + $product->setLocale($locale); + + return $product; + } + public function getRealTaxedPrice(Country $country) { return $this->getPromo() == 1 ? $this->getTaxedPromoPrice($country) : $this->getTaxedPrice($country); diff --git a/core/lib/Thelia/Model/Lang.php b/core/lib/Thelia/Model/Lang.php index a0baa80c2..546eb7b20 100644 --- a/core/lib/Thelia/Model/Lang.php +++ b/core/lib/Thelia/Model/Lang.php @@ -17,6 +17,9 @@ use Thelia\Model\Map\LangTableMap; class Lang extends BaseLang { use \Thelia\Model\Tools\ModelEventDispatcherTrait; + + protected static $defaultLanguage; + /** * Return the default language object, using a local variable to cache it. * @@ -24,10 +27,13 @@ class Lang extends BaseLang */ public static function getDefaultLanguage() { - $default_lang = LangQuery::create()->findOneByByDefault(1); + if (null === self::$defaultLanguage) { + self::$defaultLanguage = LangQuery::create()->findOneByByDefault(1); - if ($default_lang == null) throw new \RuntimeException("No default language is defined. Please define one."); - return $default_lang; + if (self::$defaultLanguage == null) throw new \RuntimeException("No default language is defined. Please define one."); + } + + return self::$defaultLanguage; } public function toggleDefault()