How to display product quantity in Prestashop

10,158

Solution 1

See this thread on Prestashop.com forum. It looks like someone else asked a similar question and was able to get it working with this code:

{*Added quantity in stock*}
<!-- availability -->
<p id="availability_statut"{if ($product.quantity <= 0 && !$product.available_later && $allow_oosp) OR ($product.quantity > 0 && !$product.available_now) OR !$product.available_for_order OR $PS_CATALOG_MODE} style="display: none;"{/if}>
    <span id="availability_label">{l s='Availability:'}</span>
    <span id="availability_value"{if $product.quantity <= 0} class="warning_inline"{/if}>
        {if $product.quantity <= 0}{if $allow_oosp}{$product.available_later}{else}{l s='This product is no longer in stock'}{/if}{else}{$product.available_now}{/if}
    </span>
</p>

<!-- number of item in stock -->
{*if ($display_qties == 1 && !$PS_CATALOG_MODE && $product.available_for_order) *}
<p id="pQuantityAvailable"{if $product.quantity <= 0} style="display: none;"{/if}>
    <span id="quantityAvailable">{$product.quantity|intval}</span>
    <span {if $product.quantity > 1} style="display: none;"{/if} id="quantityAvailableTxt">{l s='item in stock'}</span>
    <span {if $product.quantity == 1} style="display: none;"{/if} id="quantityAvailableTxtMultiple">{l s='items in stock'}</span>
</p>

<!-- Out of stock hook -->
<p id="oosHook"{if $product.quantity > 0} style="display: none;"{/if}>
    {$HOOK_PRODUCT_OOS}
</p>

<p class="warning_inline" id="last_quantities"{if ($product.quantity > $last_qties OR $product.quantity <= 0) OR $allow_oosp OR !$product.available_for_order OR $PS_CATALOG_MODE} style="display: none"{/if} >{l s='Warning: Last items in stock!'}</p>
{*End Added quantity in stock*} 

and in this other post also on Prestashop.com, someone reported that they were able to display quantities specific to the product size attribute using this Prestashop module. {/if}

Solution 2

Basically product list already show some attributes - for example, colors. Product colors have a special method to append color selection to each product box in a list.

Therefore, we should follow this method if we do not wish to create a specific module.

Because we have to change some code, we must do and override (because we're not creating a module).

Method addColorsToProductList as a variable $product['color_list'] to each product that will be displayed in a list. We should and can add information about quantities and combinations here (it's probably the easiest way).

To get info about the combinations, I looked at AdminProductsController.php @ Line 4263. You should look at this method and also addColorsToProductList. Below is a pseudo-code (not fully complete, I can't write it all out for yo, but it shows the idea):

override/classes/controller/FrontController.php

FrontController extends FrontControllerCore {

    public function addColorsToProductList(&$products)
    {
        // You may need to modify the parent code if the caching ignores your changes

        // Disable this if you dont want to add colors seperately;
        parent::addColorsToProductList($products);


        // @see AdminProductsController.php @ Line 4263
        foreach($products as &$p)
        {
            $attributes = $p->getAttributesResume($this->context->language->id);

            foreach ($attributes as $attribute)
            {
                // You may check if product_attribute is color + size here

                $id_product_attribute = $attribute['id_product_attribute'];
                $qty = StockAvailable::getQuantityAvailableByProduct((int)$obj->id, id_product_attribute);

                $p['product_attributes'][$id_product_attribute]['qty'] = $qty;
                $p['product_attributes'][$id_product_attribute]['name'] =  $attribute['attribute_designation'];

                // If you need individual color + size and their data, you will probably have to do some SQL,
                // because getAttributesResume doesn SQL to get this data too.
            }
        }
    }

}

Aftert doing this you will be able to use product_attributes inside your templates:

product-list.tpl

{if isset($product.color_list)}
    <div class="color-list-container">{$product.color_list}</div>
{/if}

<p>
{foreach $product.product_attributes as $pa}
    {$pa.qty} - {$pa.name} <br>
{/foreach}
</p>

Cheers

Share:
10,158
Aryan G
Author by

Aryan G

Programmer

Updated on June 29, 2022

Comments

  • Aryan G
    Aryan G almost 2 years

    I'm using one of the default themes included with . Is it possible to display product quantity values for a product based on size and color within the product box on the homepage?