2

i have the following code in a helper file:

public  function getAvailableAttributesHelper($_product) {
    $_product   = Mage::getModel('catalog/product')->load($product_id);

    if ($_product->getTypeId()=='configurable')
    {

        //Get Attributes
        $attributes = $_product->getTypeInstance(true)->getConfigurableAttributes($_product);
        if (count($attributes)) 
        {
            $output .= '<ul>';
            foreach($attributes as $att)
            {
                $pAtt=$_product->getProductAttribute();

                //get the child products
                $allProducts = $_product->getTypeInstance(true)->getUsedProducts(null, $_product);
                $frontValues =array();
                $output .= '<ul>';
                $output .= $pAtt->getFrontendLabel();
                $output .= '<ul>';
                foreach($allProducts as $p){
                //check stock, status, ...
                //do not show unsaleable options
                if(!$p->isSaleable()) continue;
                $out=$p->getAttributeText($pAtt->getName());
                $frontValues[$out]=$out;
            }
            $output .= '<ul>';
            implode('</li><li>', $frontValues);
            $output .= '</li></ul></li>';}
        }
        $output .= '</ul>';

      } else {
         $output .= '<span>To Bad</span>';
      }
      return $output;
}

I have the original code from here: https://stackoverflow.com/questions/23950764/magento-how-to-show-configurable-product-options-in-product-list-page This way i can show the available options of the configurable products on the product list page. This is a great wish for our shop. Only we have bought a theme for our store, and that theme generates the products from a helper .... thats why i now try to get this code working in the helper.

In the \app\design\frontend\theme\default\template\catalog\list.phtml is used <?php echo Mage::helper('mymodule')->getAvailableAttributesHelper($_product) ?> but without any output. What am i doing wrong?

I have magento 1.8.1 CE

4
  • what is the name and path of a helper file ? May 31, 2015 at 8:44
  • app/code/local/Etheme/mymodule/Helper/data.php
    – Ronny
    May 31, 2015 at 8:46
  • see this inchoo link inchoo.net/magento/…
    – Amit Bera
    May 31, 2015 at 9:22
  • But i only want to show the options in the list.phtml The orginal code i had found does that for me, only due to my theme i have to code it in a helper file. The if ($_product->getTypeId()=='configurable') is false, even when it's is a configurable product
    – Ronny
    May 31, 2015 at 9:33

2 Answers 2

2

I have optimized the code.

public function getAvailableAttributesHelper($_product){
    $html='';
        if($_product->getTypeId()=='configurable'):
            $AllowAttributes=$_product->getTypeInstance(true)->getConfigurableAttributes($_product);
                 $skipSaleableCheck = Mage::helper('catalog/product')->getSkipSaleableCheck();
                $allProducts = $_product->getTypeInstance(true)
                                ->getUsedProducts(null, $_product);
                $option=array();
                foreach ($allProducts as $product) {
                // only show available option
                if(!$product->isSaleable()) continue;
                            foreach ($AllowAttributes as $attribute) {
                                    $productAttribute   = $attribute->getProductAttribute();
                                    $productAttributeId = $productAttribute->getId();
                                        $option[$productAttribute->getAttributeCode()][]=$productAttribute->getFrontend()->getValue($product);
                            }
                }


                $html='<ul>';
                foreach($option  as $key=>$eachoption){
                        //array_unique($eachoption);
                        $html.='<li>'.$key.'<ul>';  
                        //print_r(array_unique($eachoption));
                            foreach(array_unique($eachoption) as $value){
                                $html=$html.'<li>'.$value.'</li>';

                        }
                        $html.='</ul></li>';
                }
                $html.='</ul>';

        endif;
    return $html;
}

output like this:

enter image description here

2
  • Amit, this code show all options. How can i olny show the available options? For example, when in your first image only the M, L, and XL are in stock. That only these options are shown and S & XS are hidden
    – Ronny
    Jun 2, 2015 at 18:36
  • When i add ` if($product->isSaleable()) continue; after foreach ($allProducts as $product) { ... it does the opposite ... it shows the sold options ...
    – Ronny
    Jun 2, 2015 at 18:55
2

You should set some breakpoints in your code to see on which if()-condition it breaks. My guess would be, that $attributes doesn't return a result because your configurable attributes are set to "Show in category listing" -> "No" in the backend unter "Catalog -> Manage Attributes"

== EDIT ==

There are some issues in this code. When you call $pAtt->getFrontendLabel(), you don't do anything with it. You should have $html .= $pAtt->getFrontendLabel();

Second, you don't return anything from your function. Add return $html; in the last line, or the HTML code will not "get back" to your template.

10
  • When I put The original code in The 'list.phtml' then it shows The output. Only I need The helper file to place it At The wanted position
    – Ronny
    May 31, 2015 at 8:40
  • Edited my answer May 31, 2015 at 9:01
  • I edited the code. And put an else to the code to see if it works. I do get an output now ... but only To Bad
    – Ronny
    May 31, 2015 at 9:11
  • 1
    Yes I understood that :) and it is better placed there. And you made some necessary modifications to the code, but not all of them I fear. And the first line re-defines $_product, which overwrites the value you pass to the function. Does it work if you remove this line? May 31, 2015 at 10:26
  • 1
    You should check why $pAtt = ... A few lines above that error does not return a product attribute. You can use var_dump() to check what it does return. I am on mobile atm and can not test so much. Maybe you can find a hint on this in the original code, and compare what you have changed. May 31, 2015 at 10:34

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.