Categories


Archives


Recent Posts


Categories


Product Status is Not a Boolean

astorm

Frustrated by Magento? Then you’ll love Commerce Bug, the must have debugging extension for anyone using Magento. Whether you’re just starting out or you’re a seasoned pro, Commerce Bug will save you and your team hours everyday. Grab a copy and start working with Magento instead of against it.

No Frills Magento Layout is the only Magento front end book you'll ever need. Get your copy today!

Magento Version: 1.5.0.1

Let’s say you have a product object

$p = Mage::getProduct('catalog/product')->load(3425);

You can get the value of the product’s status with the magic getter method or the getData method.

$p->getStatus()
$p->getData('status');

However, the return value of this method is not a boolean. The value of a product object’s status (Enabled, Disable by default) is the numerical value of the select in the admin. A false status means a status value isn’t set. It doesn’t mean that the status is disabled

if(!$p->getStatus())
{
    echo "No Status Set";
}
else if($p->getStatus() == 1)
{
    echo "Status: Enabled";
}
else if($p->getStatus() == 2)
{
    echo "Status: Disabled";
}

Not rocket science, but one of those little gotchas that can easily trip you up.

Update: Fabian over on the twitters points out that there’s class constants for the various status values.

Mage_Catalog_Model_Product_Status::STATUS_ENABLED

Shame on me

Copyright © Alan Storm 1975 – 2017 All Rights Reserved

Originally Posted: 10th October 2011