public function init()
 {
     parent::init();
     Plugin::getEventManager()->trigger('controller.init', $this);
     $this->view->setScriptPath(array_merge($this->view->getScriptPaths(), array(PIMCORE_WEBSITE_PATH . '/views/scripts/', PIMCORE_WEBSITE_PATH . '/views/layouts/', PIMCORE_WEBSITE_PATH . '/views/scripts/coreshop/')));
     $this->view->addHelperPath(CORESHOP_PATH . '/lib/CoreShop/View/Helper', 'CoreShop\\View\\Helper');
     $this->session = $this->view->session = Tool::getSession();
     /*
     if(!$this->session->country instanceof CoreShopCountry) {
         if($this->session->user instanceof \CoreShop\Plugin\User && count($this->session->user->getAddresses()) > 0)
         {
             $this->session->country = $this->session->user->getAddresses()->get(0)->getCountry();
         }
         else
             $this->session->country = Tool::getCountry();
     }
     */
     if ($this->getParam("currency")) {
         if (Currency::getById($this->getParam("currency")) instanceof Currency) {
             $this->session->currencyId = $this->getParam("currency");
         }
     }
     $this->view->country = Tool::getCountry();
     $this->enableLayout();
     $this->setLayout(Plugin::getLayout());
     $this->prepareCart();
     $this->view->isShop = true;
 }
 /**
  * Check if Cart is Valid for Condition
  *
  * @param Cart $cart
  * @param PriceRule $priceRule
  * @param bool|false $throwException
  * @return bool
  * @throws \Exception
  */
 public function checkCondition(Cart $cart, PriceRule $priceRule, $throwException = false)
 {
     $session = Tool::getSession();
     if ($cart->getUser() instanceof CoreShopUser && $session->user instanceof CoreShopUser) {
         if (!$cart->getUser()->getId() == $session->user->getId()) {
             if ($throwException) {
                 throw new \Exception("You cannot use this voucher");
             } else {
                 return false;
             }
         }
     }
     return true;
 }
 /**
  * Check if Cart is Valid for Condition
  *
  * @param Cart $cart
  * @param PriceRule $priceRule
  * @param bool|false $throwException
  * @return bool
  * @throws \Exception
  */
 public function checkCondition(Cart $cart, PriceRule $priceRule, $throwException = false)
 {
     $session = Tool::getSession();
     //Check Total For Customer
     if ($session->user instanceof CoreShopUser) {
         $orders = $session->user->getOrders();
         $cartRulesUsed = 0;
         foreach ($orders as $order) {
             if ($order->getPriceRule() instanceof PriceRule && $order->getPriceRule()->getId() == $priceRule->getId()) {
                 $cartRulesUsed++;
             }
         }
         if ($cartRulesUsed >= $this->getTotal()) {
             if ($throwException) {
                 throw new \Exception("You cannot use this voucher anymore (usage limit reached)");
             } else {
                 return false;
             }
         }
     }
     return true;
 }
 /**
  * calculates shipping costs for the cart
  *
  * @return int
  */
 public function getShipping()
 {
     $session = Tool::getSession();
     //check for existing shipping
     if (array_key_exists("shippingProvider", $session->order) && $session->order['deliveryProvider'] instanceof Shipping) {
         return $session->order['shippingProvider']->getShipping($this);
     }
     //get all provider and choose cheapest
     $providers = Plugin::getShippingProviders($this);
     $cheapestProvider = null;
     foreach ($providers as $p) {
         if ($cheapestProvider === null) {
             $cheapestProvider = $p;
         } else {
             if ($cheapestProvider->getShipping($this) > $p->getShipping($this)) {
                 $cheapestProvider = $p;
             }
         }
     }
     if ($cheapestProvider instanceof Shipping) {
         return $cheapestProvider->getShipping($this);
     }
     return 0;
 }
 /**
  * Calculate Product Price
  *
  * @depcreated: Should be replaced with PriceRules
  *
  * @return float|mixed
  * @throws \Exception
  */
 public function getProductPrice()
 {
     $cacheKey = "coreshop_product_price_" . $this->getId();
     if ($price = \Pimcore\Model\Cache::load($cacheKey)) {
         return $price;
     }
     $price = $this->getPrice();
     if (count($this->getSpecificPrice()) > 0) {
         $session = Tool::getSession();
         //Process Specific Prices
         foreach ($this->getSpecificPrice() as $sPrice) {
             $date = \Zend_Date::now();
             $hasCustomer = false;
             $hasCountry = false;
             $hasCurrency = false;
             if ($sPrice->getFrom() instanceof \Zend_Date) {
                 if ($date->get(\Zend_Date::TIMESTAMP) < $sPrice->getFrom()->get(\Zend_Date::TIMESTAMP)) {
                     continue;
                 }
             }
             if ($sPrice->getTo() instanceof \Zend_Date) {
                 if ($date->get(\Zend_Date::TIMESTAMP) > $sPrice->getTo()->get(\Zend_Date::TIMESTAMP)) {
                     continue;
                 }
             }
             if (count($sPrice->getCustomers()) > 0 && $session->user instanceof CoreShopUser) {
                 foreach ($sPrice->getCustomers() as $cust) {
                     if ($cust->getId() == $session->user->getId()) {
                         $hasCustomer = true;
                     }
                 }
             } else {
                 if (count($sPrice->getCustomers()) == 0) {
                     //Non is selected means all Users
                     $hasCustomer = true;
                 }
             }
             if (count($sPrice->getCountries()) > 0 && Tool::objectInList(Tool::getCountry(), $sPrice->getCountries())) {
                 $hasCountry = true;
             } else {
                 if (count($sPrice->getCountries()) == 0) {
                     //Non selected means all
                     $hasCountry = true;
                 }
             }
             if (count($sPrice->getCurrencies()) > 0 && Tool::objectInList(Tool::getCurrency(), $sPrice->getCurrencies())) {
                 $hasCurrency = true;
             } else {
                 if (count($sPrice->getCountries()) == 0) {
                     //Non selected means all
                     $hasCurrency = true;
                 }
             }
             if ($hasCountry && $hasCustomer && $hasCurrency) {
                 $price = $this->applySpecificPrice($sPrice);
                 break;
             }
         }
     }
     return Tool::convertToCurrency($price);
 }