Search code examples
phpmodel-view-controllerprestashopsmarty

Prestashop, Get other products with same ean13 in product page


I'm newbie to smarty and php.

My situation is that my product_reference is unique but product_ean13 is similar in some products which are like each other. What I need is to have product_id of those products in one of them product page. I mean when visitors open a product page I want to show Image of those products which have the same ean13. Showing the images and HTML, CSS is ok for me, my problem is in PHP,SMARTY which should pass the value from PHP file to TPL file.

I guess I should write a function in Product.php file and pass array values to product.tpl file. But I couldn't.

Would you please help me?

Edited: As you may know a weakness in Prestashop 1.6 is that if your products nature is to have colors and size, like clothes and smartphones! you have two approach to create them. first approach is to create them as a combination of one product and second approach is to create them as separated and not related products. first approach has a good point that all of them will be showed up in product page when customer visits each of them and also have a weakness of exposure in category page which all those attributes will be seen as one product.(imagine between all colors of one shirt only one is seen and your costumer may like blue more but always the red is being showed in category page. or its not easy to know from category page that you also have the gold color of the smartphone) this solution will helps you what to create your products as separated products but you what to show them in product page of each of them. this way we will use benefit of both approach and we wont have the weakness of any of them. we use ean13 (or any other unused field you have, to use as a code that is a same value in same products)


Solution

  • we should add a class in product.php to get cover images of products with the same ean13:

    public static function getImageByEan13Product($ean13_colors)
    {
        $rows = Db::getInstance()->executeS('
            SELECT `id_product`, `id_image`
            FROM `'._DB_PREFIX_.'image`
            WHERE `id_product` IN ('.implode(',', $ean13_colors).') AND `cover` = 1'
        );
        $images = array();
        foreach ($rows as $row){
            $images[] = array(
                'idProduct' => $row['id_product'],
                'idImage' => $row['id_image']
            );
        }
        return $images;
    }
    

    and the following function which gets the color attributes of the products which contain same ean13 code:

    public static function getColorByEan13Product($ean13)
    {
        $rows = Db::getInstance()->executeS('
        SELECT `id_product`
        FROM `'._DB_PREFIX_.'product`
        WHERE `ean13` = '.$ean13.' AND `active` = 1 AND `id_product` IN (SELECT DISTINCT `id_product` FROM `'._DB_PREFIX_.'stock_available` WHERE `quantity` > 0)');
        $colors = array();
        foreach ($rows as $row){
            $colors[] = $row['id_product'];                 
        }
        return $colors;
    }
    

    and also there is another class in the ProductController.php named assignAttributesGroups() which provide attributes of the product. in this class we should add following code:

    $ean13_colors = array();
    $ean13_colors = Product::getColorByEan13Product($this->product->ean13);
    $this->context->smarty->assign('ean13colors', $ean13_colors);
    
    $ean13_images = array();
    $ean13_images = Product::getImageByEan13Product($ean13_colors);
    $this->context->smarty->assign('ean13images', $ean13_images);
    

    then these two values ean13_images and ean13_coloes can be used in TPL file product.tpl and showed up instead of color attributes. like the code bellow:

    {foreach from=$ean13images key=k item=v}
        {if $product->id != $v.idProduct}
            <li>
                <a href="{$link->getProductLink($v.idProduct)|escape:'html':'UTF-8'}" class="pro_column_left selected">
                <img src="{$link->getImageLink($product->link_rewrite, $v.idImage, 'small_default')}" height="{$smallSize.height}" width="{$smallSize.width}" class="replace-2x img-responsive" {if !isset($from_product_secondary_column) || !$from_product_secondary_column} itemprop="image"{/if} />
                </a>
            </li>
        {/if}
    {/foreach}
    

    and the value ean13colors can be used where we want to show a balloon tooltip on mouseover the image to say whats the color attribute of the other product that is being showed by same ean13.