Blog

Bevelwise Blog

Create a filtered list assigned to a custom attribute in Magento 1.9.1

July 12, 2017

Magento’s default product list layout is terrific, but sometimes a company needs something more. Recently in Magento 1.9.1, we created a custom list that only displayed products that contained a custom attribute. magento.jpg

Here’s how we did it.

First, we needed to create a new blank custom-attribute-list.phtml that will replace Magento’s default catalog product list. Add the file to the path displayed below. Make sure you’re working in the correct package and theme for your website.

app/design/frontend/[package]/[theme]/template/category/product/custom-attribute-list.phtml

Next, we’ll navigate to Magento’s admin and implement the category page layout to look for this file. The best approach is to use xml in the custom design tab of the category page you would like to modify. Paste this code with your file name to set the new template on the category page.

<reference name="product_list"> <action method="setTemplate"><template>catalog/product/custom-attribute-list.phtml</template></action> </reference>

Now we have a clean workspace and can start coding.

Open up the custom-attribute-list.phtml file. The first thing we need to do here is create a few variables that select Magento’s catalog output, and another one to store all the catalog products. Paste the code below at the top of your file to accomplish this.

$_helper = $this->helper('catalog/output');$_products = Mage::getModel('catalog/product')->getCollection();

The $_products variable is grabbing all the catalog products, but we need to start filtering this out so we can view only the products that we want to display on the front-end. You can do that by using the addAttributeToFilter method and passing through the variable name and value to check against. The example below grabs the visibility attribute and checks if it has the value of 4 (visible).

->addAttributeToFilter('visibility', 4)

Append this code to the $_products variable string you previously created like this.

$_products = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('visibility', 4);

Append more attributes and attribute names for a more defined result.

Here we are checking to see if the visibility is true, the custom design and imprinting is available, the status is enabled, and grabbing the name, plus small image, of the product.

$_products = Mage::getModel('catalog/product')->getCollection()->addAttributeToFilter('visibility', 4)->addAttributeToFilter('design_and_imprint_avail', 1)->addAttributeToFilter('status', 1)->addAttributeToSelect('name')->addAttributeToSelect('small_image');

Once this work is done we can run our grid phtml and run a loop for the list items. Paste this code below our variables above and save the file.

<div class="row">    <?php echo $this->getToolbarHtml() ?>    <ul class="col-md-12 products-grid"><?php         foreach($_products as $_item) { ?>        <li class="col-xs-6 col-sm-4 item<?php if(($i-1)%$_columnCount==0): ?> first<?php elseif($i%$_columnCount==0): ?> last<?php endif; ?>">            <a href="<?php echo $_item->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_item, 'small_image'), null, true) ?>" class="product-image">            <img src="<?php echo $this->helper('catalog/image')->init($_item, 'small_image')->resize(260); ?>" width="260" height="260" alt="<?php echo $this->stripTags($this->getImageLabel($_item, 'small_image'), null, true) ?>" />            </a><p class="product-name"><?php echo $_helper->productAttribute($_item, $_item->getName(), 'name') ?></p>            <?php if($_item->getRatingSummary()): ?>            <?php echo $this->getReviewsSummaryHtml($_item, 'short') ?>            <?php endif; ?>            <div class="actions">                <ul class="add-to-links">                    <li class="first">                    <a type="button" title="<?php echo $this->__('Add to Cart') ?>" onclick="setLocation('<?php echo $this->getAddToCartUrl($_item) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></a>                    </li>                    <?phpif ($this->helper('wishlist')->isAllow()) : ?>                        <li><a href="<?php echo $this->helper('wishlist')->getAddUrl($_item) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist') ?></a></li>                    <?php endif; ?>                </ul>            </div>        </li>        <?php}?>    </ul></div>

That’s it!

Fast and effective solution. Check your work in the browser. Be sure to keep in mind our custom attribute for this project was “design_and_imprint_avail” so if your work is not executing correctly, make sure you replace this attribute with your own.

Have any questions? Let us know in the comments or reach out to Vince (Our Certified Magento Developer) here. If you aren’t using Magento for your Ecommerce platform, check out some the main benefits.

Ryder SCS Blog

June 23, 2016

Ryder’s Supply Chain Solutions – Blogging Case Study on the Value of Content In 2011, Ryder Supply Chain Solutions was in the process of acquiring once of our marketing clients, Total Logistics Control (TLC) out of Holland, Michigan, and we

T-H Marine

June 16, 2016

T-H Marine Supplies began serving the marine industry in 1975. To this day, they operate as a family business closely tied to fishing and boating on an individual level, but quite a bit has changed since their start – especially

Funky Buddha

February 9, 2016

Funky Buddha Yoga Hot House opened its doors in 2010, and quickly became one of the most popular hot yoga studios in the country. Since then, Funky Buddha had been named Best of Grand rapids six consecutive times by On-the-Town,

Got a project?

We’d love to hear more about it, so get in touch!

Request a Quote