Jump to content
  • 0

Polecane produkty na stronie głównej - problem


luqa19

Question

Witam kupiłem sobie szablon, wgrałem wszystko ładnie działa, ale mam mały problem z wyświetlaniem polecanych produktów na stronie głównej. Moduł pobiera produkty od najstarszego, a ja chciałbym sobie wybierać produkty, które są wyświetlanie po nazwie produktu. 

 

Informacje: 

 

www.tonny.gr

prestashop 1.6

 

Ma ktoś jakiś pomysł na rozwiązanie tego problemu ?

Link to comment
Share on other sites

6 answers to this question

Recommended Posts

  • 0

standardowy moduł "Polecane produkty" rozwiązuje to tak


plik homefetured.php
 

public function _cacheProducts()
    {
        if (!isset(HomeFeatured::$cache_products))
        {
            $category = new Category((int)Configuration::get('HOME_FEATURED_CAT'), (int)Context::getContext()->language->id);
            $nb = (int)Configuration::get('HOME_FEATURED_NBR');
            if (Configuration::get('HOME_FEATURED_RANDOMIZE'))
                HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), null, null, false, true, true, ($nb ? $nb : 8));
            else
                HomeFeatured::$cache_products = $category->getProducts((int)Context::getContext()->language->id, 1, ($nb ? $nb : 8), 'position');
        }

        if (HomeFeatured::$cache_products === false || empty(HomeFeatured::$cache_products))
            return false;
    }

   
    
spróbuj w Twoim module (możesz mieć inny) zamienić position na name
 

Link to comment
Share on other sites

  • 0

@atomek

Kod wygląda tak :

<?php
/**
* 2007-2015 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    PrestaShop SA <[email protected]>
*  @copyright 2007-2015 PrestaShop SA
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*/

if (!defined('_PS_VERSION_'))
	exit;
