Jump to content

Color attribute image in cart summary


lorenzoripa

Recommended Posts

How can I show the color of the attributes in the cart summary?
Let me explain, when I look at the trolley I have for example the leather label and the color label.
I would like the latter to be the image inserted in the skin-color combination

 

I'm using prestashop 1.7.7.3

 

Thanks

product-page.jpg

cart-summary.jpg

Link to comment
Share on other sites

  • 1 year later...
On 12/18/2022 at 4:08 AM, 4you.software said:

Hi.
For such modifications, it is necessary to create an override Cart.php (function getProducts) and modifications in the TPL template.
There is nothing complicated about the global, but solving it with a module is quite time-consuming, considering that each template can have a different TPL.

Thanks a lot for the answer.

i looked at the cart.php file and found the function that creates the array.

I did some tests, inserted id_attribute in the select and I can pass it in the tpl file.

I would like to leave the two arrays (attributes and attributes_small) and create a new one, for example attributes_big where I also insert the id_attribute

but if I try to create it it doesn't work well, in the tpl file, it twists 0: and all the id_attributes together example 121125136 and not 121 first item 125 second item etc

Could you give me some advice please

thanks

Ilario

Link to comment
Share on other sites

 

hi

in cart.php I add 'id_attribute' I use this because then paste put img/co/ ... .jpg and I have the link of the image then I assign new variable and create links the code basically creates a string separated by '-' and everything works fine

 

public static function cacheSomeAttributesLists($ipa_list, $id_lang)

..........

 'SELECT  pac.`id_attribute`, pac.`id_product_attribute`, agl.`public_name` AS public_group_name, al.`name` AS attribute_name
// here add 'id_attribute' 
...........

  			self::$_attributesLists[$key]['attributes'] .= $row['public_group_name'] . $colon . $row['attribute_name'] . $separator . ' ';
            self::$_attributesLists[$key]['attributes_small'] .= $row['attribute_name'] . $separator . ' ';

// here i add the new variable and then i do the rtrim
            self::$_attributesLists[$key]['attributes_url_image'] .=  _PS_COL_IMG_DIR_  .  $row['id_attribute'] . '.jpg' . $separator . ' ' ;
           
..............

			self::$_attributesLists[$id_product_attribute . '-' . $id_lang]['attributes_url_image'] = rtrim(
                self::$_attributesLists[$id_product_attribute . '-' . $id_lang]['attributes_url_image'],
                $separator . ' '
            );
            

nel file modal.tpl  or file cart-detailed-product-line

{foreach from=$product.attributes_url_image key="attribute" item="value"}
      <div class="product-line-info">
        <span  class="label"> att {$attribute}:</span>
        <span  class="value">val img {$value}</span>
      </div>
    {/foreach}
    
here is an example then I fix it

{foreach from=$product.attributes_id_image key="attribute" item="value"}
      <div class="product-line-info">
        <span class="label">{$attribute}:</span>
        <span class="value">{$value}</span>
      </div>
    {/foreach}

{$product.attributes|@print_r}

here it returns me an array


{$product.attributes_url_image|@print_r}

here is string  

I can't find where prestashop transforms the string $product.attributes (which caches function cacheSomeAttributesLists) in an array

 

thanks

Ilario

  • Like 2
Link to comment
Share on other sites

Cart.php

public static function cacheSomeAttributesLists($ipa_list, $id_lang)
    {
        if (!Combination::isFeatureActive()) {
            return;
        }

        $pa_implode = [];
        $separator = Configuration::get('PS_ATTRIBUTE_ANCHOR_SEPARATOR');

        if ($separator === '-') {
            // Add a space before the dash between attributes
            $separator = ' -';
        }

        foreach ($ipa_list as $id_product_attribute) {
            if ((int) $id_product_attribute && !array_key_exists($id_product_attribute . '-' . $id_lang, self::$_attributesLists)) {
                $pa_implode[] = (int) $id_product_attribute;
                self::$_attributesLists[(int) $id_product_attribute . '-' . $id_lang] = self::DEFAULT_ATTRIBUTES_KEYS;
            }
        }

        if (!count($pa_implode)) {
            return;
        }

        $result = Db::getInstance()->executeS(
            'SELECT pac.`id_product_attribute`, agl.`public_name` AS public_group_name, al.`name` AS attribute_name, 
            ag.is_color_group, a.id_attribute as color_id_attribute, a.color as color_value
            FROM `' . _DB_PREFIX_ . 'product_attribute_combination` pac
            LEFT JOIN `' . _DB_PREFIX_ . 'attribute` a ON a.`id_attribute` = pac.`id_attribute`
            LEFT JOIN `' . _DB_PREFIX_ . 'attribute_group` ag ON ag.`id_attribute_group` = a.`id_attribute_group`
            LEFT JOIN `' . _DB_PREFIX_ . 'attribute_lang` al ON (
                a.`id_attribute` = al.`id_attribute`
                AND al.`id_lang` = ' . (int) $id_lang . '
            )
            LEFT JOIN `' . _DB_PREFIX_ . 'attribute_group_lang` agl ON (
                ag.`id_attribute_group` = agl.`id_attribute_group`
                AND agl.`id_lang` = ' . (int) $id_lang . '
            )
            WHERE pac.`id_product_attribute` IN (' . implode(',', $pa_implode) . ')
            ORDER BY ag.`position` ASC, a.`position` ASC'
        );

        $colon = Context::getContext()->getTranslator()->trans(': ', [], 'Shop.Pdf');
        foreach ($result as $row) {
            $key = $row['id_product_attribute'] . '-' . $id_lang;
            $color = '';
            $colorImg = '';
            if ($row['is_color_group'] == '1'){
                if ($row['color_value']){
                    $color = '<span style="background-color&colon;red;height&colon;25px;width&colon;25px;display&colon;inline-block;border-radius&colon;50%;border&colon;2px solid grey;margin-bottom&colon;-6px;&colon;margin-left&colon;3px;"></span>';
                    self::$_attributesLists[$key]['attributes'] .= $row['public_group_name'] . $colon . $color;
                } else {
                    $colorImg = '<img src="/img/co/'.$row['color_id_attribute'].'.jpg" height="25" style="display&colon;inline-block;margin-bottom&colon;-6px;&colon;margin-left&colon;3px;">';
                    self::$_attributesLists[$key]['attributes'] .= $row['public_group_name'] . $colon . $colorImg;
                }
                
            } else {
                self::$_attributesLists[$key]['attributes'] .= $row['public_group_name'] . $colon . $row['attribute_name'] . $separator . ' ';
            }
            self::$_attributesLists[$key]['attributes_small'] .= $row['attribute_name'] . $separator . ' ';
        }

        foreach ($pa_implode as $id_product_attribute) {
            self::$_attributesLists[$id_product_attribute . '-' . $id_lang]['attributes'] = rtrim(
                self::$_attributesLists[$id_product_attribute . '-' . $id_lang]['attributes'],
                $separator . ' '
            );

            self::$_attributesLists[$id_product_attribute . '-' . $id_lang]['attributes_small'] = rtrim(
                self::$_attributesLists[$id_product_attribute . '-' . $id_lang]['attributes_small'],
                $separator . ' '
            );
        }
    }

 

  • Like 1
  • Thanks 1
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...