mobile_detect === null) { $this->mobile_detect = new MobileDetect(); } return $this->mobile_detect; } /** * Check if visitor's device is a mobile device * * @return bool */ public function isMobile(): bool { if ($this->is_mobile === null) { $mobileDetect = $this->getMobileDetect(); $this->is_mobile = $mobileDetect->isMobile(); } return $this->is_mobile; } /** * Check if visitor's device is a tablet device * * @return bool */ public function isTablet(): bool { if ($this->is_tablet === null) { $mobileDetect = $this->getMobileDetect(); $this->is_tablet = $mobileDetect->isTablet(); } return $this->is_tablet; } /** * Get mobile device * * @return bool */ public function getMobileDevice(): bool { if ($this->mobile_device === null) { $this->mobile_device = $this->isMobile() || $this->isTablet(); } return $this->mobile_device; } /** * Get device type * * @return int */ public function getDevice(): int { if ($this->isMobile()) { return self::DEVICE_MOBILE; } if ($this->isTablet()) { return self::DEVICE_TABLET; } return self::DEVICE_COMPUTER; } /** * Get current locale * * @return string */ public function getCurrentLocale() { if ($this->language) { return $this->language->locale; } return 'en-US'; } /** * Update customer in context * * @param Customer $customer */ public function updateCustomer(Customer $customer) { // Update the customer in context object $this->customer = $customer; // Update basic information in the cookie if ($this->cookie) { $this->cookie->id_customer = (int) $customer->id; $this->cookie->customer_lastname = $customer->lastname; $this->cookie->customer_firstname = $customer->firstname; $this->cookie->passwd = $customer->passwd; $this->cookie->logged = true; $customer->logged = true; $this->cookie->email = $customer->email; $this->cookie->is_guest = $customer->isGuest(); // Handle cart following if (Configuration::get('PS_CART_FOLLOWING') && (empty($this->cookie->id_cart) || Cart::getNbProducts((int) $this->cookie->id_cart) == 0) && $idCart = (int) Cart::lastNoneOrderedCart($this->customer->id) ) { $this->cart = new Cart($idCart); $this->cart->secure_key = $customer->secure_key; $this->cookie->id_guest = (int) $this->cart->id_guest; } else { // Initialize new visit if (!$this->cookie->id_guest) { Guest::setNewGuest($this->cookie); } // Update cart if exists if (Validate::isLoadedObject($this->cart)) { $this->cart->secure_key = $customer->secure_key; $this->cart->id_guest = (int) $this->cookie->id_guest; $this->cart->id_customer = (int) $customer->id; $this->cart->updateAddressId($this->cart->id_address_delivery, (int) Address::getFirstCustomerAddressId((int) $customer->id)); $this->cart->id_address_delivery = (int) Address::getFirstCustomerAddressId((int) $customer->id); $this->cart->id_address_invoice = (int) Address::getFirstCustomerAddressId((int) $customer->id); } } // Save cart and update cookie if (Validate::isLoadedObject($this->cart)) { $this->cart->save(); $this->cookie->id_cart = (int) $this->cart->id; } // Save cookie $this->cookie->write(); } } /** * Get translator * * @param bool $isInstaller * @return Translator */ public function getTranslator($isInstaller = false) { if (null !== $this->translator && $this->language && $this->language->locale === $this->translator->getLocale()) { return $this->translator; } if ($isInstaller || !$this->language) { $this->translator = $this->getTranslatorFromLocale('en-US'); } else { $this->translator = $this->getTranslatorFromLocale($this->language->locale); } return $this->translator; } /** * Get translator from locale * * @param string $locale * @return Translator */ public function getTranslatorFromLocale($locale) { $cacheDir = _PS_CACHE_DIR_ . 'translations'; $translator = new Translator($locale, null, $cacheDir, false); return $translator; } /** * Get computing precision * * @return int */ public function getComputingPrecision() { if ($this->priceComputingPrecision === null) { $this->priceComputingPrecision = (int) Configuration::get('PS_PRICE_COMPUTE_PRECISION'); } return $this->priceComputingPrecision; } /** * Check mobile context */ protected function checkMobileContext() { if ($this->getMobileDevice()) { $this->mode = self::MODE_STD; } } }