首页 > 解决方案 > Magento 2 - 在产品详细信息选项卡中显示自定义产品属性

问题描述

我已经创建了几个自定义产品属性,如果在 Magento Admin 中输入了数据,我需要它们显示在产品详细信息选项卡上。总共有三个,在很多情况下,这三个都有数据,但是大约 20% 的产品中的一两个有数据。

我已经很容易地创建了属性,但我很难让它们显示在前端。我已经粘贴了布局文件的代码和属性的 phtml 文件。

我得到的结果是一个空白的产品屏幕,根本没有任何信息。绝对是某个地方的错误,但经过数小时的寻找,我错过了它。

下面是自定义catalog_product_view.xml

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<page layout="2columns-right" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <move element="product.info.stock.sku" destination="product.info.price" after="product.price.final"/>
        <move element="page.main.title" destination="product.info.main" before="-"/>
        <referenceBlock name="product.info.overview" remove="true"/>

        <referenceBlock name="product.info.details">

          <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.description." template="Magento_Catalog::product/view/product_attributes.phtml" group="detailed_info">
            <arguments>
              <argument name="at_call" xsi:type="string">getDescription</argument>
              <argument name="at_code" xsi:type="string">description</argument>
              <argument name="css_class" xsi:type="string">description</argument>
              <argument name="at_label" xsi:type="string">none</argument>
              <argument name="title" translate="true" xsi:type="string">Product Details</argument>
            </arguments>
          </block>

          <block class="Magento\Catalog\Block\Product\View" name="deliveryinfo.tab" as="deliveryinfo" template="product/view/delivery_info.phtml" group="detailed_info" >
            <arguments>
              <argument translate="true" name="title" xsi:type="string">Delivery</argument>
            </arguments>
          </block>

        </referenceBlock>

        <referenceContainer name="sidebar.main">
          <block class="Magento\Cms\Block\Block" name="sidebar_delivery" after="-">
            <arguments>
              <argument name="block_id" xsi:type="string">sidebar_delivery</argument>
            </arguments>
          </block>

          <block class="Magento\Cms\Block\Block" name="sidebar_instructions" after="-">
            <arguments>
              <argument name="block_id" xsi:type="string">sidebar_instructions</argument>
            </arguments>
          </block> 

          <block class="Magento\Cms\Block\Block" name="sidebar_brochures" after="-">
            <arguments>
              <argument name="block_id" xsi:type="string">sidebar_brochures</argument>
            </arguments>
          </block>

          <block class="Magento\Cms\Block\Block" name="sidebar_blog" after="-">
            <arguments>
              <argument name="block_id" xsi:type="string">sidebar_blog</argument>
            </arguments>
          </block>
        </referenceContainer> 
    </body>
</page>

下面是布局文件中调用的product_attributes.phtml

<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();
if ($_attributeLabel && $_attributeLabel == 'default') {
$_attributeLabel = $_product->getResource()->getAttribute($_code)->getFrontendLabel();
}
$_attributeValue =$_product->getResource()->getAttribute($_code)->getFrontend()->getValue($_product);
?>

<?php if ($_attributeValue): ?>
<div>
<?php if ($_attributeLabel != 'none'): ?><strong class="type"><?php echo $_attributeLabel?></strong><?php endif; ?>
<div class="value" <?php echo $_attributeAddAttribute;?>><?php echo $_attributeValue; ?></div>
</div>
<?php endif; ?>

<?php

$dimensions = $_product->getResource()->getAttribute('dimensions')->getFrontend()->getValue($_product);
$features_benefits = $_product->getResource()->getAttribute('features_benefits')->getFrontend()->getValue($_product);
$flooring_specification = $_product->getResource()->getAttribute('flooring_specification')->getFrontend()->getValue($_product);

if (!empty($dimensions) || !empty($features_benefits) || !empty($flooring_specification) {
    ?>
        <?php 
        if (!empty($dimensions)) {
            ?>
            <h2>Dimensions</h2>
            <?php echo $dimensions; ?>
            <?php
        }
        if (!empty($features_benefits)) {
            ?>
            <h2>Features & Benefits</h2>
            <?php echo $features_benefits; ?>
            <?php
        }
        if (!empty($flooring_specification)) {
            ?>
            <h2>Specification</h2>
            <?php echo $flooring_specification; ?>
            <?php
        }
        ?>
    <?php
}
?>

这可能是我做错了一件可笑的简单事情,但我一辈子都找不到它。

该站点位于 Community Edition 2.2.5 上,服务器运行 PHP 7.1

任何建议表示赞赏。

凯夫

标签: magento2

解决方案


最终解决了这个问题。我错过了一个右括号。

这一行:

if (!empty($dimensions) || !empty($features_benefits) || !empty($flooring_specification) {

本来应该:

if (!empty($dimensions) || !empty($features_benefits) || !empty($flooring_specification)) {

我知道这会很简单。现在完美运行。


推荐阅读