1

I have a bit of code that overwrites some basic functionality and shows out-of-stock products in the dropdown for configurable products (taken from here).

That all works fine, but instead of them adding to card and then just getting a notification to say it's out of stock, I'd like to append (Out of Stock) after the option title.

These are the relevant lines from the template file (configurable.phtml):

<?php foreach($_attributes as $_attribute): ?>
    <?php #Mage::log('<pre>'. print_r($_attribute->isAvailable(), 1) .'</pre>', null, 'mum.log', true); ?>
    <dt><label class="required"><em>*</em><?php echo $_attribute->getLabel() ?></label></dt>
    <dd<?php if ($_attribute->decoratedIsLast){?> class="last"<?php }?>>
        <div class="input-box">
            <select name="super_attribute[<?php echo $_attribute->getAttributeId() ?>]" 
                    id="attribute<?php echo $_attribute->getAttributeId() ?>" 
                    class="required-entry super-attribute-select">
                <option><?php echo $this->__('Choose an Option...') ?></option>
            </select>
        </div>
    </dd>
<?php endforeach; ?>

It's more or less the base theme template file with the addition of the Mage::log().

I've tried using:

  • $_attribute->isAvailable()
  • $_attribute->isSaleable()

But both don't work, which led me to use get_class_methods() on $_attribute and I can see I only get:

(
    [0] => addPrice
    [1] => getLabel
    [2] => getIdFieldName
    [3] => getId
    [4] => setId
    [5] => getResourceName
    [6] => getResourceCollection
    [7] => getCollection
    [8] => load
    [9] => afterLoad
    [10] => save
    [11] => afterCommitCallback
    [12] => isObjectNew
    [13] => getCacheTags
    [14] => getCacheIdTags
    [15] => cleanModelCache
    [16] => delete
    [17] => getResource
    [18] => getEntityId
    [19] => clearInstance
    [20] => __construct
    [21] => _addFullNames
    [22] => _initOldFieldsMap
    [23] => _prepareSyncFieldsMap
    [24] => isDeleted
    [25] => hasDataChanges
    [26] => setIdFieldName
    [27] => addData
    [28] => setData
    [29] => unsetData
    [30] => unsetOldData
    [31] => getData
    [32] => _getData
    [33] => setDataUsingMethod
    [34] => getDataUsingMethod
    [35] => getDataSetDefault
    [36] => hasData
    [37] => __toArray
    [38] => toArray
    [39] => _prepareArray
    [40] => __toXml
    [41] => toXml
    [42] => __toJson
    [43] => toJson
    [44] => toString
    [45] => __call
    [46] => __get
    [47] => __set
    [48] => isEmpty
    [49] => _underscore
    [50] => _camelize
    [51] => serialize
    [52] => getOrigData
    [53] => setOrigData
    [54] => dataHasChangedFor
    [55] => setDataChanges
    [56] => debug
    [57] => offsetSet
    [58] => offsetExists
    [59] => offsetUnset
    [60] => offsetGet
    [61] => isDirty
    [62] => flagDirty
)

So it seems, there's no obvious method for handling my task.

I then moved onto getData() as that seemed the most likely avenue.

But using array_keys() shows:

(
    [0] => product_super_attribute_id
    [1] => product_id
    [2] => attribute_id
    [3] => position
    [4] => product_attribute
    [5] => label
    [6] => use_default
    [7] => prices
    [8] => decorated_is_even
    [9] => decorated_is_last
)

I also tried setting it in the Configurable.php file (mentioned above):

if ($product->isAvailable() != 1) {
    $product->setData('name', $product->getData('name'). ' (Out of Stock)');
}

$products[] = $product;

But that does it to the product title, not the option in the dropdown menu.

How do I go about appending "Out of Order" to configurable product options if they're... well out of stock?

0

Try the following:

$stockData = [];
$typeInstance = $configurableProduct->getTypeInstance();
$configurableAttributes = $typeInstance->getConfigurableAttributes($configurableProduct);
foreach ($configAttributes as $attribute) {
    $simpleProduct = $this->productRepository->getById($attribute->getProductId());
    $stockData[$attribute->getProductId()]['is_saleable'] = $simpleProduct->isSaleable();
}

For Magento 1 replace $this->productRepository->getById($attribute->getProductId()) with Mage::getModel('catalog/product')->load($attribute->getProductId()).

  • can you write this as a Magento 1 answer please :) $configurableProduct isn't a variable in my code :S – treyBake Oct 30 '19 at 10:31
  • I don't know which template you're modifying, but try $_product, if you don't have it load product like this: $_product = $this->getProduct(). I would also recommend to create a helper or block method, put this code there and pass product to the new method to build the $stockData array there, but not in a template. – countid Oct 30 '19 at 17:10

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.