【问题标题】:Magento 2 - How to create an attribute set based on defaultMagento 2 - 如何基于默认创建属性集
【发布时间】:2016-04-21 00:28:44
【问题描述】:

我目前有以下创建新属性集的代码:

use Magento\Framework\ObjectManagerInterface;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleDataSetupInterface;

class AttributeModel extends \Magento\Framework\Model\AbstractModel
{

    const ENTITY_TYPE = \Magento\Catalog\Model\Product::ENTITY;

    protected $_objectManager;
    protected $_moduleDataSetup;
    protected $_eavSetupFactory;

    public function __construct(
        ObjectManagerInterface $objectManager,
        ModuleDataSetupInterface $moduleDataSetup,
        EavSetupFactory $eavSetupFactory,
    ) {

        $this->_objectManager = $objectManager;
        $this->_moduleDataSetup = $moduleDataSetup;
        $this->_eavSetupFactory = $eavSetupFactory;

    }

    public function createSet($name)
    {

        $eavSetup = $this->_eavSetupFactory->create([
            'setup' => $this->_moduleDataSetup
        ]);

        $eavSetup->addAttributeSet(self::ENTITY_TYPE, $name, 0);

    }

}

但是该集合没有分配给它的属性。如何根据默认设置创建集合,以便预先填充基本属性?

非常感谢任何帮助。

【问题讨论】:

    标签: php magento2 magento-2.0


    【解决方案1】:

    原来 AttributeSetManagementInterface 模型有一个 create 函数,该函数接受一个可选的骨架 ID,新集合可以基于该骨架 ID。我最终使用 objectManager 进行快速修复,我确信有更好的方法。

    下面扩展了上面的 OP 代码:

    public function createSet($name)
    {
    
        $eavSetup = $this->_eavSetupFactory->create([
            'setup' => $this->_moduleDataSetup
        ]);
    
        $defaultId = $eavSetup->getDefaultAttributeSetId(self::ENTITY_TYPE);
    
        $model = $this->_objectManager
        ->create('Magento\Eav\Api\Data\AttributeSetInterface')
        ->setId(null)
        ->setEntityTypeId(4)
        ->setAttributeSetName($name);
    
        $this->_objectManager
        ->create('Magento\Eav\Api\AttributeSetManagementInterface')
        ->create(self::ENTITY_TYPE, $model, $defaultId)
        ->save();
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-14
      • 1970-01-01
      相关资源
      最近更新 更多