2017-04-20 77 views
0

我正嘗試將一個頻道分配給以編程方式創建的產品,因此它們將顯示在商店中。不幸的是,這個用例沒有文檔。Sylius - 如何以編程方式將頻道分配給產品?

這是我如何創建我的產品:

$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); 

不幸的是,該產品未鏈接到一個頻道:

enter image description here

回答

1

的回答你的問題:

你有一個產品變體,但產品變體實現了可擴展TranslatableInterface的ProductVariantInterface。所以我們需要一個翻譯才能讓它出現和工作。要添加翻譯:

$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); 

它會在每個呼叫直接數據刷新到數據庫。在這個例子中,你將沖刷3次而不是隻有一次。在許多「增加」的更大進程中,這可能是缺乏性能。你可以簡單地;

$this->entityManager->persist($entity); //Many times 
$this->entityManager->flush(); 
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; 
} 
0

我說這樣的渠道的產品:

$channelCode = 'US_WEB' 
$channelRepository = $this->get('sylius.repository.channel'); 
$channel = $channelRepository->findOneBy(array('code' => $channelCode)); 
$product->addChannel($channel); 
相關問題