0

I am running an if statement on an the contents of an attribute, and if it is true I want specific text to be displayed. However, the if does not appear to work. I've tried just plain html text in the file and this shows - so I know it is something to do with the if statement.

IF

<?php if($_product->getAttributeText('in_stock') == $this->__('This item is in stock')): ?>
<div class="product-img-icon-quick">IN STOCK</div>
<?php endif; ?>

Can anyone please suggest an amend to get it working?

3

Add below code in your list.phtml file

<?php if ($_product->isAvailable()): ?>
        <p class="availability in-stock"><span><?php echo $this->__('In stock') ?></span></p>
<?php else: ?>
       <p class="availability out-of-stock"><span><?php echo $this->__('Out of stock') ?></span></p>
<?php endif; ?>
  • this is the right answer if you want to check if a product can be sold – Fabian Schmengler Dec 22 '16 at 10:03
3

Others have already given you answers on how to check properly if a product is in stock.

Generally spoken, never use getAttributeText() to compare values, always check the internally stored value (i.e. use getData() instead of getAttributeText()).

This is for example 0 or 1 for boolean attributes. For attributes that you created in the backend with custom values, you have to look up the value in the database.

Problems with using getAttributeText():

  • the text can be different for each store view
  • the text can be changed for the same value
  • $this->__() does not translate product attributes to store specific values
0

use below code..

<?php if($_product->getInStock()): ?>
<div class="product-img-icon-quick">IN STOCK</div>
<?php endif; ?>
0

This is boolean Attribute you can use like below, By default stock status attribute code is is_in_stock

<?php if($_product->getIsInStock): ?>
 <div class="product-img-icon-quick">IN STOCK</div>
<?php endif; ?>

Or You can check like below also I dont Know about below code But should work

<?php if($_product->getAttributeText('is_in_stock') =='Yes' ): ?>
 <div class="product-img-icon-quick">IN STOCK</div>
<?php endif; ?>

If its your custom Attribute then check like below

 <?php if($_product->getAttributeText('in_stock') == 'This item is in stock'): ?>
    <div class="product-img-icon-quick">IN STOCK</div>
<?php endif; ?>
0

This is a late answer, but the reason it doesn't work is because you need to make sure that the attribute itself has the Used in Product Listing option set to Yes. Without this, the product listing page doesn't read the attribute.

0

Hi we're trying to check the value of multiple attributes before showing some text... I have made sure that each attribute is set with the option "Visible on Product View Page on Front-end" set to YES.

I also made sure my test product has all the following attributes with the following values set

  • damage_front = FRONT: No damage
  • damage_right = RIGHT SIDE: No damage
  • damage_left = LEFT SIDE: No damage
  • damage_back = BACK: No damage
  • damage_handles = HANDLES: No damage

I then added the below code to my product view page...

<?php if ($_product->getAttributeText('damage_front') == "FRONT: No damage" && $_product->getAttributeText('damage_back') == "Back: No damage" && $_product->getAttributeText('damage_right') == "RIGHT SIDE: No damage" && $_product->getAttributeText('damage_left') == "LEFT SIDE: No damage" && $_product->getAttributeText('damage_top') == "TOP: No damage" && $_product->getAttributeText('damage_handles') == "HANDLES: No damage"): ?>
{
echo "<div style ='color:#ffffff'><b>This Product is Box Damaged Only</b></div>";
}
<?php endif; ?>

Unfortunatly this does not show any text output, so just to test further i reduced the statement to just check one attribute...

<?php if ($_product->getAttributeText('damage_front') == "FRONT: No damage") : ?>
{
<?php echo "<div style ='color:#ffffff'><b>This Product is Box Damaged Only</b></div>"; ?>
}

<?php endif; ?>

This still doesnt show the required Box Damaged only text.

Finally just to test, i removed the whole IF statment and added

<?php echo "<div style ='color:#ffffff'><b>This Product is Box Damaged Only</b></div>"; ?>

As expected this does show the text in the correct position on the product view page, can anyone please show me where i'm going wrong to check all the attribute values and show text if they match?

edit: since checking another similar post i have also tried the following...

<?php if ($_product->getData('damage_front') == "FRONT: No damage" && $_product->getData('damage_back') == "Back: No damage" && $_product->getData('damage_right') == "RIGHT SIDE: No damage" && $_product->getData('damage_left') == "LEFT SIDE: No damage" && $_product->getData('damage_top') == "TOP: No damage" && $_product->getData('damage_handles') == "HANDLES: No damage"): ?>

The suggested answers here do not rectify this issue.

EDIT: Ok I've managed to fix my own problem and here's how i did it...

    <?php
if ($_product->getData('damage_front') == "FRONT: No damage" && ($_product->getData('damage_back') == "BACK: No damage" && ($_product->getData('damage_right') == "RIGHT SIDE: No damage" && ($_product->getData('damage_left') == "LEFT SIDE: No damage" && ($_product->getData('damage_top') == "TOP: No damage" && ($_product->getData('damage_handles') == "HANDLES: No damage")))))):
 ?>
                          <h3>Box Damaged Only.</h3>
                          <p>
                          This item is in perfect condition and only has box damage.
                          </p>
                      </div>
                  </div>
<?php endif;?>

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.