0

I am trying to change the availability message in category view in the file list.phtml depend on the qty of the product. I did on the simple products but I couldn't manage to get results for the configurable products. I tried to use the function that I found on this answer How to get stock quantity of child product from configurable product in Magento 1.9. It is working as a function in the product view.phtml but in list.phtml I get an error like Undefined function getTypeId(). What I should add to solve the problem?

0

To answer you main question. If I am not wrong you are using this function.

function getChildrenStockQTY($product, $stock_qty = 0) {
    if ($product->getTypeId() != 'configurable') return false;
    $simple_ids = Mage::getResourceSingleton('catalog/product_type_configurable')->getChildrenIds($product->getId());
    foreach ($simple_ids[0] as $simple_id) {
        $simple_model = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple_id);
        $stock_qty += $simple_model->getQty();
    }
    return $stock_qty;
}

Change this to

function getChildrenStockQTY($product, $stock_qty = 0) {
    $product = Mage::getModel('catalog/product')->load($product->getId());
    if ($product->getTypeId() != 'configurable') return false;
    $simple_ids = Mage::getResourceSingleton('catalog/product_type_configurable')->getChildrenIds($product->getId());
    foreach ($simple_ids[0] as $simple_id) {
        $simple_model = Mage::getModel('cataloginventory/stock_item')->loadByProduct($simple_id);
        $stock_qty += $simple_model->getQty();
    }
    return $stock_qty;
}

This function is using $product which is loaded on the listing page which is an object of product flat data and not product model. Product flat data doesn't include type id of product. Therefore we have to load

There is no simpler way to do this. It can only be achieved by getting child product ids from configurable product than loading those child products one by one and saving their stock details in an associative array. But let me warn you that this'll increase the load time of every listing page with configurable products on your Magento's frontend.

2
  • Thanks for the answer but where is the difference between the first and the second one? They are the same exactly.
    – George K.
    Dec 14 '20 at 17:07
  • Sorry. Edited my answer. You can check the difference now Dec 14 '20 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.