2016-12-07 102 views
2

我想顯示2個自定義產品屬性,我使用條件語句在前端的管理員中創建。第一個是Availability,第二個是shipping_rateMagento 2 phtml文件中的條件語句

Availability product attribute

Shipping rate product attribute

我已經編輯地處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; ?> 

我的問題是: 當可用性EQUA l到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; ?> 

有人可以幫我寫這個條件語句嗎?

感謝

回答

1

回答我的問題...

您有一個名爲$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 }?> 

希望這可以幫助別人。