1

Suppose to have two categories: "Red" and "Blue".
I also have two icons for each one: the "Red icon" and the "Blue Icon".
I want to assign and display to the products of the Red category the Red icon in "product-name" and the same for the blue products.

Is there any way to achieve this?

1 Answer 1

1

Supposing you're not really too fussed about having a GUI to upload them, you can implement it this way. Put the following function in a custom helper for example, and call it in your catalog/product/list.phtml for each product. Assuming you create a folder in media/catalog/category called icons and you upload there the icons named as such: {CATEGORY_ID}.png

public function getCategoryIcon($product)
{   
    $path = BP . DS . 'media' . DS . 'category' . DS . 'icons';
    $url = Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_MEDIA). 'media/category/icons/';

    $categoryIds = explode(',', $product->getCategoryIds());

    if (!is_array($categoryIds) || !count($categoryIds)) return false;

    $result = array();

    foreach ($categoryIds as $categoryId):

        $iconPath = $path. DS . $categoryId . '.png';
        if (is_file($iconPath)):
            $result[] = sprintf('<img src="%s" alt="%s" />', $url.$categoryId . '.png', $categoryId);
        endif;

    endforeach;

    return $result;
}

Note this is a basic implementation, but it avoids loading collections or objects which are resource greedy. You can also improve the code above by caching the icons and then avoid to check if the file exists unnecessarily.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.