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

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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 结构体数组文件读取问题
      • ¥15 写c++代码,第7题说一下使用的计算方法
      • ¥15 postman接口自动化测试报告实践总结
      • ¥15 有关c++的问题,利用相关知识
      • ¥15 求香农编码和解码的matlab代码
      • ¥20 ROS中的TEB局部规划问题
      • ¥20 关于#matlab#的问题:要求测出上面图片中所有同心圆的半径
      • ¥20 epanet软件运行问题
      • ¥15 Python 文件读取
      • ¥60 dpabi进行Alff计算时脑池有干扰信号