Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Magento 2将自定义属性附加到产品名称_Magento_Magento2 - Fatal编程技术网

Magento 2将自定义属性附加到产品名称

Magento 2将自定义属性附加到产品名称,magento,magento2,Magento,Magento2,我想在magento 2商店的所有位置的产品名称后添加2个自定义属性 如“Productname Attribute1 Attribute2” 这可能吗?如何实现?我是否需要修改每个页面,或者是否有方法直接对整个系统的产品名称呈现进行操作 感谢您创建扩展。请检查我的密码 在app/code/Magenest 在app/code/Magenest/Sample 现在用以下代码创建registration.php <?php \Magento\Framework\Component\C

我想在magento 2商店的所有位置的产品名称后添加2个自定义属性

如“Productname Attribute1 Attribute2”

这可能吗?如何实现?我是否需要修改每个页面,或者是否有方法直接对整个系统的产品名称呈现进行操作


感谢您创建扩展。请检查我的密码

  • app/code/Magenest

  • app/code/Magenest/Sample

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

    <?php
    
     \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Magenest_Sample',
         __DIR__
    );
    
    <?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>
    
    <?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>
    
    <?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;
     }
     }
    
  • app/code/Magenest/Sample/etc/

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

    <?php
    
     \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Magenest_Sample',
         __DIR__
    );
    
    <?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>
    
    <?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>
    
    <?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;
     }
     }
    

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