0

I got the following code to check the status of a configurable product. Basically, the stock status of all child products has to be checked, and as all child products are out of stock, the command would display 'OutOfStock', but the commands not still achieving what I want, since it displays InStock when all child products are out of stock. What could be going wrong?

 <?php
 $_product = Mage::registry('current_product');
 $stockItem = $_product->getStockItem();
 $isInStock = $stockItem->getIsInStock();
 $stockString = ($isInStock ? "In" : "OutOf")
 ;?>

 <script type="application/ld+json">
 {    
 "offers": {
         "availability": "<?php echo $stockString ."Stock"; ?>",
     }
 }
 </script>

`

3
  • 1
    A configurable product does not have any stock. It's the simple products inherited that are relevant when it comes to stock so you will need to create a getUsedProductCollection() and loop through it to get what ever information you're interested in. The basic concept behind this idea can be seen here (even though here it was about getting the data from the first item only which however you should be able to bypass by not applying ->setPageSize(1)->getFirstItem()): magento.stackexchange.com/questions/18055/… Jan 27, 2018 at 16:13
  • Hi Christoph. The main idea is to check if any of the child products are in stock, so if all child products are out of stock, the command would display 'OutOfStock' (I have already retyped my question beter); so I know that the command above has something wrong, and I would really wish someone would help with the best syntax for achieving what I want. Jan 29, 2018 at 18:47
  • did you found the solution
    – Jaisa
    Jun 13, 2018 at 8:23

1 Answer 1

0

The solution was as follows:

$_product = Mage::registry('current_product');
$isInStock = $_product->isSaleable();
$stockString = ($isInStock ? "In" : "OutOf");

And then the snippet would look like:

"availability": "<?php echo "https://schema.org/". $stockString ."Stock"; ?>",

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.