SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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/ind...ure-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


    2. 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 Code:
    <?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.

  2. #2
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    you'll need to find the function that defines
    $this->isMsgVisible()
    (whatever $this is. It's an object of SOME sort)
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  3. #3
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi!

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

    Regards,
    Chris.

    PHP Code:
    <?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() > && $this->getStockQty() <= $this->getThresholdQty());
        }

    }

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?php if ($this->getStockQty() === 0): ?>
            <?php echo "Sold Out" ?>
                </tr>
            </thead>
            <tbody></tbody></table>
    <?php endif; ?>
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  5. #5
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    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 Code:
    <?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.

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    'below the endif'.... was that in some way difficult? It should go after the piece of code you posted in your OP.
    Never grow up. The instant you do, you lose all ability to imagine great things, for fear of reality crashing in.

  7. #7
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by StarLion View Post
    '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.

  8. #8
    SitePoint Member
    Join Date
    May 2012
    Posts
    10
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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 Code:
    <?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 Code:
    <?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.

  9. #9
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ITSE1306 View Post
    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 Code:
    <?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 Code:
    <?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 The website is running on Magento platform, could it be any of the reasons on not working?

    Regards,
    Chris.

  10. #10
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    Have you echoed the value of $this->getStockQty() to check that it's returning the a number and not something else?
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  11. #11
    SitePoint Enthusiast
    Join Date
    Apr 2012
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SpacePhoenix View Post
    Have you echoed the value of $this->getStockQty() to check that it's returning the a number and not something else?
    Hi, yes it is returning a number.

    Chris.

  12. #12
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,072
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    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
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  13. #13
    Resident OCD goofball! bronze trophy Serenarules's Avatar
    Join Date
    Dec 2002
    Posts
    1,911
    Mentioned
    26 Post(s)
    Tagged
    0 Thread(s)
    I'm just curious why nobody has offered the use of the inline version of if-else?

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

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

    // to this...
    echo $this->getStockQty() == '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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •