Umbraco Commerce: Documentation confusion about pr...
# help-with-umbraco
s
I'm following along the Umbraco Commerce documentation (13.latest) and registering a product adaptor does not actually work as described. The docs express:
Copy code
cs
public static class UmbracoCommerceUmbracoBuilderExtensions
{
    public static IUmbracoCommerceBuilder AddMyServices(IUmbracoCommerceBuilder builder)
    {
        // Replacing the default Product Adapter implementation
        builder.Services.AddUnique<ProductAdapterBase, MyCustomProductAdapter>();

        // Return the builder to continue the chain
        return builder;
    }
}
Which is using the
ProductAdaptorBase
as the base class instead of
IProductAdaptor
(.. as is obsolete). But when inheriting from the base and trying to replace it doesn't actually allow me to register it, using the
IProductAdaptor
interface instead works as I expect it to. Switching up my CustomProductAdaptor to inherit base and implement interface then also allows it to work like so:
Copy code
cs
builder.CreateUmbracoBuilder()
    .AddBackOffice()
    .AddWebsite()
    .AddUmbracoCommerce(commerceBuilder =>
    {
        // works just fine
        commerceBuilder.Services.AddUnique<IProductAdapter, ProductAdaptor>();

        // does not work
        // commerceBuilder.Services.AddUnique<ProductAdaptorBase, ProductAdaptor>();

    })
So my final class impl looks like this:
Copy code
cs
public class ProductAdaptor(IProductService productService, ICommerceApi commerceApi) : ProductAdapterBase, IProductAdapter
{
    public override IProductSnapshot GetProductSnapshot(Guid storeId, string productReference, string languageIsoCode)

// rest removed for brevity
}
I think the intent is to actually inherit from the ABC and replace that
Copy code
cs
public abstract class ProductAdapterBase : IProductAdapter
Have I wildly missed something? I'm a little miffed. The obsolete warnings are trigerring. https://cdn.discordapp.com/attachments/1313875209203486771/1313875209828171836/image.png?ex=6751b924&is=675067a4&hm=c8327ccde8747128ed6cb98c671373708dc67923fbff054a2861e9ee1b46e415&
i
Hi Sean, I ran into this recently, and solved it with this code:
Copy code
// Umbraco builder setup
builder.Services.AddUnique<IProductAdapter, CustomProductAdapter>();

// Custom Product Adapter
public class CustomProductAdapter : ProductAdapterBase
{
private readonly ILogger<CustomProductAdapter> _logger;
private readonly ICommerceService _commerceService;
private readonly IPriceService _priceService;

public CustomProductAdapter(
    ICommerceService commerceService,
    ILogger<CustomProductAdapter> logger,
    IPriceService priceService)
{
    _commerceService = commerceService;
    _logger = logger;
    _priceService = priceService;
}

public override IProductSnapshot? GetProductSnapshot(Guid storeId, string productReference, string productVariantReference, string languageIsoCode)
{
    try
    {
        CustomProductSnapshot snapshot = null;
        var sku = _commerceService.GetSku(productReference, productVariantReference);

        // Product Variant - shouldn't have a productVariantReference
        if (string.IsNullOrEmpty(productVariantReference))
        {
            snapshot = new CustomProductSnapshot(storeId, sku, productReference, null, languageIsoCode, _priceService, _logger);
            return snapshot;
        }

        snapshot = new CustomProductSnapshot(storeId, sku, productReference, productVariantReference, languageIsoCode, _priceService, _logger);
        return snapshot;
    }
    catch (Exception e)
    {
        _logger.LogError(e, e.Message);
    }

    return null;
}

public override bool TryGetProductReference(Guid storeId, string sku, out string productReference, out string productVariantReference)
{
    throw new NotImplementedException();
}
}
But your close, you just need to insure that when adding the adapter in your startup code, that you use IProductAdapter and NOT ProductAdapterBase. The actual adapter class needs to inherit from ProductAdapterBase without IProductAdapter !!
s
It fails to load without the reference to the interface though 😦
ProductAdaptor(IProductService productService, ICommerceApi commerceApi) : ProductAdapterBase, IProductAdapter
This is the only flavour in which it loads in
7 Views