LinkedIn Twitter RSS Reset

Getting Configurable Attributes (Super Attributes) of a Configurable Product

In one of those time when you need to know what attributes of a product is used to make the current configurable product “configurable”, then here it goes :D Those configurable attributes are called as “Super Attributes”. Looking into the lower level at the Database level the attributes id are saved in the table “catalog_product_super_attribute”. In this configurable product’s Id and the attribute are saved. To get the attribute’s id you can directly write a custom query and fetch it. But to get its attribute code, attribute level you will then have to create an object attribute using the fetched id. But there is one shot option as well to do the same, in a managed Magento Way!

Here is the way. The code comments are self explanatory I guess!!

/**
	 * Load Product you want to get Super attributes of
	 */
	$product=Mage::getModel("catalog/product")->load(52520);

	/**
	 * Get Configurable Type Product Instace and get Configurable attributes collection
	 */
	$configurableAttributeCollection=$product->getTypeInstance()->getConfigurableAttributes();

	/**
	 * Use the collection to get the desired values of attribute
	 */
	foreach($configurableAttributeCollection as $attribute){
		echo "Attr-Code:".$attribute->getProductAttribute()->getAttributeCode()."<br/>";
		echo "Attr-Label:".$attribute->getProductAttribute()->getFrontend()->getLabel()."<br/>";
		echo "Attr-Id:".$attribute->getProductAttribute()->getId()."<br/>";
	}

Happy Codding!

10 Responses to “Getting Configurable Attributes (Super Attributes) of a Configurable Product”

  1. dave
    January 19, 2010 at 5:48 pm #

    Hi, I’m doing my best to customize a magento site and I am *very* interested in finding out how to locate class methods, etc. How do you find out all of the wonderful method calls that you use? Where do you look?

    dave

    • Subesh Pokhrel
      January 20, 2010 at 4:05 am #

      All you have to do is drill down into the core classes from above and you will get all the functions you want :D Also you will have to work for a while in Magento and “slam” you head into your desk time and again for getting the answer you want! :D

      • dave
        January 20, 2010 at 5:44 pm #

        Ooops, I guess I was slamming my head first… ;O)

        Right now, I’m trying to automate some pricing updates from an external inventory system. When I update simple products that are associated with configurable products I need to also update the super attribute’s price. I have tried this: (ProductID 2937 is definitely configurable.)

        $thisCfgableProd = Mage::getSingleton(‘catalog/Product’)->load(2937) ;
        $cfgOptions = $thisCfgableProd->getTypeInstance(true) ;
        $cfgOptionsAttr = $cfgOptions->getConfigurableAttributesAsArray($thisCfgableProd) ;
        foreach ($cfgOptionsAttr as $attribute) {
        if(attribIwant) $attribute['values'][x]['pricing_value'] = 150 ;
        Mage::getResourceModel(‘catalog/Product_Type_Configurable_Attribute’)->savePrices($attribute) ;
        }

        savePrices dies right away doing
        Call to a member function getValues() on a non-object in
        …. Eav/Mysql4/Product/Type/Configurable/Attribute.php

        I’ve been grinding at this for a couple of days now. I’m just about to update the database directly, but I would like to avoid that if I can.

        Any suggestions would be *most* appreciated.

        dave

        • Subesh Pokhrel
          January 21, 2010 at 6:25 am #

          Give me some time.. i’ll make another post for this.for creating configurable products programmatically. may be today!

          • dave
            January 21, 2010 at 11:51 am #

            That would go a *long* way to get me out of this problem. Thank you for taking the time to give this a look. dave

          • dave
            January 25, 2010 at 4:33 pm #

            Well, thank you for the post regarding Eclipse. It gave me the courage to try to figure that out again and it is just what I needed! I’ve figured out how to do the configurable product price updates. In case anyone else is looking for a way to do that:

            $productid = ’2937′ ;
            $product = Mage::getSingleton(‘catalog/Product’)->load($productid);

            $configurableAttributeData = $product->getTypeInstance()->getConfigurableAttributesAsArray($product) ;

            foreach($configurableAttributeData as $attributeData){

            // Update super attributes as required

            $id = isset($attributeData['id']) ? $attributeData['id'] : null;
            Mage::getModel(‘catalog/product_type_configurable_attribute’)
            ->setData($attributeData)
            ->setId($id)
            ->setStoreId($product->getStoreId())
            ->setProductId($productid)
            ->save();
            }

  2. Bhavesh Dave
    January 20, 2010 at 7:24 am #

    hay i try this code but it give me an error
    “FFatal error: Call to undefined method Mage_Bundle_Model_Product_Type::getConfigurableAttributes() ”

    how can i solve it…

    any idea….

    • Subesh Pokhrel
      January 20, 2010 at 10:13 am #

      I think you tried for bundled product not for configurable product.

  3. Subesh Pokhrel
    January 25, 2010 at 5:47 pm #

    @Dave… Thanks for the code… if it works :D

  4. Silverscreen
    March 11, 2010 at 2:24 pm #

    How about deleting some of the super attributes?

    This seems even be impossible in the admin after setting it once for the configurable product.

Leave a Comment