Jump to content

How to Show Product Attribute Option in Product List


jahyax

Recommended Posts

  • 2 weeks later...

Hi everybody.

I implemented the solution on Prestashop 1.6.1.1 and it works very well.

The problem I have is that it doesn't show attributes when I use the page number to move or when I use "previous" - "next" or "show all".

Further, attributes doesn't appear when I select an attribute with the layered navigation box.

Can anybody help me solve this?

Many thanks.

Matteo

Link to comment
Share on other sites

Somebody please lay out the code for SearchController and ManufacturerController

 

we have solved SearchController.php doing this modification:

 

at the end of the public function init() (at raw 50) we added:

 

    public function getProductAttributeCombinations($products) {

    $combinations = array();

 

    foreach ($products as $product)

    {

        // load product object

        $product = new Product ($product['id_product'], $this->context->language->id);

 

        // get the product combinations data

        // create array combinations with key = id_product

        $combinations[$product->id] = $product->getAttributeCombinations($this->context->language->id);

    }

 

    return $combinations;

}

 

Further to this we added few raws of code inside the public function initContent() - I'm copying all the code; the raws with comment //CMD has been added:

 

    public function initContent()

    {

        $original_query = Tools::getValue('q');

        $query = Tools::replaceAccentedChars(urldecode($original_query));

        if ($this->ajax_search) {

            $searchResults = Search::find((int)(Tools::getValue('id_lang')), $query, 1, 10, 'position', 'desc', true);

            if (is_array($searchResults)) {

                foreach ($searchResults as &$product) {

                    $product['product_link'] = $this->context->link->getProductLink($product['id_product'], $product['prewrite'], $product['crewrite']);

                }

                Hook::exec('actionSearch', array('expr' => $query, 'total' => count($searchResults)));

            }

            $this->ajaxDie(Tools::jsonEncode($searchResults));

        }

 

        //Only controller content initialization when the user use the normal search

        parent::initContent();

        

        $product_per_page = isset($this->context->cookie->nb_item_per_page) ? (int)$this->context->cookie->nb_item_per_page : Configuration::get('PS_PRODUCTS_PER_PAGE');

 

        if ($this->instant_search && !is_array($query)) {

            $this->productSort();

            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));

            $this->p = abs((int)(Tools::getValue('p', 1)));

            $search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc');

            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));

            $nbProducts = $search['total'];

            $this->pagination($nbProducts);

 

            $combinations = $this->getProductAttributeCombinations($search['result']);        // CMD

               

            $this->addColorsToProductList($search['result']);

 

            $this->context->smarty->assign(array(

                'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module

                'combinations' => $combinations, // CMD

                'search_products' => $search['result'],

                'nbProducts' => $search['total'],

                'search_query' => $original_query,

                'instant_search' => $this->instant_search,

                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));

        } elseif (($query = Tools::getValue('search_query', Tools::getValue('ref'))) && !is_array($query)) {

            $this->productSort();

            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));

            $this->p = abs((int)(Tools::getValue('p', 1)));

            $original_query = $query;

            $query = Tools::replaceAccentedChars(urldecode($query));

            $search = Search::find($this->context->language->id, $query, $this->p, $this->n, $this->orderBy, $this->orderWay);

            if (is_array($search['result'])) {

                foreach ($search['result'] as &$product) {

                    $product['link'] .= (strpos($product['link'], '?') === false ? '?' : '&').'search_query='.urlencode($query).'&results='.(int)$search['total'];

                }

            }

 

            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));

            $nbProducts = $search['total'];

            $this->pagination($nbProducts);

      

            $combinations = $this->getProductAttributeCombinations($search['result']);      //CMD

 

 

by the way does your shop shows attributes when moving through pages with "previous" - "next" or "show all" or when you select an attribute with the layered navigation box?

Bye

Matteo

      

  • Like 1
Link to comment
Share on other sites

we have solved SearchController.php doing this modification:

 

at the end of the public function init() (at raw 50) we added:

 

    public function getProductAttributeCombinations($products) {

    $combinations = array();

 

    foreach ($products as $product)

    {

        // load product object

        $product = new Product ($product['id_product'], $this->context->language->id);

 

        // get the product combinations data

        // create array combinations with key = id_product

        $combinations[$product->id] = $product->getAttributeCombinations($this->context->language->id);

    }

 

    return $combinations;

}

 

Further to this we added few raws of code inside the public function initContent() - I'm copying all the code; the raws with comment //CMD has been added:

 

    public function initContent()

    {

        $original_query = Tools::getValue('q');

        $query = Tools::replaceAccentedChars(urldecode($original_query));

        if ($this->ajax_search) {

            $searchResults = Search::find((int)(Tools::getValue('id_lang')), $query, 1, 10, 'position', 'desc', true);

            if (is_array($searchResults)) {

                foreach ($searchResults as &$product) {

                    $product['product_link'] = $this->context->link->getProductLink($product['id_product'], $product['prewrite'], $product['crewrite']);

                }

                Hook::exec('actionSearch', array('expr' => $query, 'total' => count($searchResults)));

            }

            $this->ajaxDie(Tools::jsonEncode($searchResults));

        }

 

        //Only controller content initialization when the user use the normal search

        parent::initContent();

        

        $product_per_page = isset($this->context->cookie->nb_item_per_page) ? (int)$this->context->cookie->nb_item_per_page : Configuration::get('PS_PRODUCTS_PER_PAGE');

 

        if ($this->instant_search && !is_array($query)) {

            $this->productSort();

            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));

            $this->p = abs((int)(Tools::getValue('p', 1)));

            $search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc');

            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));

            $nbProducts = $search['total'];

            $this->pagination($nbProducts);

 

            $combinations = $this->getProductAttributeCombinations($search['result']);        // CMD

               

            $this->addColorsToProductList($search['result']);

 

            $this->context->smarty->assign(array(

                'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module

                'combinations' => $combinations, // CMD

                'search_products' => $search['result'],

                'nbProducts' => $search['total'],

                'search_query' => $original_query,

                'instant_search' => $this->instant_search,

                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));

        } elseif (($query = Tools::getValue('search_query', Tools::getValue('ref'))) && !is_array($query)) {

            $this->productSort();

            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));

            $this->p = abs((int)(Tools::getValue('p', 1)));

            $original_query = $query;

            $query = Tools::replaceAccentedChars(urldecode($query));

            $search = Search::find($this->context->language->id, $query, $this->p, $this->n, $this->orderBy, $this->orderWay);

            if (is_array($search['result'])) {

                foreach ($search['result'] as &$product) {

                    $product['link'] .= (strpos($product['link'], '?') === false ? '?' : '&').'search_query='.urlencode($query).'&results='.(int)$search['total'];

                }

            }

 

            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));

            $nbProducts = $search['total'];

            $this->pagination($nbProducts);

      

            $combinations = $this->getProductAttributeCombinations($search['result']);      //CMD

 

 

