3

I am trying to associate a category to a product inside a minion.

I am using next code :

var catalog = catalogRepository.GetCatalogByName("MyCatalog");
var category = catalogRepository.GetCategoryByName("MyCategory");
Random random = new Random(DateTime.Now.Millisecond);

var result = await _createSellableItemPipeline.Run(new CreateSellableItemArgument(random.Next().ToString(), "Product name", "Product display name", "This is a description of my test product"), context);
SellableItem addedItem = null;
if (result != null) // Null here means it failed to add
{
    var catalogL = await _findEntityPipeline.Run(new FindEntityArgument(typeof(Catalog), catalog.Id, false), context) as Catalog;
    var categoryL= await _findEntityPipeline.Run(new FindEntityArgument(typeof(Category), category.Id, false), context) as Category;

    addedItem = result.SellableItems.FirstOrDefault(); // This is the item which has just been added
    addedItem.ParentCatalogList = catalogL.SitecoreId.ToLower();
    addedItem.ParentCategoryList = categoryL.SitecoreId.ToLower();
    addedItem.Brand = "My Brand";
    addedItem.Manufacturer = "My Manufacturer";

    var saveResult = _persistEntityPipeline.Run(new PersistEntityArgument(addedItem), context);
    var catalogResult = await this._associateSellableItemToParentPipeline.Run(new CatalogReferenceArgument(catalog.Id, category.Id, addedItem.Id));
} 

The ittem is created but is not associated to the category. If I associate it manually from the Merchandise dashboard it is working fine. What am I doing wrong?

1 Answer 1

5

You also need to create a relation:

await createRelationshipPipeline.Run(new RelationshipArgument(categoryId, sellableItemId, "CategoryToSellableItem")
{
    Mode = new RelationshipMode?(RelationshipMode.Create)
}, context);

Where createRelationshipPipeline is a ICreateRelationshipPipeline

1
  • Other solution is to use rest api call to commerce engine , but your solution is more elegant. Thanks. Commented Jun 1, 2018 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.