Jump to content

Display attribute quantities of products in product list


Recommended Posts

Hi everyone!

 

So I stumbled upon this great feature: https://www.prestashop.com/forums/topic/279551-how-to-show-product-attribute-option-in-product-list/

 

Now I can display different attributes in the product list. But one thing is missing i'd like to add:

 

Is it possible to display the current quantities of the attribute?

 

More in detail:

 

Lets say i have a drink in two different flavours : Chocolate and Vanilla. In the Backend, i can assign the quantity to each of the two flavors, lets say i have 20 drinks with chocolate flavor, and 10 with vanilla. Now in the productlist, I'd like to display these quanities:

 

MyDrink

Price: XX €

Flavors:

- Chocolate (20 left)

- Vanilla (10 left)

 

Please can someone help me? I uset the code from the forum post mentioned above to get the different combinations:

/* function to get all necessary data of products combinations
 * @param $products An array of products data
 * return array products combinations
 */
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;
}


public function initContent()
{
    parent::initContent();
    $products = $this->category->getProducts($this->context->language->id, (int)$this->p, (int)$this->n, $this->orderBy, $this->orderWay);
    $combinations = $this->getProductAttributeCombinations($products);
    $this->context->smarty->assign('combinations', $combinations);
}

And in the tpl:

{foreach from=$combinations key=k item=comb}
    {* because the array key are id_product, we can separate the product combinations in here 
       with if/else statement compared with the id_product from the foreach loop of products-list *}
    {if $k = $product.id_product}
        {* The attribute Group Name *}
        <p class="comb_title">{$comb.group_name}</p>
        {* List of attribute values inside the attribute Group for current product *}
        <select>
        {foreach from=$comb item=attr}
            <option value="{$attr.id_attribute}">{$attr.attribute_name} {l s=': +'} {convertPrice price=$attr.unit_price_impact}</option>
        {/foreach}
        </select>
    {/if}
{/foreach}

Can someone help me? I would be so glad! :)

 

Best regards,

SunTastic

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

Put {$combinations|print_r} in your template file (tpl) and look in the html source code at what is inside this $combinations structure. If the quantity is there you can easily use it. If it isn't there you need to have a look at that getProductAttributeCombinations() function.

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