Question

I am having some trouble and perhaps a few people here can expand my knowledge. Here is the code I am using to retrieve some information about a product on my cart page.

    if($product->getTypeId() == "configurable"){
        $sku = $_item->getSku();
        var_dump($sku);
        $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
        $invStatus = $product->getResource()->getAttribute('product_inventory_status')->getFrontend()->getValue($product);
        var_dump($invStatus);
        $quantity = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
        var_dump($quantity);

If the product is found to be configurable I retrieve the sku of the simple product it is associated with and then set the product to that configured simple product. I then get the inentory status and the quantity of that product. And this is where the issue arises. This is the result of that vardump:

string(6) "421199" string(2) "No" string(6) "1.0000" 

When a similar methodology is enacted upon simple products I receive the following from the following code:

        var_dump($product->getSku());
        $invStatus = $product->getResource()->getAttribute('product_inventory_status')->getFrontend()->getValue($product);
        var_dump($invStatus);
        $quantity = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
        var_dump($quantity);

.

string(4) "DWCP" string(1) "Z" string(6) "0.0000"

The inventory status is definitely also set from the admin panel. Any suggestions or incites would be appreciated.

--

Also, if I add the simple product as the item itself (from another part of the site) then the string resolves to the correct value. It's when I'm fetching the product in this manner that non-resolvency becomes apparent.

Why doesn't magento handle products uniformly?? It makes NO sense at all.

Was it helpful?

Solution

The issue I was running into was that loadByAttribute() does not act in the same way that load() does and therefore I would need to fetch the id of the simple product then once again load the simple product. Below is the corrected code:

$productId = $_item->getProductId();
$product = Mage::getModel('catalog/product')->load($productId);
if($product->getTypeId() == "configurable"){
    $sku = $_item->getSku();
    $simpProdId = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku)->getId();
    $product = Mage::getModel('catalog/product')->load($simpProdId);
}
$invStatus = $product->getResource()->getAttribute('product_inventory_status')->getFrontend()->getValue($product);
$quantity = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();

OTHER TIPS

If I understand correctly your problem, you need to change :

    $sku = $_item->getSku();
    $product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
    $invStatus = $product->getResource()->getAttribute('product_inventory_status')->getFrontend()->getValue($product);
    var_dump($invStatus);
    $quantity = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
    var_dump($quantity);

by

    $invStatus = $_item->getResource()->getAttribute('product_inventory_status')->getFrontend()->getValue($_item);
    var_dump($invStatus);
    $quantity = Mage::getModel('cataloginventory/stock_item')->loadByProduct($_item)->getQty();
    var_dump($quantity);

Normaly, $_item is an instance of mage_catalog_model_product. Therefore, you can use the method of that class. You should have access to all the attributes of you product if their visibility is set accordingly to the place where you call the product.

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