Jump to content

How does it work to get products images combination in product.tpl ?


Cloud Nine

Recommended Posts

Hello,

Can you explain me how this code works in product.tpl ?

I know php, JavaScript so i understand that there is a foreach loop that fill a JavaScript array.

But i can’t find where the php array is from. It should be in Product.php Class but i don’t find it.

I ask because i try to understand how to get combination product images.

Thanks

 

the code :

// Images
var img_prod_dir = '{$img_prod_dir}';
var combinationImages = new Array();

{if isset($combinationImages)}
    {foreach from=$combinationImages item='combination' key='combinationId' name='f_combinationImages'}
        combinationImages[{$combinationId}] = new Array();
        {foreach from=$combination item='image' name='f_combinationImage'}
            combinationImages[{$combinationId}][{$smarty.foreach.f_combinationImage.index}] = {$image.id_image|intval};
        {/foreach}
    {/foreach}
{/if}

combinationImages[0] = new Array();
{if isset($images)}
    {foreach from=$images item='image' name='f_defaultImages'}
        combinationImages[0][{$smarty.foreach.f_defaultImages.index}] = {$image.id_image};
    {/foreach}
{/if}
Link to comment
Share on other sites

Try in :

/controllers/front/ProductController.php

 

This is the file that calls product.tpl, and here are the smarty vars set.

 

Like:

 

$this->context->smarty->assign(array(
'groups' => $groups,
'combinations' => $combinations,
'colors' => (count($colors)) ? $colors : false,
'combinationImages' => $combination_images));
 

 

 

hope that helps,

pascal.

Link to comment
Share on other sites

Hello,

Thank you very much. It helps me !

So, ProductController generates the product page.

I found the method assignAttributesCombinations in the ProductController class. I understand that assignAttributesCombinations get and assign attributes combinations informations.

I’m creating a module that show all products in a category with all combinations. So, i should use assignAttributesGroups, do you think it’s correct ?

I was using getCombinationImages and getAttributeCombinations to get combinations datas of the products in a loop like this :

$products is an array that contains all product of a category.

for ($i = 0; $i < count($products); $i++) {
                $combinations[$i] = $this_product->getCombinationImages($params['cookie']->id_lang);
}

print_r($combinations);

what do you think of that ?

 

Thanks, best regards,

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

Hello,

First, thank you for your help.

I’ve downloaded your module and studied it. I understand how it works. In fact, i was able to do that before asking for some help here. But it shows me that i was in the right direction.

So, to explain what i try to do, let’s have a look at this picture :

 

896254prestashopcombinations.jpg

I try to make a product listing that shows all product with their combinations. On the picture, you will see that there are products with id 1, 2, 3 that have no combination. Then, product with id = 4 have 3 combinations and we can see the 3 images on the product list.

Is that clear for you or not ?

Si, i have made a module.

First, i get all the products in a category :


$category_1 = new Category(4, Configuration::get('PS_LANG_DEFAULT'));
$products = $category_1->getProducts((int)($params['cookie']->id_lang), 1, ($nb ? $nb : 10));

So, i have all my products.

Then, i make a loop to get all the combination. It works, you can test.

