PHP Else If Statement

Hi,

This is my first foray into PHP language and I need a little help here.

  1. Please reference to this site -> https://www.clothingrepublic.com/index.php/shop/ladieswear/tops-tees/nature-club.html#

Basically if a product quantity is 1, it will display the following message:
Only 1 left
NATURE CLUB-Pink-XS
S$12.00

  1. What I wanted to achieve is, when the quantity reaches “0”. It will display another message like “Out Of Stock”.

I believe these are the codes that trigger the events

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Academic Free License (AFL 3.0)
 * that is bundled with this package in the file LICENSE_AFL.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/afl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    design
 * @package     base_default
 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
 */

/**
 * @see Mage_CatalogInventory_Block_Stockqty_Composite
 */
?>

    <table align="right" id="" class="stock-qty-details">
        <thead>
            <tr>
              <?php if ($this->isMsgVisible()): ?>
    <a href="#" id="<?php echo $this->getPlaceholderId() ?>" class="stock-qty">
        <?php echo $this->__('Only %s left', $this->getStockQty()) ?>
    </a>
            </tr>
        </thead>
        <tbody>
        <?php foreach ($this->getChildProducts() as $childProduct) : ?>
            <?php $childProductStockQty = $this->getProductStockQty($childProduct); ?>
            <?php if ($childProductStockQty > 0) : ?>
                <tr>
                    <td><?php echo $childProduct->getName() ?></td>
                </tr>
            <?php endif ?>
        <?php endforeach ?>
       </tbody>
    </table>
    <!--script type="text/javascript">
    //<![CDATA[
    $('<?php //echo $this->getPlaceholderId() ?>').observe('click', function(event){
        $('<?php //echo $this->getDetailsPlaceholderId() ?>').toggleClassName('no-display');
        event.stop();
    });
    //]]>
    </script-->
<?php endif; ?>

Please do take note that I am absolutely new to this, any help rendered is greatly appreciated.

Best Regards,
Chris.

you’ll need to find the function that defines
$this->isMsgVisible()
(whatever $this is. It’s an object of SOME sort)

Hi!

Thanks for the reply! Does this help in any ways?

Regards,
Chris.

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magentocommerce.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_CatalogInventory
 * @copyright   Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */


/**
 * Product stock qty abstarct block
 *
 * @category   Mage
 * @package    Mage_CatalogInventory
 * @author      Magento Core Team <core@magentocommerce.com>
 */
abstract class Mage_CatalogInventory_Block_Stockqty_Abstract extends Mage_Core_Block_Template
{
    const XML_PATH_STOCK_THRESHOLD_QTY = 'cataloginventory/options/stock_threshold_qty';

    /**
     * Retrieve current product object
     *
     * @return Mage_Catalog_Model_Product
     */
    protected function _getProduct()
    {
        return Mage::registry('current_product');
    }

    /**
     * Retrieve current product stock qty
     *
     * @return float
     */
    public function getStockQty()
    {
        if (!$this->hasData('product_stock_qty')) {
            $qty = 0;
            if ($stockItem = $this->_getProduct()->getStockItem()) {
                $qty = (float) $stockItem->getStockQty();
            }
            $this->setData('product_stock_qty', $qty);
        }
        return $this->getData('product_stock_qty');
    }

    /**
     * Retrieve threshold of qty to display stock qty message
     *
     * @return string
     */
    public function getThresholdQty()
    {
        if (!$this->hasData('threshold_qty')) {
            $qty = (float) Mage::getStoreConfig(self::XML_PATH_STOCK_THRESHOLD_QTY);
            $this->setData('threshold_qty', $qty);
        }
        return $this->getData('threshold_qty');
    }

    /**
     * Retrieve id of message placeholder in template
     *
     * @return string
     */
    public function getPlaceholderId()
    {
        return 'stock-qty-' . $this->_getProduct()->getId();
    }

    /**
     * Retrieve visibility of stock qty message
     *
     * @return bool
     */
    public function isMsgVisible()
    {
        return ($this->getStockQty() > 0 && $this->getStockQty() <= $this->getThresholdQty());
    }

}

Hmm. It does, sort of. The code is very oddly designed, with the table,thed,tr tags outside the if but everything else inside it…

in any case, i’d just add the following below the endif


<?php if ($this->getStockQty() === 0): ?>
        <?php echo "Sold Out" ?>
            </tr>
        </thead>
        <tbody></tbody></table>
<?php endif; ?>

Hi,

Thanks for the help so far. I don’t seem able to get this piece of code to work though. Am I doing something wrong over here? Where do I insert this piece of code?

Regards,
Chris.

‘below the endif’… was that in some way difficult? It should go after the piece of code you posted in your OP.

Hi,

I have already done so, but was unable to get what I wanted?

Regards,
Chris.

Try with just == (two equals) in the if() statement.

The method getStockQty() returns a float. The if() is checking for an (int) of 0. You can use == or replace the 0 with 0.0 in the if() statement.

Use this:


<?php if ($this->getStockQty() == 0): ?>
        <?php echo "Sold Out" ?>
            </tr>
        </thead>
        <tbody></tbody></table>
<?php endif; ?>

or use the below which is more accurate:


<?php if ($this->getStockQty() === 0.0): ?>
        <?php echo "Sold Out" ?>
            </tr>
        </thead>
        <tbody></tbody></table>
<?php endif; ?>

It may not seem like it matters, but it does.

Hi,

Thanks for the reply. I am unable to get those to work too though :frowning: The website is running on Magento platform, could it be any of the reasons on not working?

Regards,
Chris.

Have you echoed the value of $this->getStockQtyCOLOR=#007700[/COLOR] to check that it’s returning the a number and not something else?

Hi, yes it is returning a number.

Chris.

Is the number returned by $this->getStockQty() a decimal?

http://www.php.net/manual/en/language.types.float.php

It looks like PHP has trouble comparing an integer with a floater

I’m just curious why nobody has offered the use of the inline version of if-else?

$result = $condition ? $valueIfConditionEvaluatesTrue : $valueIfConditionEvaluatesFalse;


// change this...
echo $this->__('Only %s left', $this->getStockQty()

// to this...
echo $this->getStockQty() == 0 ? 'Out of stock!' : $this->__('Only %s left', $this->getStockQty())

This basically says if quantity is 0, echo ‘out of stock’, otherwise echo out original line.