0

I want to check that a product attribute equals a specified value in the product/view/list.phtml template and if so, display some html.

The current code gives no result. Can an edit be suggested to get it working in Magento 2.

CURRENT CODE

<?php if ($_helper->productAttribute($_product, $_product->getInStockMadeToOrder(), 'in_stock_made_to_order') == "This item is in stock") { ?>
  <div class="listing-labels in-stock"><p>In Stock</p></div>
<?php } ?>
  • which value return for $_helper->productAttribute($_product, $_product->getInStockMadeToOrder(), 'in_stock_made_to_order')? – Rakesh Jesadiya Mar 29 '17 at 11:08
  • It would either be 'This item is in stock' or 'This item is made to order' – YorkieMagento Mar 29 '17 at 11:36
  • try with === , if ($_helper->productAttribute($_product, $_product->getInStockMadeToOrder(), 'in_stock_made_to_order') === "This item is in stock") – Rakesh Jesadiya Mar 29 '17 at 11:39
  • Try $this->getProduct()->getAttributeText('custom_attribute'); – Mrunmay Deswandikar Mar 29 '17 at 11:39
  • Neither options are working sadly. – YorkieMagento Mar 29 '17 at 12:19
0

you can add magento default functionality for checking product in-stock or out-of-stock, please check below function

<?php if ($_product->getIsSalable()): ?>
     <div class="stock available"><span><?php /* @escapeNotVerified */ echo __('In stock') ?></span></div>
<?php else: ?>
     <div class="stock unavailable"><span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span></div>
<?php endif; ?>
  • Hi Yogesh, this is a custom attribute I am trying to find the value of. The default stock functionality is not what I am checking here. – YorkieMagento Mar 29 '17 at 11:37
  • Did you enabled from admin panel Used in Product Listing to Yes From Admin Panel => Stores => Products(Attributes) => select attribute – Yogesh Mar 29 '17 at 12:25
  • Hi Yogesh, yes it is set up as Product Listing Yes – YorkieMagento Mar 31 '17 at 12:47

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.