Out of Stock Configurable Products in list.phtml
-
13-12-2019 - |
Question
I need to display a "sold out" text to all products in a list.phtml file...
I'm using <?php if($_product->isSaleable()) : ?>
and it's working great for simple products but it's not working for configurable.
I've tried few things, and for example this code
if($productType == "configurable") {
$arrayvalori = array("Small"=>"0","Regular"=>"0","Large"=>"0");
$attValConfig = $product->getTypeInstance()->getConfigurableAttributesAsArray();
if(sizeof($attValConfig)) {
$hasAtts++;
foreach($attValConfig as $attValConfigSingle) {
foreach($attValConfigSingle['values'] as $attValConfigSingleVal) {
if($arrayvalori[$attValConfigSingleVal["label"]] > 0) {
echo 'buy';
} else {
echo "sold out";
}
}
}
}
}
it works but returns all of the available attributes statuses (for the given configurable product) output example:
buy
buy
buy
buy
sold out
sold out
buy
I want it to display buy if 1 or more variations are available and display sold out if all are sold out?!?!!
thanks
Solution
Answer is already in your question. As you said it is showing buy and sold out correctly after loop. You will just need to modify your code little bit.
$canBuy = false;
foreach ($attValConfig as $attValConfigSingle) {
foreach ($attValConfigSingle['values'] as $attValConfigSingleVal) {
if ($arrayvalori[$attValConfigSingleVal["label"]] > 0) {
$canBuy = true;
}
}
}
if($canBuy){
echo 'buy';
}
else {
echo 'sold out';
}
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange