Show custom text field on category list page

喜夏-厌秋 提交于 2019-12-11 19:35:57

问题


I've added a custom module for adding an extra description to category pages. It's showing in Admin but I can't get it to show on category frontend pages. I'm probably not understanding the childtheme inheritance structure.

I've read and tried every article here but none of them provide the exact information I need.

  • Luma Childtheme is active: app>design>frontend>MyCompany>Luma_child
  • Custom module: app>code>MyCompany>CategoryAttribute

I've tried adding app/code/MyCompany/CategoryAttribute/view/frontend/templates/myCustomFile.phtml and CategoryAttribute/view/frontend/layout/catalog_category_view.xml and also something similar in app/design/frontend/MyCompany/Luma_child

Expected to see my custom category text on frontpage but it's not. No errors showing.


回答1:


After a lot of research I was able to solve the problem myself. So for future reference for myself or to help anyone facing the same issue here it is:

Build a module to add a custom field to categories using this guide.

Then, to show the value in frontend on the category list page, follow these steps:

1) In your module 'view' folder (app/code/YourName/YourModule/view) create a folder named 'frontend'

2) In this folder we need two more folders: 'layout' and 'templates'

3) In 'layout' create a file called 'catalog_category_view.xml' and add the following code:

<?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) In 'templates' create a file called 'products.phtml' and add code:

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

Make sure to apply all changes through SSH. If it's not showing try clearing your browser cache.



来源:https://stackoverflow.com/questions/58119500/show-custom-text-field-on-category-list-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!