1

I'm trying to display size shoes attribute for each product in product list (something like a native configurable swatches ) , But I got all sizes on the top of the page instead of getting them under each product name . Is there any way to create dynamic block and append it for each product or another alternative ?

I create a module and I'm using an observer to handle the collection , I don't like to have a bunch of code in list.phtml

my code is the following : observer.php

<?php  
class Campany_Swatches_Model_Observer extends Mage_Core_Model_Abstract  
{  
public function productListCollectionLoadAfter(Varien_Event_Observer $observer)  
{  
        $collection = $observer->getCollection();
        $helper = Mage::helper('Campany_swatches');
        $products = $collection->getItems();

        /* @var $product Mage_Catalog_Model_Product */
        foreach ($products as $product) {
             $helper->getAttributes($product);

         }
    }
} 

helper Data.php

<?php
class Campany_Swatches_Helper_Data extends Mage_Core_Helper_Abstract
{
    private $_html;

    public function getAttributes($_product = false)
    {
        if($_product->isConfigurable()){
        //get attributes 
           $attributes = $_product->getTypeInstance(true)->getConfigurableAttributes($_product) ;
           if(count($attributes)){
                foreach($attributes as $att){
                    $pAtt = $att->getProductAttribute();
                    //get the  product children
                    $allProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);
                    $frontValues =array();

                    if ($pAtt->getFrontendLabel() == "Shoe Size"){ 
                        foreach($allProducts as $p){
                             //check stock, status, ...
                             //do not show unsaleable options
                        if(!$p->isSaleable()) continue; 
                            $out=$p->getAttributeText($pAtt->getName()); 
                            $frontValues[$out]=$out; 
                        } 

                        $this->_html = '<div class="shoeSize">';
                        $this->_html .= '<ul class="configurable-swatch-list configurable-swatch-size_clothes ulshoeSize clearfix">';
                        $this->_html .='<li class="swatch-label">';
                        $this->_html .= implode('</li><li class="swatch-label">', $frontValues);
                        $this->_html .='</li> </ul></div>';
                    } 
                }
            }
        }

    return $this->_html;

    }
1
  • Is the product attribute's able to be used in product listing "used_in_product_listing"?
    – Raj
    Commented Apr 1, 2015 at 7:46

1 Answer 1

0

Your observer.php code does not render the html to list.phtml proper

If you observer code have create perform issue on list page. Basically code :

foreach ($products as $product) {
             $helper->getAttributes($product);

         }

create main issue for all collection twice,one at observer and another is list.phtml.

It will be good call Mage::helper('Campany_swatches')->getAttribute($_product); at list.phtml inside foreack loop of products ,this one line code is make all solution.

1
  • You ate welcome
    – Amit Bera
    Commented Apr 5, 2015 at 11:01

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.