Jump to content

1.7.x - Display attributes (size and colors) of Product on Product List / Category Pages


Pagine Verdi

Recommended Posts

Hi guys,
I need to print the product attributes (size / color) in the list of products in the category.

I have already edited the "posbestsellers", "posnewproducts" and "ps_categoryproducts" modules to display them, modifying their controllers by adding this:

foreach ($products as &$product) {
            $product_object = new Product((int)$product['id_product']);
            $groups = array();

            $attributes_groups = $product_object->getAttributesGroups($this->context->language->id);
            if (is_array($attributes_groups) && $attributes_groups) {
                foreach ($attributes_groups as $k => $row) {
                    if (!isset($groups[$row['id_attribute_group']])) {
                        $groups[$row['id_attribute_group']] = array(
                            'group_name' => $row['group_name'],
                            'name' => $row['public_group_name'],
                            'group_type' => $row['group_type'],
                            'default' => -1,
                        );
                    }

                    $groups[$row['id_attribute_group']]['attributes'][$row['id_attribute']] = array(
                        'name' => $row['attribute_name'],
                        'html_color_code' => $row['attribute_color'],
                        'texture' => (@filemtime(_PS_COL_IMG_DIR_.$row['id_attribute'].'.jpg')) ? _THEME_COL_DIR_.$row['id_attribute'].'.jpg' : '',
                        'selected' => (isset($product_for_template['attributes'][$row['id_attribute_group']]['id_attribute']) && $product_for_template['attributes'][$row['id_attribute_group']]['id_attribute'] == $row['id_attribute']) ? true : false,
                    );

                    if ($row['default_on'] && $groups[$row['id_attribute_group']]['default'] == -1) {
                        $groups[$row['id_attribute_group']]['default'] = (int) $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']] += (int) $row['quantity'];
                }

                // wash attributes list (if some attributes are unavailables and if allowed to wash it)
                if (!Product::isAvailableWhenOutOfStock($product_object->out_of_stock) && Configuration::get('PS_DISP_UNAVAILABLE_ATTR') == 0) {
                    foreach ($groups as &$group) {
                        foreach ($group['attributes_quantity'] as $key => &$quantity) {
                            if ($quantity <= 0) {
                                unset($group['attributes'][$key]);
                            }
                        }
                    }
                }
            }
            $product['groups'] = $groups;
        }

and printing in the theme template.

But I can't figure out how to make this change for the list of products in the categories.

Can someone help me?

Link to comment
Share on other sites

  • 1 month 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...