2

I am using Sitecore XC9 update-2 and noticed that import commerce items are not published by default. The items are however shown when using the Authoring environment, but not when using the Shops environment.

I found this setting for the Authoring environment:

{
   "$type": "Sitecore.Commerce.Core.IgnorePublishedPolicy, Sitecore.Commerce.Core"
}

Is it since XC9 update-2 necessary to publish commerce items after import? And if so, how to do this programmatically?

3 Answers 3

4

There is a published flag on each catalog entity in XC9-update 2. Try setting that to true before you persist the entity.

private async Task PublishSellableItem(CommercePipelineExecutionContext context, string sellableItemId)
{
    var sellableItem = (await _commerceCommander
        .Command<FindEntityCommand>()
        .Process(context.CommerceContext, typeof(SellableItem), actualSellableItemId, false)) as SellableItem;

    sellableItem.Published = true;

    await _commerceCommander.PersistEntity(context.CommerceContext, sellableItem);
}

(There's no need to have a separate method to do this. It's just for a complete example.)

1
  • just a note, this is not working on 10.1 Commented Apr 29, 2022 at 10:40
3

You can achieve this automatically by using the AutoApprovePolicy. If you add an instance of this to the context environments policy collection, then run your import, then remove it, you will find that the products are all created with their published value set to true.

I created a CSV import command that used it something like this:

public async Task<CommerceCommand> Process(CommerceContext commerceContext, IFormFile importFile, bool publishEntities = true)
{
    using (CommandActivity.Start(commerceContext, this))
    {
        var pipelineContextOptions = commerceContext.GetPipelineContextOptions();
        var catalogsArgument = new ImportCsvProductsArgument(importFile);

        if (publishEntities)
            commerceContext.Environment.SetPolicy(new AutoApprovePolicy());

        //RUN PRODUCT IMPORT

        if (publishEntities)
            commerceContext.Environment.RemovePolicy(typeof(AutoApprovePolicy));
    }
    return this;
}
1
  • This is working as it should in 10.1 Commented Apr 29, 2022 at 10:40
1

In Sitecore commerce 9 update 3 , the provided solutions did not work. Here is the solution which worked for me. This code will not create a new entity and update the current entity version and publish the same version again(even if it was published previously).

 var entity = await _findEntityPipeline.Run(new FindEntityArgument(typeof(SellableItem), item.Id), context);
     var sellableEntity = entity as SellableItem;
     WorkflowComponent workflowComponentToUpdate = sellableEntity.GetComponent<WorkflowComponent>();
     workflowComponentToUpdate.CurrentState = "Approved";
     var editResults = await _editSellableItemPipeline.Run(sellableEntity, context);

In the plugin, Make sure to add Sitecore.Commerce.Plugin.Workflow(2.4.37 or compatible) assembly to resolve WorkflowComponent.

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.