by the way does your shop shows attributes when moving through pages with "previous" - "next" or "show all" or when you select an attribute with the layered navigation box?

Bye

Matteo

      

Now work for me( Prestashop 1.6.1.4

Link to comment
Share on other sites

  • 1 month later...

Hi, Divyesh Prajapati! I did all your recommendations, but i don't understand how to change some code in ajax-cart  and not delete all upgrades in my theme. May be you can show only different for working assing to cart with combinations, please

Link to comment
Share on other sites

Hello guys,

 

Look at here. I have already told you in point#2 that you also have to assign combination of each controllers I have mentioned. If you are using Layered Navigation Module than also you need to assign combination there in layered navigation module file also.

 

hi, can you tell me what controllers file i need to change to show the combinations in home page? Only need for popular products because its the only ones i show there.

 

 

we have solved SearchController.php doing this modification:

 

at the end of the public function init() (at raw 50) we added:

 

    public function getProductAttributeCombinations($products) {

    $combinations = array();

 

    foreach ($products as $product)

    {

        // load product object

        $product = new Product ($product['id_product'], $this->context->language->id);

 

        // get the product combinations data

        // create array combinations with key = id_product

        $combinations[$product->id] = $product->getAttributeCombinations($this->context->language->id);

    }

 

    return $combinations;

}

 

Further to this we added few raws of code inside the public function initContent() - I'm copying all the code; the raws with comment //CMD has been added:

 

    public function initContent()

    {

        $original_query = Tools::getValue('q');

        $query = Tools::replaceAccentedChars(urldecode($original_query));

        if ($this->ajax_search) {

            $searchResults = Search::find((int)(Tools::getValue('id_lang')), $query, 1, 10, 'position', 'desc', true);

            if (is_array($searchResults)) {

                foreach ($searchResults as &$product) {

                    $product['product_link'] = $this->context->link->getProductLink($product['id_product'], $product['prewrite'], $product['crewrite']);

                }

                Hook::exec('actionSearch', array('expr' => $query, 'total' => count($searchResults)));

            }

            $this->ajaxDie(Tools::jsonEncode($searchResults));

        }

 

        //Only controller content initialization when the user use the normal search

        parent::initContent();

        

        $product_per_page = isset($this->context->cookie->nb_item_per_page) ? (int)$this->context->cookie->nb_item_per_page : Configuration::get('PS_PRODUCTS_PER_PAGE');

 

        if ($this->instant_search && !is_array($query)) {

            $this->productSort();

            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));

            $this->p = abs((int)(Tools::getValue('p', 1)));

            $search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc');

            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));

            $nbProducts = $search['total'];

            $this->pagination($nbProducts);

 

            $combinations = $this->getProductAttributeCombinations($search['result']);        // CMD

               

            $this->addColorsToProductList($search['result']);

 

            $this->context->smarty->assign(array(

                'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module

                'combinations' => $combinations, // CMD

                'search_products' => $search['result'],

                'nbProducts' => $search['total'],

                'search_query' => $original_query,

                'instant_search' => $this->instant_search,

                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));

        } elseif (($query = Tools::getValue('search_query', Tools::getValue('ref'))) && !is_array($query)) {

            $this->productSort();

            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));

            $this->p = abs((int)(Tools::getValue('p', 1)));

            $original_query = $query;

            $query = Tools::replaceAccentedChars(urldecode($query));

            $search = Search::find($this->context->language->id, $query, $this->p, $this->n, $this->orderBy, $this->orderWay);

            if (is_array($search['result'])) {

                foreach ($search['result'] as &$product) {

                    $product['link'] .= (strpos($product['link'], '?') === false ? '?' : '&').'search_query='.urlencode($query).'&results='.(int)$search['total'];

                }

            }

 

            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));

            $nbProducts = $search['total'];

            $this->pagination($nbProducts);

      

            $combinations = $this->getProductAttributeCombinations($search['result']);      //CMD

 

 

by the way does your shop shows attributes when moving through pages with "previous" - "next" or "show all" or when you select an attribute with the layered navigation box?

Bye

Matteo

      

 

 

I've tried that in PS 1.6.1.5 and doesn't work

Edited by luiskar (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...

we have solved SearchController.php doing this modification:

 

at the end of the public function init() (at raw 50) we added:

 

    public function getProductAttributeCombinations($products) {

    $combinations = array();

 

    foreach ($products as $product)

    {

        // load product object

        $product = new Product ($product['id_product'], $this->context->language->id);

 

        // get the product combinations data

        // create array combinations with key = id_product

        $combinations[$product->id] = $product->getAttributeCombinations($this->context->language->id);

    }

 

    return $combinations;

}

 

Further to this we added few raws of code inside the public function initContent() - I'm copying all the code; the raws with comment //CMD has been added:

 

    public function initContent()

    {

        $original_query = Tools::getValue('q');

        $query = Tools::replaceAccentedChars(urldecode($original_query));

        if ($this->ajax_search) {

            $searchResults = Search::find((int)(Tools::getValue('id_lang')), $query, 1, 10, 'position', 'desc', true);

            if (is_array($searchResults)) {

                foreach ($searchResults as &$product) {

                    $product['product_link'] = $this->context->link->getProductLink($product['id_product'], $product['prewrite'], $product['crewrite']);

                }

                Hook::exec('actionSearch', array('expr' => $query, 'total' => count($searchResults)));

            }

            $this->ajaxDie(Tools::jsonEncode($searchResults));

        }

 

        //Only controller content initialization when the user use the normal search

        parent::initContent();

        

        $product_per_page = isset($this->context->cookie->nb_item_per_page) ? (int)$this->context->cookie->nb_item_per_page : Configuration::get('PS_PRODUCTS_PER_PAGE');

 

        if ($this->instant_search && !is_array($query)) {

            $this->productSort();

            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));

            $this->p = abs((int)(Tools::getValue('p', 1)));

            $search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc');

            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));

            $nbProducts = $search['total'];

            $this->pagination($nbProducts);

 

            $combinations = $this->getProductAttributeCombinations($search['result']);        // CMD

               

            $this->addColorsToProductList($search['result']);

 

            $this->context->smarty->assign(array(

                'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module

                'combinations' => $combinations, // CMD

                'search_products' => $search['result'],

                'nbProducts' => $search['total'],

                'search_query' => $original_query,

                'instant_search' => $this->instant_search,

                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));

        } elseif (($query = Tools::getValue('search_query', Tools::getValue('ref'))) && !is_array($query)) {

            $this->productSort();

            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));

            $this->p = abs((int)(Tools::getValue('p', 1)));

            $original_query = $query;

            $query = Tools::replaceAccentedChars(urldecode($query));

            $search = Search::find($this->context->language->id, $query, $this->p, $this->n, $this->orderBy, $this->orderWay);

            if (is_array($search['result'])) {

                foreach ($search['result'] as &$product) {

                    $product['link'] .= (strpos($product['link'], '?') === false ? '?' : '&').'search_query='.urlencode($query).'&results='.(int)$search['total'];

                }

            }

 

            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));

            $nbProducts = $search['total'];

            $this->pagination($nbProducts);

      

            $combinations = $this->getProductAttributeCombinations($search['result']);      //CMD

 

 