$combinations = array();
for ($i = 0; $i < count($products); $i++) {
                $this_product = new Product($products[$i]['id_product']);
               $combinations[$i] = $this_product->getAttributeCombinations($params['cookie']->id_lang);

If we print the array or make a {debug} in smarty, we see the content of the array :

Array
(
[0] => Array
(
[0] => Array
(
[id_product_attribute] => 165
[id_product] => 143
[reference] => XXXX-B6RG
[supplier_reference] =>
[location] =>
[ean13] =>
[upc] =>
[wholesale_price] => 0.000000
[price] => 0.000000
[ecotax] => 0.000000
[quantity] => 43
[weight] => 0.000000
[unit_price_impact] => 0.00
[default_on] => 1
[minimal_quantity] => 1
[available_date] => 0000-00-00
[id_shop] => 1
[id_attribute_group] => 2
[is_color_group] => 1
[group_name] => Couleur
[attribute_name] => Rouge
[id_attribute] => 6
)

[1] => Array
(
[id_product_attribute] => 166
[id_product] => 143
[reference] => XXXX-B6BE
[supplier_reference] =>
[location] =>
[ean13] =>
[upc] =>
[wholesale_price] => 0.000000
[price] => 0.000000
[ecotax] => 0.000000
[quantity] => 53
[weight] => 0.000000
[unit_price_impact] => 0.00
[default_on] => 0
[minimal_quantity] => 1
[available_date] => 0000-00-00
[id_shop] => 1
[id_attribute_group] => 2
[is_color_group] => 1
[group_name] => Couleur
[attribute_name] => Bleu
[id_attribute] => 8
)

// and so on

If i use getCombinationImages, i can also get id_image so i guess i am very closed to the solution :

Array
(
[0] => Array
(
[165] => Array
(
[0] => Array
(
[id_image] => 58
[id_product_attribute] => 165
[legend] =>
)

)

[168] => Array
(
[0] => Array
(
[id_image] => 156
[id_product_attribute] => 168
[legend] =>
)

)

Here, what i'm missing is how to used this aray in smarty beacause a simple foreach doesn't work because the array is really complex.

 

do you think i need to change something ?

 

thank you for your advices.


 

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

Maybe it helps if you play a little with it here:

 

http://writecodeonline.com/php/

 

 

I made some arrays that mimic the code you presented above. At the end you see the (indeed complex) array code. I can imagine though that to get the values using the objects instead of the arrays directly it may be easier.....


$arr = array
(array
(array
(
'id_product_attribute' => 165,
'id_product' => 143,
'reference' => XXXX-B6RG,
'supplier_reference' => null ,
'location' => null,
'ean13' => null,
'upc' => null,
'wholesale_price' => 0.000000,
'price' => 0.000000,
'ecotax' => 0.000000,
'quantity' => 43,
'weight' => 0.000000,
'unit_price_impact' => 0.00,
'default_on' => 1,
'minimal_quantity' => 1,
'available_date' => 0000-00-00,
'id_shop' => 1,
'id_attribute_group' => 2,
'is_color_group' => 1,
'group_name' => Couleur,
'attribute_name' => Rouge,
'id_attribute' => 6,
),
array
(
'id_product_attribute' => 166,
'id_product' => 143,
'reference' => XXXX-B6BE,
'supplier_reference' => null ,
'location' => null,
'ean13' => null,
'upc' => null,
'wholesale_price' => 0.000000,
'price' => 0.000000,
'ecotax' => 0.000000,
'quantity' => 53,
'weight' => 0.000000,
'unit_price_impact' => 0.00,
'default_on' => 0,
'minimal_quantity' => 1,
'available_date' => 0000-00-00,
'id_shop' => 1,
'id_attribute_group' => 2,
'is_color_group' => 1,
'group_name' => Couleur,
'attribute_name' => Bleu,
'id_attribute' => 8,
)
));


$imgarr = array
(array
(
"165" => array
(array
('id_image' => 58,
'id_product_attribute' => 165,
'legend' => null
)
),

"168" => array
(array
(
'id_image' => 156,
'id_product_attribute' => 168,
'legend' => null
)
)
));


echo '$arr = ';
print_r($arr);
echo'<br />';
echo'<br />';

echo '$imgarr = '; 
print_r($imgarr); 
echo'<br />'; 
echo'<br />'; 

echo "example of how the get first id_prod_atr =".$arr[0][0][id_product_attribute]; 
echo'<br />'; 
echo "example of how the get second id_prod_atr =".$arr[0][1][id_product_attribute];
echo'<br />';

// try out step, if we want image array contents of attr 165 
echo "image array contents of attribute 165 = "; 
print_r($imgarr[0][165][0]); 
echo'<br />'; 

echo "image of first attribute ".$arr[0][0][id_product_attribute]." is ".$imgarr[0][$arr[0][0][id_product_attribute]][0][id_image];




Copy the code to the online php coder and play with it a little. Maybe this gives some more insight....

pascal.

Link to comment
Share on other sites

  • 3 months later...
  • 1 year later...
Guest locen

Hi, i have this problem with combinations:

 

- I enabled the combinations and with the first visualization you show also photos that are only for the other combination.

- Then if you change combination and you rechange combinations like the first time all photos are will be ok like image associated with the combinations. 

 

can anyone help me?

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