Umbraco Commerce - Adding Custom ProductSnapShot t...
# help-with-umbraco
r
Hello I am looking into Umbraco Commerce and doing some prototyping. One of our requirements is that we will have products as nodes, and products from external sources. From what I can read, I should be able to create a custom ProductSnapShot that inherits from IProductSnapshotWithImage, and add that to my order. Everything also looks fine except the price gets saved as 0. Help would be greatly appreciated I have based the following code on https://github.com/umbraco/Umbraco.Commerce.DemoStore public IActionResult AddToCart(AddToCartDto postModel) { var store = CurrentPage.GetStore(); var currencyGuid = _currencyService.GetCurrency(store.Id, "GBP"); var name = "Test Product"; var productPrices = new List { new ProductPrice(50, currencyGuid.Id) }; var imageUrl = "https://localhost:44322/media/dksp5tpr/test-3.jpg?width=500&height=500&v=1d885613f9a7f59"; var sku = "sku-123"; var externalProductSnapShot = new ExternalProductSnapShot(store.Id, name, productPrices, imageUrl, sku); _commerceApi.Uow.Execute(uow => { var order = _commerceApi.GetOrCreateCurrentOrder(store.Id) .AsWritable(uow) .AddProduct(externalProductSnapShot, 1); _commerceApi.SaveOrder(order); uow.Complete(); }); return CurrentUmbracoPage(); }
m
You can't just create a snapshot and add it to the order, you have to implement a product adapter which is responsible for creating snapshots from a product reference https://docs.umbraco.com/umbraco-commerce/key-concepts/product-adapters
r
Thanks for the response Matt. I cannot quite get it to work however. I find the documentation to be a bit lacking. The documentation tells us to use IProductAdapter, but that one is obsolete and replaced by ProductAdapterBase. After I have implemented my custom product adapter, how is it supposed to be used? Should it just automatically hook into it when I use the AddProduct to order method, because that currently does not hit my Custom Product Adaptor.
s
I think all the custom logic that extends the commerce logic is added in the addumbracocommerce builder
r
Hi Sander I think that is correct, and according to the documentation I should add my MyCustomProductAdapter there, which I believe I have done correctly like this: public static class DemoStoreBuilderExtensions { public static IUmbracoBuilder AddDemoStore(this IUmbracoBuilder umbracoBuilder) { umbracoBuilder.AddUmbracoCommerce(v => { v.Services.AddUnique(); ... From this point I am unsure of how to proceed, as my logic in MyCustomProductAdapter is never executed. From what I can find the old IProductAdapter would override the GetProductSnapshot used when adding a product to an Order.
33 Views