doujiao0110 2016-12-07 13:40
浏览 36
已采纳

Magento 2 phtml文件中的条件语句

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

  • 写回答

1条回答 默认 最新

  • dongquexi1990 2017-07-06 08:12
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计