by the way does your shop shows attributes when moving through pages with "previous" - "next" or "show all" or when you select an attribute with the layered navigation box?

Bye

Matteo

      

Hi, I tried to do what you suggested but nothing happened. My code is here:

	public function getProductAttributeCombinations($products) {
    $combinations = array();

    foreach ($products as $product)
    {
        // load product object
        $product = new Product ($product['id_product'], $this->context->language->id);

        // get the product combinations data
        // create array combinations with key = id_product
        $combinations[$product->id] = $product->getAttributeCombinations($this->context->language->id);
    }

    return $combinations;
}
    /**
     * Assign template vars related to page content
     * @see FrontController::initContent()
     */
    public function initContent()
    {
        $original_query = Tools::getValue('q');
        $query = Tools::replaceAccentedChars(urldecode($original_query));
        if ($this->ajax_search) {
            $searchResults = Search::find((int)(Tools::getValue('id_lang')), $query, 1, 10, 'position', 'desc', true);
            if (is_array($searchResults)) {
                foreach ($searchResults as &$product) {
                    $product['product_link'] = $this->context->link->getProductLink($product['id_product'], $product['prewrite'], $product['crewrite']);
                }
                Hook::exec('actionSearch', array('expr' => $query, 'total' => count($searchResults)));
            }
            $this->ajaxDie(Tools::jsonEncode($searchResults));
        }

        //Only controller content initialization when the user use the normal search
        parent::initContent();
        
        $product_per_page = isset($this->context->cookie->nb_item_per_page) ? (int)$this->context->cookie->nb_item_per_page : Configuration::get('PS_PRODUCTS_PER_PAGE');

        if ($this->instant_search && !is_array($query)) {
            $this->productSort();
            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));
            $this->p = abs((int)(Tools::getValue('p', 1)));
            $search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc');
            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));
            $nbProducts = $search['total'];
            $this->pagination($nbProducts);
            
            $combinations = $this->getProductAttributeCombinations($search['result']);      

            $this->addColorsToProductList($search['result']);

            $this->context->smarty->assign(array(
              
                'nbProducts' => $search['total'],
                'search_query' => $original_query,
                'instant_search' => $this->instant_search,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
        } elseif (($query = Tools::getValue('search_query', Tools::getValue('ref'))) && !is_array($query)) {
            $this->productSort();
            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));
            $this->p = abs((int)(Tools::getValue('p', 1)));
            $original_query = $query;
            $query = Tools::replaceAccentedChars(urldecode($query));
            $search = Search::find($this->context->language->id, $query, $this->p, $this->n, $this->orderBy, $this->orderWay);
            if (is_array($search['result'])) {
                foreach ($search['result'] as &$product) {
                    $product['link'] .= (strpos($product['link'], '?') === false ? '?' : '&').'search_query='.urlencode($query).'&results='.(int)$search['total'];
                }
            }

            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));
            $nbProducts = $search['total'];
            $this->pagination($nbProducts);
            
            $combinations = $this->getProductAttributeCombinations($search['result']);  

            $this->addColorsToProductList($search['result']);

            $this->context->smarty->assign(array(
                'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
                'combinations' => $combinations,
                'search_products' => $search['result'],
                'nbProducts' => $search['total'],
                'search_query' => $original_query,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
        } elseif (($tag = urldecode(Tools::getValue('tag'))) && !is_array($tag)) {
            $nbProducts = (int)(Search::searchTag($this->context->language->id, $tag, true));
            $this->pagination($nbProducts);
            $result = Search::searchTag($this->context->language->id, $tag, false, $this->p, $this->n, $this->orderBy, $this->orderWay);
            Hook::exec('actionSearch', array('expr' => $tag, 'total' => count($result)));
            
           $combinations = $this->getProductAttributeCombinations($search['result']);
           
            $this->addColorsToProductList($result);

            $this->context->smarty->assign(array(
                'search_tag' => $tag,
                'products' => $result, // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
                'combinations' => $combinations,
                'search_products' => $result,
                'nbProducts' => $nbProducts,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
        } else {
            $this->context->smarty->assign(array(
                'products' => array(),
                'combinations' => $combinations,
                'search_products' => array(),
                'pages_nb' => 1,
                'nbProducts' => 0));
        }
        $this->context->smarty->assign(array('add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'comparator_max_item' => Configuration::get('PS_COMPARATOR_MAX_ITEM')));

        $this->setTemplate(_PS_THEME_DIR_.'search.tpl');
    }

I cannot see any change in my e-shop. Can you please tell me, where is the mistake?

Link to comment
Share on other sites

  • 3 weeks later...

Hello,

I would like your help about something! How can I addapt these code to show the combinations like products? I want to get all product combinations in product-list with the images of each color for examle like a product! Check this example in this site . As you can see the product 1616100220 and 1616100230 are the same but different colors! If click on it you will  see that inside has the two combinations! Is a way to do that with your code??? 

 

Thanks

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...

Glad :) to know it worked for you guys ;)

 

Hello, i need help, i want to show just the colors ( combinations ) of my products on homepage, some of my clients doesn't know that the product has more colors, and they don't enter to product page.

 
So i just want to show the colors, like that i'll be sure that the client will enter to product page. 
 
Thank you so much !
Link to comment
Share on other sites

  • 1 month later...
Hello, is this solution works correctly with Prestashop 1.6.1 (1.6.1.1), in my case, when I added products to the shopping cart price is unchanged, the same is when changing attribute - price stays default.

 

In my case I only can choose the attribute from list and nothing else is changed :(

 

Any sugestions with Prestashop 1.6.1 (1.6.1.1) and changing price when adding to cart?
Link to comment
Share on other sites

  • 3 weeks later...

Hi guys, 
 

Hi, thanks for your solutions with listing attributes on product list page.
 
Can you help me with just displaying default attribute combination on product list for each product. I don't need to add to cart or anything else. Just to show what is default attribute.
 
You know what i mean? 
 
I just want to show visitor when he browsing on product list page, what price is for what combination.

Please look at attacment ! 

Thanks a lot !
 
 
 
 

post-764993-0-64715500-1473274180_thumb.png

post-764993-0-95970900-1473274217_thumb.png

Link to comment
Share on other sites

  • 4 weeks later...
  • 3 weeks later...

Any ideas how to make it work in HomeTabs?

 

Im using this modification to show data if product have desired attribute value id

{foreach from=$product.combinations.values key=id_product_attribute item=combination}
 {if $combination.attributes_names == 50}Xtra Large SIZE!{/if}
{/foreach}

This works in category controller but how to make it work in home page and HomeFeatured modules?

Thanks for informations ASAP! :)

Link to comment
Share on other sites

  • 2 weeks later...

Hi All,

 

I have tried to show attribute combinations on product listing page in prestashop 1.6.0.14 ( for defalut theme ) and adding products to cart from the same page it self and it worked for me ( See Attachment ). I am going to give brief steps here; Just follow the instruction given below.

 

1. First of all it is required to have list of combinations for all product and for that add following function in classes/Product.php file.

public static function getProductAttributeCombinations($id_product) {
	$combinations = array();
	$context = Context::getContext();
	$product = new Product ($id_product, $context->language->id);
	$attributes_groups = $product->getAttributesGroups($context->language->id);
	$att_grps = '';
	foreach ($attributes_groups as $k => $row)
	{
		$combinations[$row['id_product_attribute']]['attributes_values'][$row['id_attribute_group']] = $row['attribute_name'];
		$combinations[$row['id_product_attribute']]['attributes_group'][$row['id_attribute_group']] = $row['group_name'];

		$combinations[$row['id_product_attribute']]['attributes_groups'] = @implode(', ', $combinations[$row['id_product_attribute']]['attributes_group']);
		$att_grps = $combinations[$row['id_product_attribute']]['attributes_groups'];
		$combinations[$row['id_product_attribute']]['attributes_names'] = @implode(', ', $combinations[$row['id_product_attribute']]['attributes_values']);
		$combinations[$row['id_product_attribute']]['attributes'][] = (int)$row['id_attribute'];
		$combinations[$row['id_product_attribute']]['price'] = (float)$row['price'];

		// Call getPriceStatic in order to set $combination_specific_price
		if (!isset($combination_prices_set[(int)$row['id_product_attribute']]))
		{
			Product::getPriceStatic((int)$product->id, false, $row['id_product_attribute'], 6, null, false, true, 1, false, null, null, null, $combination_specific_price);
			$combination_prices_set[(int)$row['id_product_attribute']] = true;
			$combinations[$row['id_product_attribute']]['specific_price'] = $combination_specific_price;
		}
		$combinations[$row['id_product_attribute']]['ecotax'] = (float)$row['ecotax'];
		$combinations[$row['id_product_attribute']]['weight'] = (float)$row['weight'];
		$combinations[$row['id_product_attribute']]['quantity'] = (int)$row['quantity'];
		$combinations[$row['id_product_attribute']]['reference'] = $row['reference'];
		$combinations[$row['id_product_attribute']]['unit_impact'] = $row['unit_price_impact'];
		$combinations[$row['id_product_attribute']]['minimal_quantity'] = $row['minimal_quantity'];
		if ($row['available_date'] != '0000-00-00')
		{
			$combinations[$row['id_product_attribute']]['available_date'] = $row['available_date'];
			$combinations[$row['id_product_attribute']]['date_formatted'] = Tools::displayDate($row['available_date']);
		}
		else
			$combinations[$row['id_product_attribute']]['available_date'] = '';
		foreach ($combinations as $id_product_attribute => $comb)
		{
			$attribute_list = '';
			foreach ($comb['attributes'] as $id_attribute)
				$attribute_list .= '\''.(int)$id_attribute.'\',';
			$attribute_list = rtrim($attribute_list, ',');
			$combinations[$id_product_attribute]['list'] = $attribute_list;
		}
	}
	$comb = array(
		'attribute_groups' => $att_grps,
		'values' => $combinations
	);

	return $comb;
} 

 2. Now need to assign this combinations in controllers file like CategoryController.php, ManufacturerController.php, NewProductsController.php, PriceDropController.php, BestSalesController.php that is in controllers folder.

 

I am showing you here only for category page and for that need to call function that we have made in step#1 in CategoryController.php

 

Change function initContent() with following code

public function initContent()
{
	parent::initContent();

	$this->setTemplate(_PS_THEME_DIR_.'category.tpl');

	if (!$this->customer_access)
		return;

	if (isset($this->context->cookie->id_compare))
		$this->context->smarty->assign('compareProducts', CompareProduct::getCompareProducts((int)$this->context->cookie->id_compare));

	$this->productSort(); // Product sort must be called before assignProductList()

	$this->assignScenes();
	$this->assignSubcategories();
	$this->assignProductList();
	
	$products = (isset($this->cat_products) && $this->cat_products) ? $this->cat_products : null;
	foreach($products as &$pro)
	{	
		$pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
	}
	
	$this->context->smarty->assign(array(
		'category' => $this->category,
		'description_short' => Tools::truncateString($this->category->description, 350),
		'products' => $products,
		'combinations' => $combinations,
		'id_category' => (int)$this->category->id,
		'id_category_parent' => (int)$this->category->id_parent,
		'return_category_name' => Tools::safeOutput($this->category->name),
		'path' => Tools::getPath($this->category->id),
		'add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'),
		'categorySize' => Image::getSize(ImageType::getFormatedName('category')),
		'mediumSize' => Image::getSize(ImageType::getFormatedName('medium')),
		'thumbSceneSize' => Image::getSize(ImageType::getFormatedName('m_scene')),
		'homeSize' => Image::getSize(ImageType::getFormatedName('home')),
		'allow_oosp' => (int)Configuration::get('PS_ORDER_OUT_OF_STOCK'),
		'comparator_max_item' => (int)Configuration::get('PS_COMPARATOR_MAX_ITEM'),
		'suppliers' => Supplier::getSuppliers(),
		'body_classes' => array($this->php_self.'-'.$this->category->id, $this->php_self.'-'.$this->category->link_rewrite)
	));
}

3. Replace code in your product-list.tpl ( in folder /themes/default-bootstrap/ ) file of default theme with code in product-list.tpl.txt file ( File attached )

 

4. Replace code in your global.js ( in folder /themes/default-bootstrap/js ) file of default theme with code in global.js.txt file ( File attached )

 

5. Add this in global.css ( in folder /themes/default-bootstrap/css )

.att_list{padding:10px;}
.att_list .selector{margin: 0 auto;}

6. Replace code in your ajax-cart.js ( in folder /themes/default-bootstrap/modules/blockcart ) file of default theme with code in ajax-cart.js.txt file ( File attached )

 

That's It! :)

 

Please note that this is only working in default theme for now if you want to use in custom theme you need to make changes in tpl files accordingly.

 

Dear Divyesh,

 

Thank you for the code it's very helpful and it seems a solution to my problem, but I have couple of questions to ask before I apply your code as I am not technically advanced (please excuse my shallow knowledge in this area).

 - if I upgraded prestashop after I applied code above, will I have to change the code again or it will remain there? 

 - in case I upgraded the current theme I am using or changed the theme I have to change the code again, right?

 - Does this code above conflict / impact other modules?

-  Is code above applied to all categories with their relevant attributes I have or I have to apply for each category? example I have kids clothes and the attribute (AGE), Men Cloths attribute (SIZE)

SaveSave

Edited by aymanbakir (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...

 

1. First of all it is required to have list of combinations for all product and for that add following function in classes/Product.php file.

public static function getProductAttributeCombinations($id_product) {
    $combinations = array();
    $context = Context::getContext();
    $product = new Product ($id_product, $context->language->id);
    $attributes_groups = $product->getAttributesGroups($context->language->id);
    $att_grps = '';
    foreach ($attributes_groups as $k => $row)
    {
        $combinations[$row['id_product_attribute']]['attributes_values'][$row['id_attribute_group']] = $row['attribute_name'];
        $combinations[$row['id_product_attribute']]['attributes_group'][$row['id_attribute_group']] = $row['group_name'];

        $combinations[$row['id_product_attribute']]['attributes_groups'] = @implode(', ', $combinations[$row['id_product_attribute']]['attributes_group']);
        $att_grps = $combinations[$row['id_product_attribute']]['attributes_groups'];
        $combinations[$row['id_product_attribute']]['attributes_names'] = @implode(', ', $combinations[$row['id_product_attribute']]['attributes_values']);
        $combinations[$row['id_product_attribute']]['attributes'][] = (int)$row['id_attribute'];
        $combinations[$row['id_product_attribute']]['price'] = (float)$row['price'];

        // Call getPriceStatic in order to set $combination_specific_price
        if (!isset($combination_prices_set[(int)$row['id_product_attribute']]))
        {
            Product::getPriceStatic((int)$product->id, false, $row['id_product_attribute'], 6, null, false, true, 1, false, null, null, null, $combination_specific_price);
            $combination_prices_set[(int)$row['id_product_attribute']] = true;
            $combinations[$row['id_product_attribute']]['specific_price'] = $combination_specific_price;
        }
        $combinations[$row['id_product_attribute']]['ecotax'] = (float)$row['ecotax'];
        $combinations[$row['id_product_attribute']]['weight'] = (float)$row['weight'];
        $combinations[$row['id_product_attribute']]['quantity'] = (int)$row['quantity'];
        $combinations[$row['id_product_attribute']]['reference'] = $row['reference'];
        $combinations[$row['id_product_attribute']]['unit_impact'] = $row['unit_price_impact'];
        $combinations[$row['id_product_attribute']]['minimal_quantity'] = $row['minimal_quantity'];
        if ($row['available_date'] != '0000-00-00')
        {
            $combinations[$row['id_product_attribute']]['available_date'] = $row['available_date'];
            $combinations[$row['id_product_attribute']]['date_formatted'] = Tools::displayDate($row['available_date']);
        }
        else
            $combinations[$row['id_product_attribute']]['available_date'] = '';
        foreach ($combinations as $id_product_attribute => $comb)
        {
            $attribute_list = '';
            foreach ($comb['attributes'] as $id_attribute)
                $attribute_list .= '\''.(int)$id_attribute.'\',';
            $attribute_list = rtrim($attribute_list, ',');
            $combinations[$id_product_attribute]['list'] = $attribute_list;
        }
    }
    $comb = array(
        'attribute_groups' => $att_grps,
        'values' => $combinations
    
);

    return $comb;
} 

 2. Now need to assign this combinations in controllers file like CategoryController.php, ManufacturerController.php, NewProductsController.php, PriceDropController.php, BestSalesController.php that is in controllers folder.

 

 
 
Good afternoon, everybody.
I made all the changes, and then gives an error "HTTP ERROR 500".
 
error falls once how I change product.php file.
Tell me please.
 
I just need to on a standard topic in the directory displayed attribute size. just in line.
prestashop version 1.6.1.7.
 
Help!
ps Sorry for my bad english
Link to comment
Share on other sites

  • 3 weeks later...
  • 1 month later...
  • 1 month later...

Cool, bless the founder :)) saved my life man. Little tip for these who were fighting the searchcontroller and didnt solve it after all - should look like this (1.6.1.6):

 

