Jump to content

Attributes in category view - SOLVED


Recommended Posts

Here's non-paid solution, that's still in development. When I was looking for solution I realized how many people are asking for this feature, so I was trying hard to find out how it works, and I'm happy that I can share this solution. There's a little problem that I still cannot fix. If you click on Add to cart in category view, it ignores attribute you selected. This is solved quite well for quantity in category view (hyperlink included at the bottom of this post) but I still cannot fix this for attributes, any help would be appreciated. Here's the solution:


category.php - add this on line 63:


foreach ( $cat_products as $key => $cat_product ) {

$product = new Product ( intval ( $cat_product[ 'id_product' ] ), true , intval ( $cookie -> id_lang ));
$colors = array();
$groups = array();
$attributesGroups = $product -> getAttributesGroups ( intval ( $cookie -> id_lang ));
if ( Db :: getInstance ()-> numRows ())
{


foreach ( $attributesGroups AS $k => $row )
{

if (isset( $row[ 'attribute_color' ] ) AND $row[ 'attribute_color' ] AND $row[ 'id_attribute_group' ] == $product -> id_color_default )
{
$colors[$row[ 'id_attribute' ]][ 'value' ] = $row[ 'attribute_color' ] ;
$colors[$row[ 'id_attribute' ]][ 'name' ] = $row[ 'attribute_name' ] ;
$colors[$row[ 'id_attribute' ]][ 'id_image' ] =(( $row[ 'id_image' ] != NULL ) ? intval ( $row[ 'id_image' ] ) : - 1 );
}


                   $groups[$row['id_attribute_group']]['attributes'][$row['id_attribute']] = $row['attribute_name'];
                   $groups[$row['id_attribute_group']]['name'] = $row['public_group_name'];
                   $groups[$row['id_attribute_group']]['is_color_group'] = $row['is_color_group'];
                   if ($row['default_on'])
                       $groups[$row['id_attribute_group']]['default'] = intval($row['id_attribute']);
                   if (!isset($groups[$row['id_attribute_group']]['attributes_quantity'][$row['id_attribute']]))
                       $groups[$row['id_attribute_group']]['attributes_quantity'][$row['id_attribute']] = 0;
                   $groups[$row['id_attribute_group']]['attributes_quantity'][$row['id_attribute']] += intval($row['quantity']);


                   $combinations[$row['id_product_attribute']]['attributes_values'][$row['id_attribute_group']] = $row['attribute_name'];
                   $combinations[$row['id_product_attribute']]['attributes'][] = intval($row['id_attribute']);
                   $combinations[$row['id_product_attribute']]['price'] = floatval($row['price']);
                   $combinations[$row['id_product_attribute']]['ecotax'] = floatval($row['ecotax']);
                   $combinations[$row['id_product_attribute']]['weight'] = floatval($row['weight']);
                   $combinations[$row['id_product_attribute']]['quantity'] = intval($row['quantity']);
                   $combinations[$row['id_product_attribute']]['reference'] = $row['reference'];
                   $combinations[$row['id_product_attribute']]['id_image'] = isset($combinationImages[$row['id_product_attribute']][0]['id_image']) ? $combinationImages[$row['id_product_attribute']][0]['id_image'] : -1;



}

}

//wash attributes list (if some attributes are unavailables and if allowed to wash it)
               if (!Product::isAvailableWhenOutOfStock($product->out_of_stock) && Configuration::get('PS_DISP_UNAVAILABLE_ATTR') == 0) {


                   foreach ($groups AS &$group)
                       foreach ($group['attributes_quantity'] AS $key => &$quantity)
                           if (!$quantity)
                               unset($group['attributes'][$key]);
               foreach($groups AS &$group)
                   natcasesort($group['attributes']);
               foreach ($combinations AS $id_product_attribute => $comb)
               {
                   $attributeList = '';
                   foreach ($comb['attributes'] AS $id_attribute)
                       $attributeList .= '\''.intval($id_attribute).'\',';
                   $attributeList = rtrim($attributeList, ',');
                   $combinations[$id_product_attribute]['list'] = $attributeList;
               }

               }


$cat_products[$key][ 'colors' ] = $colors;
$cat_products[$key][ 'groups' ] = $groups;
$cat_products[$key][ 'combinations' ] = $combinations;
$cat_products[$key][ 'combinationImages' ] = $combinationImages;

}





To be continued in the next post.

  • Like 1
Link to comment
Share on other sites

