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.

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

报告相同问题?

悬赏问题

  • ¥20 Python安装cvxpy库出问题
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥15 python天天向上类似问题,但没有清零
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 C#调用python代码(python带有库)
  • ¥15 矩阵加法的规则是两个矩阵中对应位置的数的绝对值进行加和
  • ¥15 活动选择题。最多可以参加几个项目?
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题