yofisim
yofisim

Reputation: 370

Conditional statement in Magento 2 phtml file

I want to display 2 custom product attributes I created in the admin on the frontend with conditional statement.

The first is Availability, and the second is shipping_rate.

Availability product attribute

Shipping rate product attribute

I have edited the defauproduct page template located in:

/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml

to this:

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

?>
<?php /* @var $block \Magento\Catalog\Block\Product\View\AbstractView */?>
<?php $_product = $block->getProduct() ?>

<?php if ($block->displayProductStockStatus()): ?>
    <?php if ($_product->isAvailable()): ?>
        <div class="stock available" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
            <span><?php /* @escapeNotVerified */ echo __('In stock') ?></span>
        </div>
        <span class="estimated-delivery">    
        <?php /* @escapeNotVerified */ if ($_product->getResource()->getAttribute('availability')->getValue($_product) != '0'): ?>
            Livraison estimée dans <?php /* @escapeNotVerified */ echo $_product->getResource()->getAttribute('availability')->getFrontend()->getValue($_product); ?> jour(s)</span>
        <?php endif; ?>
        <span>Livraison à partir de <?php /* @escapeNotVerified */ echo $_product->getResource()->getAttribute('shipping_rate')->getFrontend()->getValue($_product); ?> ‎€</span>
    <?php else: ?>
        <div class="stock unavailable" title="<?php /* @escapeNotVerified */ echo __('Availability') ?>">
            <span><?php /* @escapeNotVerified */ echo __('Out of stock') ?></span>
        </div>
    <?php endif; ?>
<?php endif; ?>

My problem is: When the availability is equal to 0, I would like to display something, and when it's different than 0, something else. It's a classic conditional statement, but I'm not succeeding :S

Here is what I did, and it didn't work.

<span class="estimated-delivery">    
    <?php /* @escapeNotVerified */ if ($_product->getResource()->getAttribute('availability')->getValue($_product) != '0'): ?>
            Livraison estimée dans <?php /* @escapeNotVerified */ echo $_product->getResource()->getAttribute('availability')->getFrontend()->getValue($_product); ?> jour(s)</span>
        <?php else ?>
             Disponibilité: <strong>En Stock</strong> </span>    
    <?php endif; ?>

Can someone help me to write this conditional statement?

Thanks

Upvotes: 2

Views: 6065

Answers (1)

yofisim
yofisim

Reputation: 370

To answer my own question...

You have a M2 global variable called $product. Then to access your product attributes, let's say availability, you should access it like this:

$product->[attribute-slug]

so in my case, this was $product->availability.

Then I use on my template (default-luma), on the product page description template, with some conditional:

<?php if ($product->availability > 0 ) {?>
  <span>Product will be delivered in <?php echo $product->availability; ?> days.</span>
<?php} else {?>
  <span> Product is in stock </span>
<?php }?>

Hope this helps someone.

Upvotes: 1

Related Questions