Magento 2 phtml 文件中的条件语句
Conditional statement in Magento 2 phtml file
我想使用条件语句在前端的管理中显示我创建的 2 个自定义产品属性。
第一个是Availability
,第二个是shipping_rate
。
我已经编辑了位于:
的 defauproduct 页面模板
/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml
对此:
<?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; ?>
我的问题是:
当可用性等于 0 时,我想显示一些东西,当它不同于 0 时,我想显示其他东西。这是一个经典的条件语句,但我没有成功 :S
这是我所做的,但没有奏效。
<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; ?>
有人可以帮我写这个条件语句吗?
谢谢
回答我自己的问题...
您有一个名为 $product
的 M2 全局变量。
然后要访问您的产品属性,比方说可用性,您应该像这样访问它:
$product->[attribute-slug]
所以就我而言,这是 $product->availability
。
然后我在我的模板(默认亮度)上使用,在产品页面描述模板上,有一些条件:
<?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 }?>
希望这对某人有所帮助。
我想使用条件语句在前端的管理中显示我创建的 2 个自定义产品属性。
第一个是Availability
,第二个是shipping_rate
。
我已经编辑了位于:
的 defauproduct 页面模板/vendor/magento/module-catalog/view/frontend/templates/product/view/type/default.phtml
对此:
<?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; ?>
我的问题是: 当可用性等于 0 时,我想显示一些东西,当它不同于 0 时,我想显示其他东西。这是一个经典的条件语句,但我没有成功 :S
这是我所做的,但没有奏效。
<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; ?>
有人可以帮我写这个条件语句吗?
谢谢
回答我自己的问题...
您有一个名为 $product
的 M2 全局变量。
然后要访问您的产品属性,比方说可用性,您应该像这样访问它:
$product->[attribute-slug]
所以就我而言,这是 $product->availability
。
然后我在我的模板(默认亮度)上使用,在产品页面描述模板上,有一些条件:
<?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 }?>
希望这对某人有所帮助。