product-list.tpl - add this to whatever column you want, I added it to center_column

            {if !empty($product.groups)}
           <!-- attributes -->

           {foreach from=$product.groups key=id_attribute_group item=group}
           {if $group.attributes|@count}


{$group.name|escape:'htmlall':'UTF-8'} :
               {assign var='groupName' value='group_'|cat:$id_attribute_group}
0}$('#wrapResetImages').show('slow');{/if}">
                   {foreach from=$group.attributes key=id_attribute item=group_attribute}
{$group_attribute|escape:'htmlall':'UTF-8'}
                   {/foreach}

               <input type="hidden" name="id_product_attribute" id="idCombination" value="" />

           {/if}
           {/foreach}

           {/if}



           {if !empty($product.colors)}
           {foreach from=$product.colors key=id_attribute item=color }    

              {/foreach}
           {/if}




And the promised hyperlink: http://www.prestashop.com/forums/viewthread/28136/

Link to comment
Share on other sites

  • 2 weeks later...
  • 3 months later...

not working for me (((

in which folder lying a file category.php?
after which code in category.php should I insert code from the first post?

And after which code in product-list.tpl should I insert code from the second post?

Link to comment
Share on other sites

  • 1 month later...
  • 2 months later...
  • 3 months later...
  • 3 months later...

I used this code, and got it working with slight modifications.

 

Replace this;

		$attributesGroups = $product->getAttributesGroups(intval($cookie->id_lang));

 

With this;

		$attributesGroups = $product->getAttributesGroups(intval($cookie->id_lang), $product->id);

 

--

 

Replace this;

					foreach ($group['attributes_quantity'] AS $key => &$quantity)
						if (!$quantity)
							unset($group['attributes'][$key]);

 

With this;

					foreach ($group['attributes_quantity'] AS $key2 => &$quantity)
						if (!$quantity)
							unset($group['attributes'][$key2]);

 

--

 

 

Replace this;

$cat_products[$key][ 'colors' ] = $colors;
$cat_products[$key][ 'groups' ] = $groups;
$cat_products[$key][ 'combinations' ] = $combinations;
$cat_products[$key][ 'combinationImages' ] = $combinationImages;

 

With this;

$this->cat_products[$key][ 'colors' ] = $colors;
$this->cat_products[$key][ 'groups' ] = $groups;
$this->cat_products[$key][ 'combinations' ] = $combinations;
$this->cat_products[$key][ 'combinationImages' ] = $combinationImages;

 

--

 

In classes/Product.php, find the getAttributesGroups function (line 2204), and replace the whole function with this;

public function getAttributesGroups($id_lang, $product_id = NULL)
{
	if($product_id == NULL) $product_id = (int)($this->id);
	if($id_lang == 0) $id_lang = 1;

 return Db::getInstance()->ExecuteS('
 SELECT ag.`id_attribute_group`, ag.`is_color_group`, agl.`name` AS group_name, agl.`public_name` AS public_group_name, a.`id_attribute`, al.`name` AS attribute_name,
 a.`color` AS attribute_color, pa.*
 FROM `'._DB_PREFIX_.'product_attribute` pa
 LEFT JOIN `'._DB_PREFIX_.'product_attribute_combination` pac ON pac.`id_product_attribute` = pa.`id_product_attribute`
 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`
 LEFT JOIN `'._DB_PREFIX_.'attribute_group_lang` agl ON ag.`id_attribute_group` = agl.`id_attribute_group`
 WHERE pa.`id_product` = '.$product_id.'
 AND al.`id_lang` = '.(int)($id_lang).'
 AND agl.`id_lang` = '.(int)($id_lang).'
 ORDER BY agl.`public_name`, al.`name`');
}

 

 

I don't know if all these modifications are actually necessary, since I tried a lot of things and I'm not a Prestashop [spam-filter] or anything, but this got it working for me. :)

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...
  • 4 months later...
  • 3 months later...

This is not a simple change, certainly not in PS 1.5, and by taking a quick look at the code that was posted in this thread, I don't see any way it would be working, as there is no Javascript anywhere, and a lot of it is required for proper use.

 

You can see the following module, which lets you add attributes in category pages (or any other product on any page) at http://www.prestashop.com/forums/index.php?/topic/79758-module-product-list-attributes-allow-customers-to-select-product-attributes-on-the-category-manufacturer-supplier-and-search-pages/

Link to comment
Share on other sites

  • 1 month later...
  • 1 year later...

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