magento2 - 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
任何建议表示赞赏。
凯夫
解决方案
最终解决了这个问题。我错过了一个右括号。
这一行:
if (!empty($dimensions) || !empty($features_benefits) || !empty($flooring_specification) {
本来应该:
if (!empty($dimensions) || !empty($features_benefits) || !empty($flooring_specification)) {
我知道这会很简单。现在完美运行。
推荐阅读
- scala - 在 Scala/Gatling 中提取 json 响应的第 n 个元素
- ios - 如何在 Swift 中为没有 URL 的 UIImage 添加 Kingfisher?
- reactjs - 选择项目进入项目并将它们显示到列表中
- scala - 如何通过排除集成测试来运行 sbt 程序集
- angular - OpenLayers OSM - 跨域问题
- python - 使用 pyinstaller 生成的基于 kivy 的 Windows exe 的黑屏
- javascript - 当我得到这样的数组 [["a","b","c"]] 时,如何从 JS 中的数组中获取值
- python - 气流:动态生成任务时“已注册 DAG”
- web-services - 将 ws-security 添加到 Mule 中的流
- c++ - 当少数测试用例失败时如何生成完整的 GCOV 报告?