Magento 2 - отображение пользовательских атрибутов продукта на вкладке «Сведения о продукте» - PullRequest
0 голосов
/ 02 сентября 2018

Я создал несколько пользовательских атрибутов продукта, и мне нужно, чтобы они отображались на вкладке «Сведения о продукте», если данные были введены в 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

Любые предложения приветствуются.

Кев

1 Ответ

0 голосов
/ 02 сентября 2018

Решил проблему в итоге. Я пропустил закрывающую скобку.

Эта строка:

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

Должно было быть:

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

Я знал, что это было бы что-то простое. Теперь отлично работает.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...