cancel
Showing results for 
Search instead for 
Did you mean: 

Custom Message in Product Page with Qty 0

Custom Message in Product Page with Qty 0

Hi,

I would like to print a message on the product page, when the quantity is 0 or less (with active backorders).

 

I have this code:

<?php if($_product->isSaleable()): ?>
<?php echo("CUSTOM MESSAGE")?>
<?php endif; ?>

I would like to recall the message with this code for example

<?php if($_product->getQty() = 0 ): ?>
<?php echo("CUSTOM MESSAGE")?>
<?php endif; ?>

but my code doesn’t work clearly.

 

Can you help me?

2 REPLIES 2

Re: Custom Message in Product Page with Qty 0

Hello,

Please try this in your model class! 

It will work, as what you wantSmiley Happy

Thanks

 

<?php
/**
 * Copyright ©  All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Demo\Stock\Model;
use Demo\Stock\Helper\Data as Helperdata;

class StockManagement implements \Demo\Stock\Api\StockManagementInterface
{
    protected $helperdata;

    public function __construct(
        Helperdata $helperdata
    ) {
        $this->Helperdata = $helperdata;

    }

    /**
     * {@inheritdoc}
     */
    public function getStock($key)
    {

        if($key   ==  "Ideas")
        {
            try {
                $result     =       $this->Helperdata->getProductCollection();

                if (empty($result)) {
                    $message = "Data Not Found";
                }else{
                    $message = "Data Found";
                }
                return $finalResult	=	array(array('status'=>true,'message'=>$message,'result'=>$result));
            }catch (\Exception $e){

                $result = "";
                return $finalResult	=	array(array('status'=>false,'message'=>$e,'result'=>$result));
            }
        }else{
            $message    =   "Please enter valid Secret Key";
            $result     =   "";
            return $finalResult	=	array(array('status'=>false,'message'=>$message,'result'=>$result));
        }

    }
}

Re: Custom Message in Product Page with Qty 0

Thank you so much for your answer, but I can’t understand this code.

 

I am working with the .phtml files:

app/design/frontend/VENDOR/THEME/Magento_Catalog/templates/product/view

 

below the full code of my file:

<?php $_product = $block->getProduct() ?>

 <?php if($_product->isSaleable()): ?>
<?php echo("")?>

<?php else: ?>
<?php echo("WARNING MESSAGE")?>

<?php endif; ?>

the code works, but I would recall the code only if the product quantity is 0, even if the stock status is "In Stock".

 

I think it would suffice to replace this:

<?php if($_product->isSaleable()): ?>

 

with something like this:

<?php if($_product->Qty() ==0 ): ?>


Thanks in advance for the help.