系统学习magento二次开发,推荐小册:《Magento中文全栈二次开发 》
本小册面向Magento2以上版本,书代码及示例兼容magento2.0-2.4版本。涵盖了magento前端开发,后端开发,magento2主题,magento2重写,magento2 layout,magento2控制器,magento2 block等相关内容,带领您成为magento开发技术专家。
步骤在Magento 2的产品列表页面上添加额外的类别描述:
步骤1: 首先我们需要 在安装程序文件夹中创建描述属性.php文件
app\code\Vendor\Extension\Setup\Patch\Data
现在添加代码,如下所示
<?php namespace Vendor\Extension\Setup\Patch\Data; use Magento\Eav\Setup\EavSetupFactory; use Magento\Framework\Setup\ModuleDataSetupInterface; use Magento\Framework\Setup\Patch\DataPatchInterface; use Magento\Framework\Setup\Patch\PatchRevertableInterface; use Magento\Store\Model\Store; class DescriptionAttribute implements DataPatchInterface { private $eavSetupFactory; /** * Constructor * * @param \Magento\Eav\Setup\EavSetupFactory $eavSetupFactory */ public function __construct(EavSetupFactory $eavSetupFactory, ModuleDataSetupInterface $moduleDataSetup) { $this->eavSetupFactory = $eavSetupFactory; $this->setup = $moduleDataSetup; } /** * {@inheritdoc} */ public function apply() { $eavSetup = $this->eavSetupFactory->create(['setup' => $this->setup]); $eavSetup->addAttribute( \Magento\Catalog\Model\Category::ENTITY, 'extra_description', [ 'type' => 'text', 'label' => 'Extra Description', 'input' => 'textarea', 'required' => false, 'sort_order' => 8, 'global' => ScopedAttributeInterface::SCOPE_STORE, 'wysiwyg_enabled' => true, 'is_html_allowed_on_front' => true, 'group' => 'General Information', ] ); } public static function getDependencies() { return []; } /** * {@inheritdoc} */ public function getAliases() { return []; } /** * {@inheritdoc} */ public static function getVersion() { return '1.0.0'; } }
步骤2:下一步,我们需要 在view文件夹中创建category_form.xml文件
app\code\Vendor\Extension\view\adminhtml\ui_component
并添加以下代码片段
<?xml version="1.0" ?> <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd"> <fieldset name="content"> <field name="extra_description" template="ui/form/field" sortOrder="50" formElement="wysiwyg"> <argument name="data" xsi:type="array"> <item name="config" xsi:type="array"> <item name="wysiwygConfigData" xsi:type="array"> <item name="height" xsi:type="string">100px</item> <item name="add_variables" xsi:type="boolean">false</item> <item name="add_widgets" xsi:type="boolean">false</item> <item name="add_images" xsi:type="boolean">true</item> <item name="add_directives" xsi:type="boolean">true</item> </item> <item name="source" xsi:type="string">category</item> </item> </argument> <settings> <label translate="true">Extra Description</label> <dataScope>extra_description</dataScope> </settings> <formElements> <wysiwyg class="Magento\Catalog\Ui\Component\Category\Form\Element\Wysiwyg"> <settings> <rows>8</rows> <wysiwyg>true</wysiwyg> </settings> </wysiwyg> </formElements> </field> </fieldset> </form>
步骤3:之后,我们需要 在以下路径中创建extra_description.phtml文件
app\code\Vendor\Extension\view\frontend\templates\product\list
然后添加下面提到的代码
<?php if ($_bottomDescription = $block->getCurrentCategory()->getExtraDescription()): ?> <div class="category-extra-description"> <?= /* @escapeNotVerified */ $this->helper('Magento\Catalog\Helper\Output')->categoryAttribute($block->getCurrentCategory(), $_bottomDescription, 'extra_description') ?> </div> <?php endif; ?>
步骤4:在最后一步中,我们需要在 以下路径创建catalog_category_view.xml
app\code\Vendor\Extension\view\frontend\layout
现在添加代码,如下所示
<?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\Catalog\Block\Category\View" name="extra.description" template="Vendor_Extension::product/list/extra_description.phtml" after="-"/> </referenceContainer> </body> </page>
结论:
现在,您可以在Magento 2的产品列表页面上轻松添加有关该类别的更多信息。