Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Michael Nielsen 153 posts 810 karma points
    Sep 26, 2023 @ 14:12
    Michael Nielsen
    0

    Umbraco Commerce: Replacing Product Adapter

    Hi

    I'm trying to replace the default Product Adapter, as products are in a custom db table.

    I've looked at the documentation here https://docs.umbraco.com/umbraco-commerce/key-concepts/product-adapters

    and created a Product Adapter like this

    public class CustomProductAdapter : IProductAdapter
    {
        private readonly ICustomProductsService _customProductsService;
    
        public CustomProductAdapter(CustomProductAdapter _customProductsService)
        {
            _customProductsService = customProductsService;
        }
    
        public IProductSnapshot GetProductSnapshot(Guid storeId, string productReference, string languageIsoCode)
        {
            var product = _customProductsService.GetByProductReference(productReference);
    
            if (product != null)
            {
                return new CustomProductSnapshot(product, languageIsoCode);
            }
    
            return null;
        }
    
        public IProductSnapshot GetProductSnapshot(Guid storeId, string productReference, string productVariantReference, string languageIsoCode)
        {
    
    
            return null;
        }
    
        public bool TryGetProductReference(Guid storeId, string sku, out string productReference, out string productVariantReference)
        {
            // Try lookup a product / variant reference by store + sku
    
            productReference = "";
            productVariantReference = "";
    
            return false;
        }
    
        public PagedResult<IProductSummary> SearchProductSummaries(Guid storeId, string languageIsoCode, string searchTerm, long currentPage = 1, long itemsPerPage = 50)
        {
            // Search for products matching the given search term and convert to a IProductSummary
    
            return null;
        }
    
        public IEnumerable<Attribute> GetProductVariantAttributes(Guid storeId, string productReference, string languageIsoCode)
        {
            throw new NotImplementedException();
        }
    
        public PagedResult<IProductVariantSummary> SearchProductVariantSummaries(Guid storeId, string productReference, string languageIsoCode, string searchTerm, IDictionary<string, IEnumerable<string>> attributes, long currentPage = 1, long itemsPerPage = 50)
        {
            throw new NotImplementedException();
        }
    

    Which is added to Startup like this

    public static IUmbracoCommerceBuilder AddMyCommerceServices(this IUmbracoCommerceBuilder builder)
    {
        // Replacing the default Product Adapter implementation
        builder.Services.AddUnique<IProductAdapter, CustomProductAdapter>();
    
        return builder;
    }
    

    And added it to startup like this

    .AddUmbracoCommerce(builder =>
    {
        builder.AddMyCommerceServices();
    })
    

    However I get an error when trying to add products to the cart like this

    order.AddProduct(addProduct.ProductReference, addProduct.Quantity);
    

    The error I get is this

    ArgumentNullException: Value cannot be null. (Parameter 'productSnapshot')
    

    Not sure what it is I'm missing 🤔

  • Michael Nielsen 153 posts 810 karma points
    Sep 27, 2023 @ 12:12
    Michael Nielsen
    100

    I've digged a little deeper, and found the answer in this answer https://our.umbraco.com/packages/website-utilities/vendr/vendr-support/107406-product-prices-from-external-source#comment-334241

    I don't have variants, but after adding logic to the method that takes a productVariantReference parameter, it works, as tht is the method that is called.

Please Sign in or register to post replies

Write your reply to:

Draft