6

I'm looping through some products using the following:

public function getAllowProducts()
{
    if (!$this->hasAllowProducts()) {
        $products = array();
        $allProducts = $this->getProduct()->getTypeInstance(true)->getUsedProducts(array('status'), $this->getProduct());

        foreach ($allProducts as $product):
            $status = $product->getStatus();
            Mage::log($product->getSku() . ' status: ' . $status, null, 'debug.log');
            if(Mage::getStoreConfig('custom_stock/configurableproducts/configoutofstock', Mage::app()->getStore()->getId()) && $status != 2):
                $products[] = $product;
            else:
                $qty=(int)$product->getStockItem()->getQty();
                $backordered = $product->getStockItem()->getBackorders();
                $isOutOfStock = $product->getStockItem()->getIsInStock();
                if ($qty > 0 && $status != 2):
                    $products[] = $product;
                elseif(($backordered == 1 || $backordered == 2) && $isOutOfStock == 1 && $status != 2):
                    $products[] = $product;
                endif;
            endif;
        endforeach;
        $this->setAllowProducts($products);
    }

My issue is, that the value returned by $product->getStatus() is wrong. It is returning 1 (enabled) in all cases, even though a direct query to the product shows that many has status 2 (disabled).

Can anyone help me to get the correct status on the products?

I'm using magento 1.9.1.1

  • please show direct query .. – Amit Bera May 22 '15 at 14:36
3

Taking a closer look at the getUsedProducts you will see that the result that it returns when null is passed through is a just a simple collection result. What this means is that the full product model is not loaded but only a very small subset of data. In this result status is not return. Then reason that you always get a result saying the product is enabled is due that in Mage_Catalog_Model_Product there is a getStatus function that if status is not set it will return that the product is enabled.

A quick solution to your problem is to include the attributes that you would like when calling the getUsedProducts function. I have include some sample code below. If you need any other attributes be sure to include them in the array

$entityId     = Mage::getModel('eav/entity_type')
                    ->loadByCode('catalog_product');
$attribute_id = Mage::getModel('eav/entity_attribute')
                    ->loadByCode($entityId, 'status');

$allProducts  = $this
                    ->getProduct()
                    ->getTypeInstance(true)
                    ->getUsedProducts(array($attribute_id), $this->getProduct());

foreach ($allProducts as $product){
    $status = $product->getStatus();
}
  • Your are right, the status should be set. But sadly it doesn't solve the issue, the getStatus still returns 1 i all instances with your example. – dannevang May 22 '15 at 14:43
  • My apologies, I skimmed over the fact that getUsedProducts requires attribute id not attribute code. I have edited my answer to reflect this. – WilhelmE May 22 '15 at 17:00
2

This issue was plaguing me briefly as well. Since upgrading an install to EE 1.14, child products that formerly were hidden began showing up even though their status was set to “disabled”. The way I was able to solve the problem was inspired by an answer on a related question. Once I explicitly added status to configurable child products like this, everything started working as expected:

<config>
    <frontend>
        <product>
            <configurable>
                <child>
                    <attributes>
                        <status/>
                    </attributes>
                </child>
            </configurable>
        </product>
    </frontend>
</config>

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.