我正在尝试为我以编程方式创建的产品分配频道,因此它们将显示在商店中。不幸的是,这个用例没有文档。
以下是我创建产品的方法:
$productFactory = $this->container->get('sylius.factory.product');
$productManager = $this->container->get('sylius.manager.product');
$productRepository = $this->get('sylius.repository.product');
$productVariantFactory = $this->get('sylius.factory.product_variant');
$productVariantRepository = $this->get('sylius.repository.product_variant');
$channelPricingFactory = $this->get('sylius.factory.channel_pricing');
$channelPricingRepository = $this->get('sylius.repository.channel_pricing');
//CREATE PRODUCT
$product = $factory->createNew();
$product->setName('TEST 2 - '.$title);
$product->setCode($this->generateRandomString());
$product->setSlug($this->generateRandomString());
$productRepository->add($product);
//CREATE VARIANT & ATTACH IT TO PRODUCT
$variant = $productVariantFactory->createNew();
$variant->setName('TEST 2 - '.$title);
$variant->setCode($this->generateRandomString());
$variant->setProduct($product);
$productVariantRepository->add($variant);
//CREATE PRICE & ATTACH IT TO VARIANT
$channelPricing = $channelPricingFactory->createNew();
$channelPricing->setPrice(999);
$channelPricing->setOriginalPrice(999);
$channelPricing->setChannelCode('US_WEB');
$channelPricing->setProductVariant($variant);
$channelPricingRepository->add($channelPricing);
不幸的是,这些产品没有链接到频道:
答案 0 :(得分:1)
你的问题的答案:
您有一个产品变体,但Product variant实现了ProductVariantInterface,它扩展了TranslatableInterface。所以我们需要一个翻译来使它显示和工作。要添加翻译:
$productVariant->setCurrentLocale('fr_FR');
$productVariantTranslation = $productVariant->getTranslation();
或
$productVariantTranslation = $productVariant->getTranslation($locale);
之后可以添加名称:
$productVariantTranslation->setName('What a product variant!');
之后:
$product->addVariant($productVariant);
$this->entityManager->persist($product);
您将在线获得您的产品变体。
另一件事是:
$channelPricingRepository = $this->get('sylius.repository.channel_pricing');
$channelPricingRepository->add($entity);
它会在每次调用中直接将数据刷新到DB。在示例中,您将刷新3次而不是一次。在更大的过程中有许多“添加”,这可能是缺乏表现。你可以简单地说;
$this->entityManager->persist($entity); //Many times
$this->entityManager->flush();
答案 1 :(得分:0)
以下是我设法创建产品的方法(将它们附加到我的实体):
function createProduct($livre,$options){
$title = $livre['title'];
$prix = $livre['prix'];
// SYLIUS REPOSITORIES LOAD
$productFactory = $options['sylius_factory_product'];
$productRepository = $options['sylius_repository_product'];
$productVariantFactory = $options['sylius_factory_product_variant'];
$productVariantRepository = $options['sylius_repository_product_variant'];
$channelPricingFactory = $options['sylius_factory_channel_pricing'];
$channelPricingRepository = $options['sylius_repository_channel_pricing'];
//CREATE PRODUCT
$product = $productFactory->createNew();
$product->setName($title);
$product->setCode($title.'_'.$this->generateRandomString());
$product->setSlug($title.'_'.$this->generateRandomString());
$productRepository->add($product);
//CREATE VARIANT & ATTACH IT TO PRODUCT
$variant = $productVariantFactory->createNew();
$variant->setName($title. 'Variant');
$variant->setCode($title.'_'.$this->generateRandomString());
$variant->setProduct($product);
$productVariantRepository->add($variant);
//CREATE PRICE & ATTACH IT TO VARIANT
$channelPricing = $channelPricingFactory->createNew();
$channelPricing->setPrice($prix*100);
$channelPricing->setOriginalPrice($prix*100);
$channelPricing->setChannelCode('CH');
$channelPricing->setProductVariant($variant);
$channelPricingRepository->add($channelPricing);
$productId = $product->getId();
return $productId;
}
答案 2 :(得分:0)
我将频道添加到产品中:
$channelCode = 'US_WEB'
$channelRepository = $this->get('sylius.repository.channel');
$channel = $channelRepository->findOneBy(array('code' => $channelCode));
$product->addChannel($channel);