/** * Assign template vars related to page content. * * @see FrontController::initContent() */ public function initContent() { parent::initContent(); if (!$this->errors) { if (Pack::isPack((int) $this->product->id) && !Pack::isInStock((int) $this->product->id)) { $this->product->quantity = 0; } $this->product->description = $this->transformDescriptionWithImg($this->product->description); $priceDisplay = Product::getTaxCalculationMethod((int) $this->context->cookie->id_customer); $productPrice = 0; $productPriceWithoutReduction = 0; if (!$priceDisplay || $priceDisplay == 2) { $productPrice = $this->product->getPrice(true, null, 6); $productPriceWithoutReduction = $this->product->getPriceWithoutReduct(false, null); } elseif ($priceDisplay == 1) { $productPrice = $this->product->getPrice(false, null, 6); $productPriceWithoutReduction = $this->product->getPriceWithoutReduct(true, null); } if (Tools::isSubmit('submitCustomizedData')) { // If cart has not been saved, we need to do it so that customization fields can have an id_cart // We check that the cookie exists first to avoid ghost carts if (!$this->context->cart->id && isset($_COOKIE[$this->context->cookie->getName()])) { $this->context->cart->add(); $this->context->cookie->id_cart = (int) $this->context->cart->id; } $this->pictureUpload(); $this->textRecord(); } elseif (Tools::getIsset('deletePicture') && !$this->context->cart->deleteCustomizationToProduct($this->product->id, Tools::getValue('deletePicture'))) { $this->errors[] = $this->trans('An error occurred while deleting the selected picture.', array(), 'Shop.Notifications.Error'); } $pictures = array(); $text_fields = array(); if ($this->product->customizable) { $files = $this->context->cart->getProductCustomization($this->product->id, Product::CUSTOMIZE_FILE, true); foreach ($files as $file) { $pictures['pictures_' . $this->product->id . '_' . $file['index']] = $file['value']; } $texts = $this->context->cart->getProductCustomization($this->product->id, Product::CUSTOMIZE_TEXTFIELD, true); foreach ($texts as $text_field) { $text_fields['textFields_' . $this->product->id . '_' . $text_field['index']] = str_replace('<br />', "\n", $text_field['value']); } } $this->context->smarty->assign(array('pictures' => $pictures, 'textFields' => $text_fields)); $this->product->customization_required = false; $customization_fields = $this->product->customizable ? $this->product->getCustomizationFields($this->context->language->id) : false; if (is_array($customization_fields)) { foreach ($customization_fields as &$customization_field) { if ($customization_field['type'] == 0) { $customization_field['key'] = 'pictures_' . $this->product->id . '_' . $customization_field['id_customization_field']; } elseif ($customization_field['type'] == 1) { $customization_field['key'] = 'textFields_' . $this->product->id . '_' . $customization_field['id_customization_field']; } } unset($customization_field); } // Assign template vars related to the category + execute hooks related to the category $this->assignCategory(); // Assign template vars related to the price and tax $this->assignPriceAndTax(); // Assign attributes combinations to the template $this->assignAttributesCombinations(); // Pack management $pack_items = Pack::isPack($this->product->id) ? Pack::getItemTable($this->product->id, $this->context->language->id, true) : array(); $assembler = new ProductAssembler($this->context); $presenter = new ProductListingPresenter(new ImageRetriever($this->context->link), $this->context->link, new PriceFormatter(), new ProductColorsRetriever(), $this->getTranslator()); $presentationSettings = $this->getProductPresentationSettings(); $presentedPackItems = array(); foreach ($pack_items as $item) { $presentedPackItems[] = $presenter->present($this->getProductPresentationSettings(), $assembler->assembleProduct($item), $this->context->language); } $this->context->smarty->assign('packItems', $presentedPackItems); $this->context->smarty->assign('noPackPrice', $this->product->getNoPackPrice()); $this->context->smarty->assign('displayPackPrice', $pack_items && $productPrice < $this->product->getNoPackPrice() ? true : false); $this->context->smarty->assign('packs', Pack::getPacksTable($this->product->id, $this->context->language->id, true, 1)); $accessories = $this->product->getAccessories($this->context->language->id); if (is_array($accessories)) { foreach ($accessories as &$accessory) { $accessory = $presenter->present($presentationSettings, Product::getProductProperties($this->context->language->id, $accessory, $this->context), $this->context->language); } unset($accessory); } if ($this->product->customizable) { $customization_datas = $this->context->cart->getProductCustomization($this->product->id, null, true); } $product_for_template = $this->getTemplateVarProduct(); $this->context->smarty->assign(array('priceDisplay' => $priceDisplay, 'productPriceWithoutReduction' => $productPriceWithoutReduction, 'customizationFields' => $customization_fields, 'id_customization' => empty($customization_datas) ? null : $customization_datas[0]['id_customization'], 'accessories' => $accessories, 'product' => $product_for_template, 'displayUnitPrice' => !empty($this->product->unity) && $this->product->unit_price_ratio > 0.0 ? true : false, 'product_manufacturer' => new Manufacturer((int) $this->product->id_manufacturer, $this->context->language->id))); // Assign attribute groups to the template $this->assignAttributesGroups($product_for_template); } }
protected function getProducts() { $category = new Category((int) Configuration::get('HOME_FEATURED_CAT')); $searchProvider = new CategoryProductSearchProvider($this->context->getTranslator(), $category); $context = new ProductSearchContext($this->context); $query = new ProductSearchQuery(); $nProducts = Configuration::get('HOME_FEATURED_NBR'); if ($nProducts < 0) { $nProducts = 12; } $query->setResultsPerPage($nProducts)->setPage(1); if (Configuration::get('HOME_FEATURED_RANDOMIZE')) { $query->setSortOrder(SortOrder::random()); } else { $query->setSortOrder(new SortOrder('product', 'position', 'asc')); } $result = $searchProvider->runQuery($context, $query); $assembler = new ProductAssembler($this->context); $presenterFactory = new ProductPresenterFactory($this->context); $presentationSettings = $presenterFactory->getPresentationSettings(); $presenter = new ProductListingPresenter(new ImageRetriever($this->context->link), $this->context->link, new PriceFormatter(), new ProductColorsRetriever(), $this->context->getTranslator()); $products_for_template = []; foreach ($result->getProducts() as $rawProduct) { $products_for_template[] = $presenter->present($presentationSettings, $assembler->assembleProduct($rawProduct), $this->context->language); } return $products_for_template; }