Question

Okay, i'm relatively new to magento and this has been bugging me -- because it's something I would've even considered and it's stopped my ability to complete a project. Why does Magento instantiate 'product' multiple different ways? For example when I load a product using 'loadByAttribute()' many aspects of that product simply aren't present. When I use 'load()' they are! IMO they should be the same product model. I don't see a benefit to making them completely different. Below is some code to clarify what i'm talking about. Perhaps someone can correct my thinking or clarify my thought process to better align with magmento practice.

I am getting the realized simple product of a configured item in the cart. To do this I need to do the following:

$productId = $_item->getProductId();
//this loads the initial configurable or simple product
$product = Mage::getModel('catalog/product')->load(productId);
//this is used to get the simple product of the configurable product
if($product->getTypeId() == "configurable"){
    sku = $_item->getSku();
    //this loads a sort of 'psuedo-product' with only some attributes
    $producted = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
    //this loads the actual product with all attributes
    $product = Mage::getModel('catalog/product')->load($producted->getId());
}

Is there a simpler way to do this?

Was it helpful?

Solution

Because you're using the Use Flat Catalog Product and the attribute isn't set to display on the front end.

load always loads into the php object the entity data from the database.

loadByAttribute (defined in Mage/Catalog/Model/Abstract.php) returns the first element in the model resource collection (if it exists). However, the collection checks to see if the store has the Use Flat Catalog Product enabled, if it does, then the Product objects it instantiates will only have attributes from the flat table. If you want to access the attribute from an object returned from loadByAttribute set the attribute to be visible on the front end and rebuild the flat table.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top