class SearchControllerCore extends FrontController
{
    public $php_self = 'search';
    public $instant_search;
    public $ajax_search;
 
    /**
     * Initialize search controller
     * @see FrontController::init()
     */
    public function init()
    {
        parent::init();
 
        $this->instant_search = Tools::getValue('instantSearch');
 
        $this->ajax_search = Tools::getValue('ajaxSearch');
 
        if ($this->instant_search || $this->ajax_search) {
            $this->display_header = false;
            $this->display_footer = false;
        }
    }
 
    /**
     * Assign template vars related to page content
     * @see FrontController::initContent()
     */
    public function initContent()
    {
        $original_query = Tools::getValue('q');
        $query = Tools::replaceAccentedChars(urldecode($original_query));
        if ($this->ajax_search) {
            $searchResults = Search::find((int)(Tools::getValue('id_lang')), $query, 1, 10, 'position', 'desc', true);
            if (is_array($searchResults)) {
                foreach ($searchResults as &$product) {
                    $product['product_link'] = $this->context->link->getProductLink($product['id_product'], $product['prewrite'], $product['crewrite']);
                }
                Hook::exec('actionSearch', array('expr' => $query, 'total' => count($searchResults)));
            }
            $this->ajaxDie(Tools::jsonEncode($searchResults));
        }
 
        //Only controller content initialization when the user use the normal search
        parent::initContent();
 
        $product_per_page = isset($this->context->cookie->nb_item_per_page) ? (int)$this->context->cookie->nb_item_per_page : Configuration::get('PS_PRODUCTS_PER_PAGE');
 
        if ($this->instant_search && !is_array($query)) {
            $this->productSort();
            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));
            $this->p = abs((int)(Tools::getValue('p', 1)));
            $search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc');
            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));
            $nbProducts = $search['total'];
            $this->pagination($nbProducts);
 
            $this->addColorsToProductList($search['result']);
 
        $products = $search['result'];
        foreach($products as &$pro)
        {
            $pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
        }
 
            $this->context->smarty->assign(array(
                'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
                'combinations' => $combinations,
                'search_products' => $products,
                'nbProducts' => $search['total'],
                'search_query' => $original_query,
                'instant_search' => $this->instant_search,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
        } elseif (($query = Tools::getValue('search_query', Tools::getValue('ref'))) && !is_array($query)) {
            $this->productSort();
            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));
            $this->p = abs((int)(Tools::getValue('p', 1)));
            $original_query = $query;
            $query = Tools::replaceAccentedChars(urldecode($query));
            $search = Search::find($this->context->language->id, $query, $this->p, $this->n, $this->orderBy, $this->orderWay);
            if (is_array($search['result'])) {
                foreach ($search['result'] as &$product) {
                    $product['link'] .= (strpos($product['link'], '?') === false ? '?' : '&').'search_query='.urlencode($query).'&results='.(int)$search['total'];
                }
            }
 
            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));
            $nbProducts = $search['total'];
            $this->pagination($nbProducts);
 
            $this->addColorsToProductList($search['result']);
 
        $products = $search['result'];
        foreach($products as &$pro)
        {
            $pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
        }
 
            $this->context->smarty->assign(array(
                'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
                'search_products' => $products,
                //'combinations' => $combinations,
                'nbProducts' => $search['total'],
                'search_query' => $original_query,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
        } elseif (($tag = urldecode(Tools::getValue('tag'))) && !is_array($tag)) {
            $nbProducts = (int)(Search::searchTag($this->context->language->id, $tag, true));
            $this->pagination($nbProducts);
            $result = Search::searchTag($this->context->language->id, $tag, false, $this->p, $this->n, $this->orderBy, $this->orderWay);
            Hook::exec('actionSearch', array('expr' => $tag, 'total' => count($result)));
 
            $this->addColorsToProductList($result);
 
        $products = $result;
        foreach($products as &$pro)
        {
            $pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
        }
 
            $this->context->smarty->assign(array(
                'search_tag' => $tag,
                'products' => $result, // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
                'combinations' => $combinations,
                'search_products' => $products,
                'nbProducts' => $nbProducts,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
        } else {
 
 
        $products = array();
        foreach($products as &$pro)
        {
            $pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
        }
 
 
 
            $this->context->smarty->assign(array(
                'products' => array(),
                'combinations' => $combinations,
                'search_products' => $products,
                'pages_nb' => 1,
                'nbProducts' => 0));
        }
        $this->context->smarty->assign(array('add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'comparator_max_item' => Configuration::get('PS_COMPARATOR_MAX_ITEM')));
 
        $this->setTemplate(_PS_THEME_DIR_.'search.tpl');
    }
 
    public function displayHeader($display = true)
    {
        if (!$this->instant_search && !$this->ajax_search) {
            parent::displayHeader();
        } else {
            $this->context->smarty->assign('static_token', Tools::getToken(false));
        }
    }
 
    public function displayFooter($display = true)
    {
        if (!$this->instant_search && !$this->ajax_search) {
            parent::displayFooter();
        }
    }
 
    public function setMedia()
    {
        parent::setMedia();
 
        if (!$this->instant_search && !$this->ajax_search) {
            $this->addCSS(_THEME_CSS_DIR_.'product_list.css');
        }
    }
}
  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...
  • 3 months later...
  • 2 months later...

Hi !

 

I'm trying to implement this code in my site, but I have two problems:

 

1. The view of products is in grid. How can I change to list view?

2.- The attributes change but not the final price of product, how can do it?

 

thanks so muchs for your time !

 

http://www.milcolchones.com/56-colchones-muelles-viscoelasticos

 

Hi there, how you solve it?

 

 

Thanks for this great contribution!

Link to comment
Share on other sites

  • 1 year later...
  • 6 months later...
On 3/7/2017 at 9:13 PM, Idiotic said:

Cool, bless the founder :)) saved my life man. Little tip for these who were fighting the searchcontroller and didnt solve it after all - should look like this (1.6.1.6):

 

class SearchControllerCore extends FrontController
{
    public $php_self = 'search';
    public $instant_search;
    public $ajax_search;
 
    /**
     * Initialize search controller
     * @see FrontController::init()
     */
    public function init()
    {
        parent::init();
 
        $this->instant_search = Tools::getValue('instantSearch');
 
        $this->ajax_search = Tools::getValue('ajaxSearch');
 
        if ($this->instant_search || $this->ajax_search) {
            $this->display_header = false;
            $this->display_footer = false;
        }
    }
 
    /**
     * Assign template vars related to page content
     * @see FrontController::initContent()
     */
    public function initContent()
    {
        $original_query = Tools::getValue('q');
        $query = Tools::replaceAccentedChars(urldecode($original_query));
        if ($this->ajax_search) {
            $searchResults = Search::find((int)(Tools::getValue('id_lang')), $query, 1, 10, 'position', 'desc', true);
            if (is_array($searchResults)) {
                foreach ($searchResults as &$product) {
                    $product['product_link'] = $this->context->link->getProductLink($product['id_product'], $product['prewrite'], $product['crewrite']);
                }
                Hook::exec('actionSearch', array('expr' => $query, 'total' => count($searchResults)));
            }
            $this->ajaxDie(Tools::jsonEncode($searchResults));
        }
 
        //Only controller content initialization when the user use the normal search
        parent::initContent();
 
        $product_per_page = isset($this->context->cookie->nb_item_per_page) ? (int)$this->context->cookie->nb_item_per_page : Configuration::get('PS_PRODUCTS_PER_PAGE');
 
        if ($this->instant_search && !is_array($query)) {
            $this->productSort();
            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));
            $this->p = abs((int)(Tools::getValue('p', 1)));
            $search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc');
            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));
            $nbProducts = $search['total'];
            $this->pagination($nbProducts);
 
            $this->addColorsToProductList($search['result']);
 
        $products = $search['result'];
        foreach($products as &$pro)
        {
            $pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
        }
 
            $this->context->smarty->assign(array(
                'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
                'combinations' => $combinations,
                'search_products' => $products,
                'nbProducts' => $search['total'],
                'search_query' => $original_query,
                'instant_search' => $this->instant_search,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
        } elseif (($query = Tools::getValue('search_query', Tools::getValue('ref'))) && !is_array($query)) {
            $this->productSort();
            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));
            $this->p = abs((int)(Tools::getValue('p', 1)));
            $original_query = $query;
            $query = Tools::replaceAccentedChars(urldecode($query));
            $search = Search::find($this->context->language->id, $query, $this->p, $this->n, $this->orderBy, $this->orderWay);
            if (is_array($search['result'])) {
                foreach ($search['result'] as &$product) {
                    $product['link'] .= (strpos($product['link'], '?') === false ? '?' : '&').'search_query='.urlencode($query).'&results='.(int)$search['total'];
                }
            }
 
            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));
            $nbProducts = $search['total'];
            $this->pagination($nbProducts);
 
            $this->addColorsToProductList($search['result']);
 
        $products = $search['result'];
        foreach($products as &$pro)
        {
            $pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
        }
 
            $this->context->smarty->assign(array(
                'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
                'search_products' => $products,
                //'combinations' => $combinations,
                'nbProducts' => $search['total'],
                'search_query' => $original_query,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
        } elseif (($tag = urldecode(Tools::getValue('tag'))) && !is_array($tag)) {
            $nbProducts = (int)(Search::searchTag($this->context->language->id, $tag, true));
            $this->pagination($nbProducts);
            $result = Search::searchTag($this->context->language->id, $tag, false, $this->p, $this->n, $this->orderBy, $this->orderWay);
            Hook::exec('actionSearch', array('expr' => $tag, 'total' => count($result)));
 
            $this->addColorsToProductList($result);
 
        $products = $result;
        foreach($products as &$pro)
        {
            $pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
        }
 
            $this->context->smarty->assign(array(
                'search_tag' => $tag,
                'products' => $result, // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
                'combinations' => $combinations,
                'search_products' => $products,
                'nbProducts' => $nbProducts,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
        } else {
 
 
        $products = array();
        foreach($products as &$pro)
        {
            $pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
        }
 
 
 
            $this->context->smarty->assign(array(
                'products' => array(),
                'combinations' => $combinations,
                'search_products' => $products,
                'pages_nb' => 1,
                'nbProducts' => 0));
        }
        $this->context->smarty->assign(array('add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'comparator_max_item' => Configuration::get('PS_COMPARATOR_MAX_ITEM')));
 
        $this->setTemplate(_PS_THEME_DIR_.'search.tpl');
    }
 
    public function displayHeader($display = true)
    {
        if (!$this->instant_search && !$this->ajax_search) {
            parent::displayHeader();
        } else {
            $this->context->smarty->assign('static_token', Tools::getToken(false));
        }
    }
 
    public function displayFooter($display = true)
    {
        if (!$this->instant_search && !$this->ajax_search) {
            parent::displayFooter();
        }
    }
 
    public function setMedia()
    {
        parent::setMedia();
 
        if (!$this->instant_search && !$this->ajax_search) {
            $this->addCSS(_THEME_CSS_DIR_.'product_list.css');
        }
    }
}

At last, this has worked for me, thanks!

Edited by jordibabot
grammar (see edit history)
Link to comment
Share on other sites

  • 1 month later...
On 4/22/2015 at 11:48 AM, Divyesh Prajapati said:

May be you are using block layered navigation module. If yes than you need to add some short of code in blocklayered.php file.

 

In blocklayered.php file there is a function ajaxCall() and in that just before $smarty->assign add below code.


foreach($products as &$pro)
{	
	$pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
}

Hi Divyesh, Thanks to your post I discovered that the problem I have is due the blocklayered module OMG! Thanks god .

 

I have the productcomments module modified to show the product rating in the product-list.tpl but when I am using the blocklayered module the ratings only appears on the first page... I have been reading you reply and I understand that I need to add a code at the blocklayered.php inside the ajaxCall() to load the rating on every ajax action but I have tried  some  code based in your reply without luck. Maybe you can help me:

 

My working function in Productscomment.php is: 

Quote

 

 public function hookDisplayProductListReviews($params)
    {
        $id_product = (int) $params['product']['id_product'];
        if (!$this->isCached('productcomments_reviews.tpl', $this->getCacheId($id_product))) {
            require_once dirname(__FILE__).'/ProductComment.php';
            $average = ProductComment::getAverageGrade($id_product);
            $this->smarty->assign(array(
                'product' => $params['product'],
                'averageTotal' => round($average['grade']),
                'ratings' => ProductComment::getRatings($id_product),
                'nbComments' => (int) ProductComment::getCommentNumber($id_product),
            ));
        }

        return $this->display(__FILE__, 'productcomments_reviews.tpl', $this->getCacheId($id_product));
    }

 

Could you say me what I need in the blocklayered.php to load the rating?

 

Thanks so much!

Link to comment
Share on other sites

  • 3 months later...
On 3/8/2017 at 1:43 AM, Idiotic said:

Cool, bless the founder :)) saved my life man. Little tip for these who were fighting the searchcontroller and didnt solve it after all - should look like this (1.6.1.6):

 

class SearchControllerCore extends FrontController
{
    public $php_self = 'search';
    public $instant_search;
    public $ajax_search;
 
    /**
     * Initialize search controller
     * @see FrontController::init()
     */
    public function init()
    {
        parent::init();
 
        $this->instant_search = Tools::getValue('instantSearch');
 
        $this->ajax_search = Tools::getValue('ajaxSearch');
 
        if ($this->instant_search || $this->ajax_search) {
            $this->display_header = false;
            $this->display_footer = false;
        }
    }
 
    /**
     * Assign template vars related to page content
     * @see FrontController::initContent()
     */
    public function initContent()
    {
        $original_query = Tools::getValue('q');
        $query = Tools::replaceAccentedChars(urldecode($original_query));
        if ($this->ajax_search) {
            $searchResults = Search::find((int)(Tools::getValue('id_lang')), $query, 1, 10, 'position', 'desc', true);
            if (is_array($searchResults)) {
                foreach ($searchResults as &$product) {
                    $product['product_link'] = $this->context->link->getProductLink($product['id_product'], $product['prewrite'], $product['crewrite']);
                }
                Hook::exec('actionSearch', array('expr' => $query, 'total' => count($searchResults)));
            }
            $this->ajaxDie(Tools::jsonEncode($searchResults));
        }
 
        //Only controller content initialization when the user use the normal search
        parent::initContent();
 
        $product_per_page = isset($this->context->cookie->nb_item_per_page) ? (int)$this->context->cookie->nb_item_per_page : Configuration::get('PS_PRODUCTS_PER_PAGE');
 
        if ($this->instant_search && !is_array($query)) {
            $this->productSort();
            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));
            $this->p = abs((int)(Tools::getValue('p', 1)));
            $search = Search::find($this->context->language->id, $query, 1, 10, 'position', 'desc');
            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));
            $nbProducts = $search['total'];
            $this->pagination($nbProducts);
 
            $this->addColorsToProductList($search['result']);
 
        $products = $search['result'];
        foreach($products as &$pro)
        {
            $pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
        }
 
            $this->context->smarty->assign(array(
                'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
                'combinations' => $combinations,
                'search_products' => $products,
                'nbProducts' => $search['total'],
                'search_query' => $original_query,
                'instant_search' => $this->instant_search,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
        } elseif (($query = Tools::getValue('search_query', Tools::getValue('ref'))) && !is_array($query)) {
            $this->productSort();
            $this->n = abs((int)(Tools::getValue('n', $product_per_page)));
            $this->p = abs((int)(Tools::getValue('p', 1)));
            $original_query = $query;
            $query = Tools::replaceAccentedChars(urldecode($query));
            $search = Search::find($this->context->language->id, $query, $this->p, $this->n, $this->orderBy, $this->orderWay);
            if (is_array($search['result'])) {
                foreach ($search['result'] as &$product) {
                    $product['link'] .= (strpos($product['link'], '?') === false ? '?' : '&').'search_query='.urlencode($query).'&results='.(int)$search['total'];
                }
            }
 
            Hook::exec('actionSearch', array('expr' => $query, 'total' => $search['total']));
            $nbProducts = $search['total'];
            $this->pagination($nbProducts);
 
            $this->addColorsToProductList($search['result']);
 
        $products = $search['result'];
        foreach($products as &$pro)
        {
            $pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
        }
 
            $this->context->smarty->assign(array(
                'products' => $search['result'], // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
                'search_products' => $products,
                //'combinations' => $combinations,
                'nbProducts' => $search['total'],
                'search_query' => $original_query,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
        } elseif (($tag = urldecode(Tools::getValue('tag'))) && !is_array($tag)) {
            $nbProducts = (int)(Search::searchTag($this->context->language->id, $tag, true));
            $this->pagination($nbProducts);
            $result = Search::searchTag($this->context->language->id, $tag, false, $this->p, $this->n, $this->orderBy, $this->orderWay);
            Hook::exec('actionSearch', array('expr' => $tag, 'total' => count($result)));
 
            $this->addColorsToProductList($result);
 
        $products = $result;
        foreach($products as &$pro)
        {
            $pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
        }
 
            $this->context->smarty->assign(array(
                'search_tag' => $tag,
                'products' => $result, // DEPRECATED (since to 1.4), not use this: conflict with block_cart module
                'combinations' => $combinations,
                'search_products' => $products,
                'nbProducts' => $nbProducts,
                'homeSize' => Image::getSize(ImageType::getFormatedName('home'))));
        } else {
 
 
        $products = array();
        foreach($products as &$pro)
        {
            $pro['combinations'] = Product::getProductAttributeCombinations($pro['id_product']);
        }
 
 
 
            $this->context->smarty->assign(array(
                'products' => array(),
                'combinations' => $combinations,
                'search_products' => $products,
                'pages_nb' => 1,
                'nbProducts' => 0));
        }
        $this->context->smarty->assign(array('add_prod_display' => Configuration::get('PS_ATTRIBUTE_CATEGORY_DISPLAY'), 'comparator_max_item' => Configuration::get('PS_COMPARATOR_MAX_ITEM')));
 
        $this->setTemplate(_PS_THEME_DIR_.'search.tpl');
    }
 
    public function displayHeader($display = true)
    {
        if (!$this->instant_search && !$this->ajax_search) {
            parent::displayHeader();
        } else {
            $this->context->smarty->assign('static_token', Tools::getToken(false));
        }
    }
 
    public function displayFooter($display = true)
    {
        if (!$this->instant_search && !$this->ajax_search) {
            parent::displayFooter();
        }
    }
 
    public function setMedia()
    {
        parent::setMedia();
 
        if (!$this->instant_search && !$this->ajax_search) {
            $this->addCSS(_THEME_CSS_DIR_.'product_list.css');
        }
    }
}

Thanks :)

Link to comment
Share on other sites

  • 1 month later...
  • 3 months later...

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...