【问题标题】:How can I show custom attribute value as menu item label in magento 2?如何在 magento 2 中将自定义属性值显示为菜单项标签?
【发布时间】:2022-11-12 07:29:32
【问题描述】:

我在我的 magento 2 设置中为类别添加/编辑页面上的菜单标题创建了一个自定义属性。我想显示保存在数据库中的属性值,而不是主菜单上的类别标题。我怎样才能做到这一点。请帮忙

我在网上搜索,但没有找到解决我的问题的任何确切方法。

【问题讨论】:

    标签: magento magento2


    【解决方案1】:

    您需要在前端主题和模块中编写代码,以在类别标题的位置上显示您的显示自定义属性。

    自定义/模块/视图/前端/布局/catalog_category_view.xml

    <?xml version="1.0"?>
    <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
        <body>
            <referenceContainer name="category.view.container">
                <block class="MagentoCatalogBlockCategoryView" name="category.heading" template="Example_CategoryHeading::category/heading.phtml" before="category.description"/>
            </referenceContainer>
            <referenceBlock name="page.main.title" remove="true"/>
        </body>
    </page>
    

    打开模板路径提示并找到 phtml 文件,您可以在其中找到要在菜单标题中显示的文件位置属性。请记住覆盖您的自定义主题或模块。

    对于模板路径提示:

    1. 在管理侧边栏上,转到商店 > 设置 > 配置.
    2. 在左侧面板中,展开高级并选择开发人员。
    3. 在调试部分展开扩展选择器并执行以下操作:

      获取类别属性:

      $categoryId = 3;
      $_objectManager = MagentoFrameworkAppObjectManager::getInstance();
      $category = $_objectManager->create('MagentoCatalogModelCategory')->load($categoryId);
      echo $category->getName();
      echo $category->getData('<attribute_code>');
      

      请记住,不推荐使用对象管理器。

      在app/design/frontend/theme vendor/themename/Magento_Catalog/layout/文件夹中,文件catalog_category_view.xml负责显示标题。在这个xml文件中,使用了setPageTitle动作方法,您可以根据自己的需要覆盖类别默认标题。

    【讨论】:

      猜你喜欢
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 2022-01-14
      • 1970-01-01
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      相关资源
      最近更新 更多