Reading list Switch to dark mode

    Add a custom column to show aggregate function value in render list using PrestaShop 8.0

    Updated 4 January 2024

    In this blog, we are going to learn how we can show aggregate function value in the render list on the Prestashop Admin modern controller based on Symfony.

    First of all, we need to understand the aggregate function:

    An aggregate function in SQL performs a calculation on multiple values and returns a single value. SQL provides many aggregate functions that include avg, count, sum, min, max, etc.

    In this blog, We are taking an example of the count aggregate function.

    With the new PrestaShop, things are handled differently & we going to explain this in detail & we are going to explain an integration process with the help of a live example of “adminProductController”.

    Searching for an experienced
    Prestashop Company ?
    Find out More

    In this example, We are going to show the product image count in the listing by adding a new column. Let’s Start.

    Step 1:-

    Register the actionAdminProductsListingFieldsModifier hook in your module.

    $this->registerHook('actionAdminProductsListingFieldsModifier');

    Now we need to create a related function as per PrestaShop flow to act.

    public function hookActionAdminProductsListingFieldsModifier($list)
    {
    
        if (isset($list['sql_select'])) {
            $list['sql_select']['id_image'] = array(
                "table" => "pi",
                "field" => "id_image",
                "filtering" => "  %s "
            );
            $list['sql_select']['image_count'] = array(
                'select' => 'COUNT(pi.id_image)',
                'filtering' => " %s "
            );
        }
        if (isset($list['sql_table'])) {
            $list['sql_table']['pi'] = array(
                "table" => "image",
                "join" => "LEFT JOIN",
                "on" => "pi.`id_product` = p.`id_product`"
            );
    
            $list['sql_group_by'][] = 'p.id_product';
        }
    }

    Step 2:-

    We need to override these below listed two files below in the custom module. Here is the file path.

    In the below file, we have added a header column named “Image count”.

    Path: ROOT_DIR/modules/YOUR_MODULE_NAME/views/PrestaShop/Admin/Product/CatalogPage/Lists/products_table.html.twig

    <th scope="col" class="text-center" style="width: 9%">  
        {{ "Image count"|trans({}, 'Admin.Global') }}
    </th>
    Header_Column

    In the below file, we have added a value of column.

    Path: ROOT_DIR/modules/YOUR_MODULE_NAME/views/PrestaShop/Admin/Product/CatalogPage/Lists/list.html.twig

    <td class="text-center">
        {% if product.id_image is defined %}
            {% if product.id_image != '' %}
                    <span class="">{{ product.image_count }}</span>
            {% else %}
            -
            {%endif%}
        {% else %}
        -
        {%endif%}
    </td>
    Listing_Value

    After adding the code you will get the result on the admin product listing and you can see the image count in the column.

    Listing

    You can see the product contains the same number of images when checking inside the product.

    Image_count

    That’s all about this blog.

    If you are facing any issues or doubts in the above process, please feel free to contact us through the comment section.

    I would be happy to help.

    Also, you can explore our PrestaShop Development Services and a large range of quality PrestaShop Modules.

    For any doubt contact us at [email protected]

    . . .

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    Be the first to comment.

    Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home