【问题标题】:Magento 2 append custom attribute to product nameMagento 2 将自定义属性附加到产品名称
【发布时间】:2019-10-11 07:22:17
【问题描述】:

我想在 magento 2 商店的产品名称 EVERYWHERE 之后添加 2 个自定义属性。

如“产品名称属性 1 属性 2”

这可能吗?怎么做?我需要修改每个页面还是有办法直接作用于整个系统的产品名称呈现?

谢谢

【问题讨论】:

  • 你能告诉我如何创建自定义属性吗?
  • 是的,有可能
  • 现在检查我的答案,如果你有困难请告诉我。

标签: magento magento2


【解决方案1】:

你必须创建扩展。请检查我的代码

  1. app/code/Magenest中创建文件夹

  2. app/code/Magenest/Sample中创建子文件夹

  3. 现在使用以下代码创建registration.php

    <?php
    
     \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Magenest_Sample',
         __DIR__
    );
    
  4. app/code/Magenest/Sample/中创建etc文件夹

  5. 使用以下代码在etc 文件夹中创建module.xml

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Magenest_Sample" setup_version="2.0.0">    
    </module></config>
    
  6. frontend 中创建文件夹app/code/Magenest/Sample/etc/

  7. 使用以下代码在app/code/Magenest/Sample/etc/frontend 中创建文件di.xml 文件

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Magento\Catalog\Model\Product" type="Magenest\Sample\Model\Product"/>
    </config>
    
  8. 现在在app/Magenest/Sample/ 中创建一个Model 文件夹

  9. 使用以下代码在模型文件夹中创建Product.php

    <?php
    namespace Magenest\Sample\Model;
    class Product extends \Magento\Catalog\Model\Product
    {
    public function getName()
    {
    
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $product = $objectManager->create('Magento\Catalog\Model\Product')->load($this->_getData('entity_id'));
     $myattribute = $product->getResource()->getAttribute('putyourcustomattribute')->getFrontend()->getValue($product);
    
     $changeNamebyPreference = $this->_getData('name') . ' '. $myattribute;
     return $changeNamebyPreference;
     }
     }
    

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-04-10
    • 1970-01-01
    • 1970-01-01
    • 2016-07-16
    • 2014-10-23
    • 2014-02-10
    • 1970-01-01
    相关资源
    最近更新 更多