Jump to content

Accessories for each product on product-list


pkdsgn
 Share

Recommended Posts

Hi,

I want display in product list (below each product) their accessries.

I'm trying override categroyController.php whith new array but something is wrong.

How get accessories for each product? Maybe, do you have some manuale for this ?

 I looked through offcial documentantion but I dont find this.
Any help ?

Share this post


Link to post
Share on other sites

You could add code like the following to themes/default-bootstrap/product-list.tpl before the {hook h="displayProductPriceBlock" product=$product type="weight"} line:

                    {assign var='accessories' value=Product::getAccessoriesLight($cookie->id_lang, $product.id_product)}
                    {if $accessories|@count > 0}
                    <p>{l s='Available accessories:'}
                    {foreach from=Product::getAccessoriesLight($cookie->id_lang, $product.id_product) item='accessory' name='accessories'}
                        {$accessory.name}{if $smarty.foreach.accessories.iteration < $accessories|@count},{/if}
                    {/foreach}
                    </p>
                    {/if}

This will display "Available accessories:" followed by the names of all available accessories.

  • Like 1

Share this post


Link to post
Share on other sites

In that case, you'll need to use the Product::getAccessories() function, but that is not static, so it can't be used directly in the TPL file. You'll need to create override/classes/Category.php with the following:

<?php

class Category extends CategoryCore
{
    public function getProducts($id_lang, $p, $n, $order_by = null, $order_way = null, $get_total = false, $active = true, $random = false, $random_number_products = 1, $check_access = true, Context $context = null)
    {       
        $products = parent::getProducts($id_lang, $p, $n, $order_by, $order_way, $get_total, $active, $random, $random_number_products, $check_access, $context);

        if (!$context) {
            $context = Context::getContext();   
        }

        if ($get_total) {
            return $products;   
        }

        foreach ($products as &$product) {
            $productObj = new Product((int)$product['id_product']);
            
            if (Validate::isLoadedObject($productObj)) {
                $product['accessories'] = $productObj->getAccessories((int)$context->language->id);
            }
        }

        return $products;
    }    
}

Remember to go to the Advanced Parameters > Performance tab and click the "Clear cache" button (or manually delete cache/class_index.php) so PrestaShop can find the override. Then you can use {$product.accessories} in product-list.tpl to get the accessories of the product.

  • Like 1

Share this post


Link to post
Share on other sites

 

It's look interesting, but is it only extra code .tpl ?

If I added this to my theme, nothing happen... Do you have module for this or something ?

 

I need accesorie for each product on list, It should be that same like a product..

Or maybe do you can make this module for me ? Of course - extra gift for you :)

 

Share this post


Link to post
Share on other sites

1- Upload the .tpl file included in the zip file to your theme folder

2- Add this code in your product-list.tpl, bellow this

{if isset($product.on_sale) && $product.on_sale && isset($product.show_price) && $product.show_price && !$PS_CATALOG_MODE}
<a class="sale-box" href="{$product.link|escape:'html':'UTF-8'}">
<span class="sale-label">{l s='Sale!'}</span></a>{/if}

add this code

{if isset($product.packItems) && $product.packItems}
<div class="packitems">
<span class="tab_packitems">{l s='Pack content'}</span>
 {include file="$tpl_dir./product-list-packs.tpl" products=$product.packItems}
</div>
{/if}

3- Add this css code in your global.css

ul.product_list .product-image-container:hover .packitems {
    display: none;
}
.packitems .tab_packitems {
    font-family: georgia;
    font-style: italic;
    background: #6ad4ff none repeat scroll 0 0;
    padding: 3px 20px;
    color: #ffffff;
    position: relative;
    bottom: 10px;
	text-shadow:1px 1px rgba(0, 0, 0, 0.24)
}
.packitems .product-image-container {
    background: #ffffff;
}
.packitems .pack_quantify {
    font-family: arial;
    font-size: 11px;
    font-weight: bold;
    position: absolute;
    top: 0;
    right: 5%;
    color: #666666;
    padding: 1px;
    text-shadow: 1px 1px rgba(0, 0, 0, 0.24);
}
.packitems {
    bottom: 0;
    left: 0;
    position: absolute;
    right: 0;
    background: rgba(255, 255, 255, 0.75);
}

This css code it´s for default theme, if you use another theme you must adapt it to your theme

Share this post


Link to post
Share on other sites

OO, sorry, my bad, I'm thinking this is for accessorries but it for packs item.
I need accessories (one for each product). I want show Master Product and Accessory becouse both's products are similar. 

Share this post


Link to post
Share on other sites

Ok, sorry. I didn't understand. Then try with Rocky´s override code and keep in mind that

{$product.accessories}

it´s an array. To test his variables use

{$product.accessories|print_r}

and a access them by way foreach

Share this post


Link to post
Share on other sites

Check to make sure you don't have any modules installed in the "actionProductListOverride" hook, since they will prevent the override from working. Adding the following to controllers/front/CategoryController.php at line 220 (before the if (!$hook_executed) line) will enable the override to work even if you have modules installed in that hook:

        $hook_executed = false;
  • Like 2

Share this post


Link to post
Share on other sites

This module does that you need

https://www.prestashop.com/forums/topic/546901-modulo-accesorios-en-product-list/

 

1- Install the module

2- Add the hook in your themes/your_theme_/product-list.tpl file, eg in default theme above this 

<p class="product-desc" itemprop="description">

add the hook

{hook h='displayAccesoriesOnList' product=$product}
  • Like 1

Share this post


Link to post
Share on other sites

I don't know, whats is wrong when I override category, I am trying add $hook_executed = falsebut I always got number "1".

 

Also your module is great ! exactly what I need ! Thank you for helping and great module !

Share this post


Link to post
Share on other sites

  • 1 year later...
  • 3 years 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
 Share

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More