【问题标题】:Magento 2 Load specific attribute on front endMagento 2 在前端加载特定属性
【发布时间】:2016-03-22 11:34:02
【问题描述】:

我正在尝试修改产品页面。我知道我需要更改哪些文件,但是我似乎无法弄清楚我需要做什么具体的事情。

如何加载特定属性以显示在产品页面上?我希望某个特定的标签出现在比“更多信息”标签更显眼的位置。

我可以整理样式和布局的东西,我只需要知道如何抓取一个特定的属性。

附加问题:我已将一个设置为“图像样本”。有没有办法让它在前端显示图像(当它是一个产品选项时,它似乎只显示图像)。

【问题讨论】:

    标签: magento attributes magento2


    【解决方案1】:

    最佳方式 - 创建区块 您可以编辑相关的 XML 文件以创建一个块并将其放置在您需要的位置。如果需要,可以添加容器(具有特定类的 div);

    <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.sku" template="product/view/attribute.phtml" after="product.info.type">
        <arguments>
            <argument name="at_call" xsi:type="string">getSku</argument>
            <argument name="at_code" xsi:type="string">sku</argument>
            <argument name="css_class" xsi:type="string">sku</argument>
        </arguments>
    </block>
    

    在 phtml 模板中 如果您在 phtml 模板中工作,您可以执行以下操作;

    <?php $_product = $block->getProduct(); ?>
    <?php echo $_product->getAttributeText('size');?>
    

    参考 http://devdocs.magento.com/guides/v2.0/frontend-dev-guide/layouts/xml-manage.html#xml-manage-block

    如果有人有更好的方法,那会很有趣。

    【讨论】:

    • 为什么是$_product-&gt;getAttributeText('size')
    • 如果属性是下拉框值,我应该如何写我的论点?此块方法适用于文本输入属性,但它仅在前端显示下拉属性的数字。
    • 要显示下拉选项值而不是 id,您可以将 &lt;argument name="at_type" xsi:type="string"&gt;text&lt;/argument&gt; 添加到参数中。
    【解决方案2】:

    您也可以通过以下方式在自定义模板中加载属性:

     <?php /* @escapeNotVerified */ echo $product->getResource()->getAttribute('my_attribute_id')->getFrontend()->getValue($product); ?>
    

    【讨论】:

    • 你不应该这样编码,那很脏。始终将此类代码移动到块类中
    猜你喜欢
    • 1970-01-01
    • 2017-12-02
    • 1970-01-01
    • 2015-06-25
    • 2017-06-04
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 2019-01-14
    相关资源
    最近更新 更多