class CsFeatureCategory extends Module
{
	private $_html;
	private $_config;
	private $_product_list;
	private $_settings_default;
	private $_selected_category;
	protected static $cache_filter_products;
	private $cs_name_config;
	private $size_big_product = 25;
	public function __construct()
	{
		$this->name = 'csfeaturecategory';
		$this->tab = 'others';
		$this->version = 1.1;
		$this->author = 'Codespot';
		$this->need_instance = 0;
		$this->bootstrap = true;
		parent::__construct();
		$this->displayName = $this->l('Cs Featured Category Block');
		$this->description = $this->l('Adds a block displaying feature category products.');
		$this->ps_versions_compliancy = array('min' => '1.6', 'max' => _PS_VERSION_);
		$this->cs_name_config = 'CS_CONFIG_FEATURE_CATEGORY_PRODUCT';
		$this->_settings_default = array (
			'title_block' => $this->l('Block feature category product'),
			'used_slider' => 1,
			'number_product_get' => 10,
			'number_line' => 1,
			'number_product_aline' => 3,
			'display_big_product' => 0,
			'display_image' => 1,
			'navigation' => 1,
			'pagination' => 0,
			'input_product_list' => '',
			'category' => Context::getContext()->shop->getCategory()
		);
		$this->getInitSettings();
	}
	public function getInitSettings()
	{
		$this->_config = (array)Tools::jsonDecode(Configuration::get($this->cs_name_config));
		$this->_config = (object)array_merge((array)$this->_settings_default, $this->_config); 
		$this->_selected_category = $this->_config->category;
		if (isset($this->_config->input_product_list))
		{
			$arrayIdProducts = explode('-', $this->_config->input_product_list);
			$id_products = Tools::substr(implode(',', $arrayIdProducts), 0, Tools::strlen(implode(',', $arrayIdProducts)) - 1);
			$this->_product_list = $this->getProductsLight($this->context->language->id, $id_products);
		}
		else 
			$this->_product_list = array();
	}
	public function install()
	{
		$this->_clearCache('*');
		if (!parent::install() || !$this->registerHook('displayHeader') || !$this->registerHook('addproduct') || !$this->registerHook('updateproduct') || !$this->registerHook('deleteproduct') || !$this->registerHook('categoryUpdate') || !$this->registerHook('displayTopHome') || !$this->registerHook('displayLeftColumn') || !$this->registerHook('displayRightColumn'))
			return false;
		if (!Configuration::hasKey($this->cs_name_config))
			Configuration::updateValue($this->cs_name_config, '');
		return true;
	}
	public function getProductsPerLineList()
	{
		$products_per_line = array();
		$i = 0;
		for ($i = 1; $i <= 8; $i++)
			$products_per_line[$i]['number'] = $i;
		return $products_per_line;
	}
	public function uninstall()
	{
		$this->_clearCache('*');
		if (parent::uninstall() == false || !Configuration::deleteByName($this->cs_name_config))
			return false;
		return true;
	}
	public function getContent()
	{
		$this->_postProcess();
		$this->initForm();
		return $this->_html;
	}
	public function checkValidate()
	{
		$configs = Tools::getValue('config');
		$errors = array();
		foreach ($configs as $key_option => $value_option)
		{
			$pos = strpos($key_option, 'number_');
			if ($pos !== false)
				if (isset($value_option) && (!$value_option || $value_option <= 0 || !Validate::isInt($value_option)))
					$errors[] = $this->l('An invalid '.$key_option.' has been specified.');
		}
		return $errors;
	}
	public function _postProcess()
	{
		if (Tools::isSubmit('saveConfig'))
		{
			$errors = $this->checkValidate();
			
			if (isset($errors) && count($errors))
				$this->_html .= $this->displayError(implode('<br />', $errors));
			else 
			{
				$configs = Tools::getValue('config');
				$configs['input_product_list'] = Tools::getValue('input_product_list');
				$configs['name_product_list'] = Tools::getValue('name_product_list');
				
				$languages = Language::getLanguages(false);
				foreach ($languages as $language)
				{
					if (Tools::getValue('title_block_'.$language['id_lang'].''))
						$configs['title_block'][$language['id_lang']] = Tools::getValue('title_block_'.$language['id_lang'].'');
				}
				
				$configs['category'] = Tools::getValue('id_parent');

				$config = Tools::jsonEncode($configs);
				if ($config)
				{
					Configuration::updateValue($this->cs_name_config, $config);
					$this->_clearCache('*');
					Tools::redirectAdmin($this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name.'&token='.Tools::getAdminTokenLite('AdminModules').'&successConfirmation');
				}
			}
		}
		else if (Tools::isSubmit('successConfirmation'))
			$this->_html .= $this->displayConfirmation($this->l('Your settings have been updated.'));
	}
	public function initForm()
	{
		$lang_default = Configuration::get('PS_LANG_DEFAULT');
		$fields_form = array();
		$this->context->controller->addJqueryPlugin('autocomplete');
		$this->context->controller->addJs($this->_path.'js/admin/csautocomplete.js');
		include(dirname(__FILE__).'/class/settings.php');
		$this->fields_form[0]['form'] = $fields_form; /* load form config */
		$helper = new HelperForm();
		$helper->module = $this;
		$helper->name_controller = $this->name;
		$helper->identifier = $this->identifier;
		$helper->token = Tools::getAdminTokenLite('AdminModules');
		$default_lang = (int)Configuration::get('PS_LANG_DEFAULT');
		foreach (Language::getLanguages(false) as $lang)
			$helper->languages[] = array(
				'id_lang' => $lang['id_lang'],
				'iso_code' => $lang['iso_code'],
				'name' => $lang['name'],
				'is_default' => ($default_lang == $lang['id_lang'] ? 1 : 0)
			);
		$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false).'&configure='.$this->name;
		$helper->default_form_language = $default_lang;
		$helper->allow_employee_form_lang = $default_lang;
		$helper->toolbar_scroll = true;
		$helper->title = $this->displayName;
		$helper->submit_action = 'saveConfig';
		if (Tools::getIsset('config'))
			$this->_config = (object)array_merge(Tools::getValue('config'), (array)$this->_config); 
		foreach (Language::getLanguages(false) as $lang)
		{
			if (is_object($this->_config->title_block))
				$title = (isset($this->_config->title_block->$lang['id_lang']) ? $this->_config->title_block->$lang['id_lang'] : $this->_config->title_block->$lang_default);
			else
				$title = (isset($this->_config->title_block->$lang['id_lang']) ? $this->_config->title_block->$lang['id_lang'] : $this->_config->title_block);
			$helper->fields_value['title_block'][(int)$lang['id_lang']] = Tools::getValue('title_block_'.(int)$lang['id_lang'].'', $title);
		}
		foreach ($this->fields_form[0]['form']['input'] as $field)
		{
			if ($field['name'] != 'title_block' && $field['name'] != 'product_list')
			{
				$option = str_replace('config[', '', $field['name']);
				$option = str_replace(']', '', $option);
				$helper->fields_value[''.$field['name'].''] = (isset($this->_config->$option) ? $this->_config->$option : '');
			}
		}
		$this->_html .= '<script>
				$(document).ready(function(){
					$(\'.ac_input\').keypress(function(){
						var v = $(\'#input_product_list\').val();
						if ( v != \'\')
							return false;
					});
			   });
			</script>'.$helper->generateForm($this->fields_form);
	}
	public static function getProductsLight($id_lang, $id_products, Context $context = null)
	{
		if (!$context)
			$context = Context::getContext();
		if ($id_products == '')	
			return;
		$sql = 'SELECT p.`id_product`, p.`reference`, pl.`name`
				FROM  `'._DB_PREFIX_.'product` p
				'.Shop::addSqlAssociation('product', 'p').'
				LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (
					p.`id_product` = pl.`id_product`
					AND pl.`id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('pl').'
				)
				WHERE p.`id_product` IN ('.$id_products.')';

		return Db::getInstance()->executeS($sql);
	}
	public static function getBigProduct($id_lang, $id_shop, $arrayIdProducts = null, $number_product_get = 10)
	{
		$id_products = Tools::substr(implode(',', $arrayIdProducts), 0, Tools::strlen(implode(',', $arrayIdProducts)) - 1);
		if (!$id_products)
			return;
		$productsImages = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
						SELECT MAX(image_shop.id_image) id_image, p.id_product, il.legend, product_shop.active, pl.name, pl.description_short, pl.link_rewrite, cl.link_rewrite AS category_rewrite,p.out_of_stock,p.ean13,p.id_category_default,product_shop.available_for_order,product_shop.minimal_quantity, product_shop.customizable,product_shop.show_price
						FROM '._DB_PREFIX_.'product p
						'.Shop::addSqlAssociation('product', 'p').'
						LEFT JOIN '._DB_PREFIX_.'product_lang pl ON (pl.id_product = p.id_product'.Shop::addSqlRestrictionOnLang('pl').')
						LEFT JOIN '._DB_PREFIX_.'image i ON (i.id_product = p.id_product)'.
						Shop::addSqlAssociation('image', 'i', false, 'image_shop.cover=1').'
						LEFT JOIN '._DB_PREFIX_.'image_lang il ON (il.id_image = image_shop.id_image)
						LEFT JOIN '._DB_PREFIX_.'category_lang cl ON (cl.id_category = product_shop.id_category_default'.Shop::addSqlRestrictionOnLang('cl').')
						WHERE p.id_product IN ('.$id_products.')
						AND pl.id_lang = '.$id_lang.'
						AND cl.id_lang = '.$id_lang.'
						GROUP BY product_shop.id_product');
		$productsImagesArray = array();
		foreach ($productsImages as $pi)
		{
			if (count($productsImagesArray) < $number_product_get)
				$productsImagesArray[$pi['id_product']] = $pi;
		}
		$products = Product::getProductsProperties((int)$id_lang, $productsImagesArray);
		return $products;
	}
	public function callGetProductList()
	{
		$id_lang = $this->context->language->id;
		$number_product_get = $this->_config->number_product_get;
		$category = new Category($this->_selected_category, (int)Context::getContext()->language->id);
		$products = $category->getProducts($id_lang, 1, $number_product_get, 'position');
		return $products;
	}
	public function callGetCategoryList($id_lang)
	{

		if (!isset($this->_selected_category) || empty($this->_selected_category))
			return;
		$number_product_get = $this->_config->number_product_get;
		$categories = array();
		foreach ($this->_selected_category as $key => $id_cat)
		{
			$category = new Category($id_cat, $id_lang);
			$product_list = $category->getProducts($id_lang, 1, $number_product_get, 'date_add');
			$categories[$key]['category'] = $category;
			if ($this->_config->display_big_product == 1)
			{
				$big_product = reset($product_list);
				array_shift($product_list);
				$categories[$key]['big_product'] = $big_product;
			}
			$categories[$key]['product_list'] = $product_list;
		}
		return $categories;
	}
	public function hookDisplayHeader()
	{
		$this->context->controller->addCSS(($this->_path).'css/'.$this->name.'.css', 'all');
	}
	public function hookDisplayHome()
	{
		if (!$this->isCached($this->name.'.tpl', $this->getCacheId()))
		{

			$id_lang = $this->context->language->id;
			$categories = $this->callGetCategoryList($id_lang);
			$id_shop = $this->context->shop->id;
			//$products = $this->callGetProductList();
			if (!isset($this->_config->title_block->$id_lang) || $this->_config->title_block->$id_lang == '')
				$id_lang = Configuration::get('PS_LANG_DEFAULT');
			
			if ($this->_config->display_big_product == 1 && !empty($this->_product_list))
			{
				$w_big_product = $this->size_big_product;
				$arrayIdProduct = explode('-', $this->_config->input_product_list);
				$big_product = $this->getBigProduct($id_lang, $id_shop, $arrayIdProduct, 1);
				$this->context->smarty->assign(array(
					'cs_big_product'.$this->name => reset($big_product)
				));
			}
			else 
				$w_big_product = 0;
			$this->context->smarty->assign(array(
				'cs_categories' => $categories,
				'cs_config' => $this->_config,
				'cs_title' => (isset($this->_config->title_block->$id_lang) ? $this->_config->title_block->$id_lang : $this->_config->title_block),
				'w_big_product' => $w_big_product,
				'cs_id' => $this->name
			));
		}
		if (CsFeatureCategory::$cache_filter_products === false)
			return false;
		return $this->display(__FILE__, $this->name.'.tpl', $this->getCacheId());
	}
	public function hookDisplayTopHome()
	{
		return $this->hookDisplayHome();
	}
	public function hookDisplayBottomHome()
	{
		return $this->hookDisplayHome();
	}
	public function hookDisplayRightColumn()
	{
			if (!$this->isCached($this->name.'_sidebar.tpl', $this->getCacheId()))
			{
				
				$id_lang = $this->context->language->id;
				$id_shop = $this->context->shop->id;
				$products = $this->callGetProductList();
				if (!isset($this->_config->title_block->$id_lang) || $this->_config->title_block->$id_lang == '')
					$id_lang = Configuration::get('PS_LANG_DEFAULT');
				if (!isset(CsFeatureCategory::$cache_filter_products))
					CsFeatureCategory::$cache_filter_products = $products;	
				if ($this->_config->display_big_product == 1 && !empty($this->_product_list))
				{
					$w_big_product = $this->size_big_product;
					$arrayIdProduct = explode('-', $this->_config->input_product_list);
					$big_product = $this->getBigProduct($id_lang, $id_shop, $arrayIdProduct, 1);
					$this->context->smarty->assign(array(
						'cs_big_product'.$this->name => reset($big_product)
					));
				}
				else 
					$w_big_product = 0;
				$this->context->smarty->assign(array(
					'cs_products' => $products,
					'cs_config' => $this->_config,
					'cs_title' => (isset($this->_config->title_block->$id_lang) ? $this->_config->title_block->$id_lang : $this->_config->title_block),
					'w_big_product' => $w_big_product,
					'cs_id' => $this->name
				));
			}
			if (CsFeatureCategory::$cache_filter_products === false)
				return false;
				
			return $this->display(__FILE__, $this->name.'_sidebar.tpl', $this->getCacheId($this->name.'-sidebar'));
	}
	public function hookDisplayLeftColumn()
	{
		return $this->hookDisplayRightColumn();
	}
	
	public function hookAddProduct($params)
	{
		$this->_clearCache('*');
	}
	public function hookUpdateProduct($params)
	{
		$this->_clearCache('*');
	}
	public function hookDeleteProduct($params)
	{
		$this->_clearCache('*');
	}
	public function hookCategoryUpdate($params)
	{
		$this->_clearCache('*');
	}
	public function _clearCache($template, $cache_id = null, $compile_id = null)
	{
		parent::_clearCache($this->name.'.tpl');
		parent::_clearCache($this->name.'_sidebar.tpl', $this->getCacheId($this->name.'-sidebar'));
	}
}

Jak go zmodyfikować, żeby pobierał produkty tak jak ten standardowy moduł z kategorii głównej HOME ?

Link to comment
Share on other sites

  • 0

kolejność wyświetlania spróbuj zmienić tu:
 

public function callGetProductList()
{
    $id_lang = $this->context->language->id;
    $number_product_get = $this->_config->number_product_get;
    $category = new Category($this->_selected_category, (int)Context::getContext()->language->id);
    $products = $category->getProducts($id_lang, 1, $number_product_get, 'position');
    return $products;
}

   
position zamień na name, po zmianie wyczyść cache sklepu


kategorie z której będą wyświetlane produkty pewnie ustawisz w konfiguracji modułu


co masz na myśli "żeby pobierał produkty tak jak ten standardowy moduł z kategorii głównej HOME"?

Link to comment
Share on other sites

  • 0

chyba się pospieszyłem ;) w tej funkcji zmień date_add na name,
 

public function callGetCategoryList($id_lang)
    {

        if (!isset($this->_selected_category) || empty($this->_selected_category))
            return;
        $number_product_get = $this->_config->number_product_get;
        $categories = array();
        foreach ($this->_selected_category as $key => $id_cat)
        {
            $category = new Category($id_cat, $id_lang);
            $product_list = $category->getProducts($id_lang, 1, $number_product_get, 'date_add');
            $categories[$key]['category'] = $category;
            if ($this->_config->display_big_product == 1)
            {
                $big_product = reset($product_list);
                array_shift($product_list);
                $categories[$key]['big_product'] = $big_product;
            }
            $categories[$key]['product_list'] = $product_list;
        }
        return $categories;
    }
  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...