【问题标题】:Show custom text field on category list page在类别列表页面上显示自定义文本字段
【发布时间】:2019-09-26 14:52:38
【问题描述】:

我添加了一个自定义模块,用于向类别页面添加额外描述。它显示在管理员中,但我无法让它显示在类别前端页面上。我可能不了解 childtheme 继承结构。

我已阅读并尝试过这里的每篇文章,但没有一篇文章能提供我需要的确切信息。

  • Luma Childtheme 处于活动状态:app>design>frontend>MyCompany>Luma_child
  • 自定义模块:app>code>MyCompany>CategoryAttribute

我尝试添加 app/code/MyCompany/CategoryAttribute/view/frontend/templates/myCustomFile.phtml 和 CategoryAttribute/view/frontend/layout/catalog_category_view.xml 在 app/design/frontend/MyCompany/Luma_child 中也有类似的东西

预计会在首页上看到我的自定义类别文本,但事实并非如此。没有显示错误。

【问题讨论】:

    标签: frontend categories magento2


    【解决方案1】:

    经过大量研究,我能够自己解决问题。因此,为了我自己的未来参考或帮助任何面临同样问题的人,它是:

    使用this guide. 构建一个模块以将自定义字段添加到类别中

    然后,要在类别列表页面的前端显示值,请按照下列步骤操作:

    1) 在您的模块“视图”文件夹(app/code/YourName/YourModule/view)中创建一个名为“前端”的文件夹

    2) 在这个文件夹中,我们还需要两个文件夹:'layout' 和 'templates'

    3) 在“布局”中创建一个名为“catalog_category_view.xml”的文件并添加以下代码:

    <?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceContainer name="content"> <block class="Magento\Framework\View\Element\Template" name="YourModule" template="YourName_YourModule::products.phtml" /> </referenceContainer> </body> </page>

    4) 在“模板”中创建一个名为“products.phtml”的文件并添加代码:

    <?php 
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');//get current category
    echo $category->getCustomCategoryField(); ?>
    

    确保通过 SSH 应用所有更改。如果没有显示,请尝试清除浏览器缓存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-22
      • 2019-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多