Akeneo:创建一个包含属性的新变体组

Son*_*123 3 symfony akeneo

我想在Akeneo中创建一个新的变体组,其中包含一些固定属性来"模拟"产品系列的行为.不幸的是,我不知道如何ProductTemplate使用空值来获取某些属性.

当我尝试下面的代码时,在Akeneo GUI中打开创建的变体组后出现错误:

Error: Option "attributes" must only contains instances of "Pim\Bundle\CatalogBundle\Entity\Attribute", got "Pim\Bundle\EnrichBundle\Form\Type\AvailableAttributesType"
Run Code Online (Sandbox Code Playgroud)

我的代码看起来像这样:

    $groupType = $this->groupManager
        ->getGroupTypeRepository()
        ->findOneBy(['code' => 'VARIANT']);
    $group = $this->groupFactory->createGroup($groupType);
    $group->setCode('MY_VARIANT_GROUP');

    $attributes = array($this->attributeRepository->findOneByIdentifier('AXIS_ATTRIBUTE'));
    $group->setAxisAttributes($attributes);

    // ??? How can I create a new product value?
    $productValue1 = new ProductValue();
    $productValue1->setId('PREDEFINED_ATTRIBUTE1');
    $productValue1->setAttribute($this->attributeRepository->findOneByIdentifier('PREDEFINED_ATTRIBUTE1'));

    $productTemplate = new ProductTemplate();
    $productTemplate->setValuesData(array($productValue1));

    $group->setProductTemplate($productTemplate);
    $this->groupSaver->save($group);
Run Code Online (Sandbox Code Playgroud)

小智 5

我建议您使用Pim\Bundle\CatalogBundle\Builder\ProductTemplateBuilder创建和添加属性到您的产品模板.

这将确保使用空产品值正确创建产品模板.