0

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

0

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';
}
  • thanks for replay.. but now it's showing only sold out – Rado Oct 4 '15 at 19:05
  • can you post your list.phtml file code here? – Akhilesh Patel Oct 4 '15 at 19:07
  • here's it is pastebin.com/JeRkwLs5 – Rado Oct 4 '15 at 19:09
  • the part I'm working on starts on line 386 – Rado Oct 4 '15 at 19:10
  • I think this is your original code. how did you tried with my trick? Can you post that? – Akhilesh Patel Oct 4 '15 at 19:15

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.