Issue with custom site price agent item factory not being invoked while calling price agent

Please see my code. Am I doing something wrong?

TapwellProductPriceAgentFeed.cs

using Litium.Owin.InversionOfControl;
using Litium.Web.Products.PriceAgents;
using System;
using System.Collections.Generic;
using System.IO;
using Tapwell.Integration.TapwellPriceAgent;
using Tapwell.Integration.Utilities;

namespace Tapwell.Integration
{
    [Plugin("TapwellProductFeed")]
    public class TapwellProductPriceAgentFeed : IPriceAgent
    {
        public TapwellProductPriceAgentFeed()
        {
        }

        /// <summary>
        /// Gets the type of the content.
        /// </summary>
        /// <value>
        /// The type of the content.
        /// </value>
        public string ContentType
        {
            get { return "text/plain"; }
        }

        /// <summary>
        /// Render price agent
        /// </summary>
        /// <param name="writer">The writer.</param>
        /// <param name="priceAgentItemResults">The price agent item results.</param>
        /// <exception cref="System.NotImplementedException"></exception>
        public void Render(TextWriter writer, IEnumerable<PriceAgentItemResult> priceAgentItemResults)
        {
            //DO NOT fetch the product name, or price or any other article information here,
            //instead specify them in the PriceAgentItemFactory implementation.
            TapwellChannelFeedDto feed = new TapwellChannelFeedDto()
            {
                Title = "Tapwell Product Feed",
                Description = "Tapwell Product Feed For External Websites",
                Items = new List<TapwellPriceAgentItemResult>()
            };


            try
            {
                foreach (PriceAgentItemResult priceAgentItemResult in priceAgentItemResults)
                {
                    var item = priceAgentItemResult as TapwellPriceAgentItemResult;
                    if (item == null)
                        continue;

                    feed.Items.Add(item);
                }
            }
            catch (Exception ex)
            {
                string s = ex.Message;
            }

            string sb = feed.ObjectToXmlSerialize();

            writer.Write(sb);
        }
    }
}

SitePRiceAgentItemFactory.cs

using Litium.Accelerator.Builders.Framework;
using Litium.Accelerator.Extensions;
using Litium.Account;
using Litium.Common;
using Litium.FieldFramework;
using Litium.Foundation.Modules.ECommerce.Plugins.Orders;
using Litium.Foundation.Modules.ECommerce.Plugins.PriceCalculator;
using Litium.Foundation.Modules.ExtensionMethods;
using Litium.Foundation.Settings;
using Litium.Globalization;
using Litium.Media;
using Litium.Products;
using Litium.Products.PriceCalculator;
using Litium.Products.StockStatusCalculator;
using Litium.Web;
using Litium.Web.Models.Products;
using Litium.Web.Products.PriceAgents;
using Litium.Web.Routing;
using Litium.Websites;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;

namespace Tapwell.Integration.TapwellPriceAgent
{
	public class SitePriceAgentItemFactory : PriceAgentItemFactory, IPriceAgentItemFactory
	{
		private readonly ChannelService _channelService;
		private ProductModelBuilder _productModelBuider;
		private readonly FileService _fileService;
		private string _baseUrl;
		private readonly RouteRequestLookupInfoAccessor _routeRequestLookupInfoAccessor;
		private readonly UnitOfMeasurementService _unitOfMeasurementService;
		private readonly SiteSettingViewModelBuilder _siteSettingViewModelBuilder;
		private readonly ProductListService _productListService;
		private readonly ProductListItemService _productListItemService;
		private readonly KeyLookupService _keyLookupService;
		List<Guid> activeVariantsList = null;
		public SitePriceAgentItemFactory(IPriceCalculator priceCalculator, IStockStatusCalculator stockStatusCalculator, ICampaignPriceCalculator campaignPriceCalculator, 
			IOrderFactory orderFactory, LanguageService languageService, UrlService urlService, CategoryService categoryService, CurrencyService currencyService, 
			FieldDefinitionService fieldDefinitionService, ISettingService settingService, WebsiteService websiteService, CountryService countryService, 
			ChannelService channelService, DomainNameService domainNameService, Litium.Application.Security.AuthorizationOperationHelperService authorizationOperationHelperService, 
			AccountService accountService, Litium.Application.Security.AccessControlService accessControlService, RouteRequestLookupInfoAccessor routeRequestLookupInfoAccessor, 
			FileService fileService, UnitOfMeasurementService unitOfMeasurementService, SiteSettingViewModelBuilder siteSettingViewModelBuilder,
			ProductListService productListService, ProductListItemService productListItemService, KeyLookupService keyLookupService, ProductModelBuilder productModelBuider)
			: base(priceCalculator, stockStatusCalculator, campaignPriceCalculator, orderFactory, languageService, urlService, categoryService, currencyService,
				  fieldDefinitionService, settingService, websiteService, countryService, channelService, domainNameService, authorizationOperationHelperService, 
				  accountService, accessControlService)
		{
			_productModelBuider = productModelBuider;
			_channelService = channelService;
			_siteSettingViewModelBuilder = siteSettingViewModelBuilder;
			_productListService = productListService;
			_productListItemService = productListItemService;
			_keyLookupService = keyLookupService;
			_fileService = fileService;
			_routeRequestLookupInfoAccessor = routeRequestLookupInfoAccessor;
			_unitOfMeasurementService = unitOfMeasurementService;

			Guid? productFeedProductListId = _siteSettingViewModelBuilder.GetProductFeedProductListId();

			if (productFeedProductListId != null)
			{
				var existProductList = _productListService.Get<ProductList>((Guid)productFeedProductListId);
				if (existProductList != null)
				{
					activeVariantsList = _productListItemService.GetByProductList(existProductList.SystemId)
						.SelectMany(x => x.ActiveVariantSystemIds).ToList();
				}
			}
		}

		PriceAgentItemResult IPriceAgentItemFactory.Create(PriceAgentItemArgs priceAgentItemArgs)
        {
			try
			{
				var variant = priceAgentItemArgs.Variant;
				if (!activeVariantsList.Contains(variant.SystemId) || !variant.IsPublished())
					return null;

				var productModel = _productModelBuider.BuildFromVariant(variant);
				var baseProduct = productModel.BaseProduct;
				

				//Unhandled exception in here
				var item = Create(priceAgentItemArgs);

				//var nordicFeelItem = ConvertToNordicFeelItem(item, priceAgentItemArgs.BaseProduct, language);
				var drawing2DFileId = baseProduct.Fields
								.GetValue<Guid>("Drawing2D");
				var drawing2DUrl = drawing2DFileId != Guid.Empty
					? GetAbsoluteUrl(_fileService.Get(drawing2DFileId)?.GetUrl(true))
					: "";

				var drawing3DFileId = baseProduct.Fields
					.GetValue<Guid>("Drawing3D");
				var drawing3DUrl = drawing3DFileId != Guid.Empty
					? GetAbsoluteUrl(_fileService.Get(drawing3DFileId)?.GetUrl(true))
					: "";

				var drawingPDFFileId = baseProduct.Fields
					.GetValue<Guid>("DrawingPDF", CultureInfo.CurrentUICulture);
				var drawingPDFUrl = drawingPDFFileId != Guid.Empty
					? GetAbsoluteUrl(_fileService.Get(drawingPDFFileId)?.GetUrl(true))
					: "";

				var connectionInlet = baseProduct.Fields.GetValue<string>("ConnectionInlet", CultureInfo.CurrentUICulture);
				var numberOfGrips = baseProduct.Fields.GetValue<string>("NumberOfGrips", CultureInfo.CurrentUICulture);
				var buildingHeightUnderneathOutlet = baseProduct.Fields.GetValue<decimal>("BuildingHeightUnderneathOutlet");
				var builtIn = baseProduct.Fields.GetValue<bool>("BuiltIn");
				var classificationBK04 = baseProduct.Fields.GetValue<string>("ClassificationBK04");
				var classificationBSAB = baseProduct.Fields.GetValue<string>("ClassificationBSAB");
				var color = variant.Fields.GetValue<string>("Color", CultureInfo.CurrentUICulture);
				var commodityCode = variant.Fields.GetValue<string>("CommodityCode");
				var diameterCeilingShowerHead = baseProduct.Fields.GetValue<decimal>("DiameterCeilingShowerHead");

				var explodedViewPDFFileId = baseProduct.Fields
					.GetValue<Guid>("DrawingPDF", CultureInfo.CurrentUICulture);
				var explodedViewPDFUrl = explodedViewPDFFileId != Guid.Empty
					? GetAbsoluteUrl(_fileService.Get(explodedViewPDFFileId)?.GetUrl(true))
					: "";

				var flowClass = baseProduct.Fields.GetValue<string>("FlowClass");
				var gtin = variant.Fields.GetValue<string>("GTIN");

				var installationGuidePDFFileId = baseProduct.Fields
					.GetValue<Guid>("InstallationGuide", CultureInfo.CurrentUICulture);
				var installationGuidePDFUrl = installationGuidePDFFileId != Guid.Empty
					? GetAbsoluteUrl(_fileService.Get(installationGuidePDFFileId)?.GetUrl(true))
					: "";

				var lengthProtrusionOutletHead = baseProduct.Fields.GetValue<decimal>("LengthProtrusionOutletHead");
				var lockableHead = baseProduct.Fields.GetValue<IList<string>>("LockableHead");
				var machineShutdown = baseProduct.Fields.GetValue<bool>("MachineShutdown");
				var maxFlow = baseProduct.Fields.GetValue<string>("MaxFlow");
				var packageDepth = baseProduct.Fields.GetValue<decimal>("PackageDepth");
				var packageHeight = baseProduct.Fields.GetValue<decimal>("PackageHeight");
				var packageWeight = baseProduct.Fields.GetValue<decimal>("PackageWeight");
				var packageWidth = baseProduct.Fields.GetValue<decimal>("PackageWidth");
				var packageVolume = baseProduct.Fields.GetValue<decimal>("PackageVolume");
				var preasureControlled = baseProduct.Fields.GetValue<bool>("PreasureControlled");

				var pressureDropDiagramId = baseProduct.Fields
					.GetValue<Guid>("PressureDropDiagram", CultureInfo.CurrentUICulture);
				var pressureDropDiagramPDFUrl = pressureDropDiagramId != Guid.Empty
					? GetAbsoluteUrl(_fileService.Get(pressureDropDiagramId)?.GetUrl(true))
					: "";

				var productDepth = baseProduct.Fields.GetValue<decimal>("ProductDepth");
				var productDescription = baseProduct.Fields.GetValue<string>("ProductDescription", CultureInfo.CurrentUICulture);
				var productHeight = baseProduct.Fields.GetValue<decimal>("ProductHeight");

				var productSheetFileId = baseProduct.Fields
					.GetValue<Guid>("ProductSheet", CultureInfo.CurrentUICulture);
				var productSheetPDFUrl = productSheetFileId != Guid.Empty
					? GetAbsoluteUrl(_fileService.Get(productSheetFileId)?.GetUrl(true))
					: "";

				var productWidth = baseProduct.Fields.GetValue<decimal>("ProductWidth");
				var rskNumber = variant.Fields.GetValue<int>("RSKNumber");
				var sizeCeilingShowerHead = baseProduct.Fields.GetValue<string>("SizeCeilingShowerHead");
				var temperatureControl = baseProduct.Fields.GetValue<bool>("TemperatureControl");

				var totalBuildingHeight = variant.Fields.GetValue<decimal>("TotalBuildingHeight");
				var turnableHead = baseProduct.Fields.GetValue<bool>("TurnableHead");
				var unitOfMeasurement = _unitOfMeasurementService.Get(variant.UnitOfMeasurementSystemId.GetValueOrDefault());

				TapwellPriceAgentItemResult resultItem = new TapwellPriceAgentItemResult()
				{
					ArticleNumber = item.ArticleNumber,
					ConnectionInlet = connectionInlet,
					NumberOfGrips = numberOfGrips,
					ProductName = item.ProductName,
					ProductDescription = productDescription,
					ProductImageUrl = item.ProductImageUrl,
					BuildingHeightUnderneathOutlet = buildingHeightUnderneathOutlet,
					BuiltIn = builtIn,
					ClassificationBK04 = classificationBK04,
					ClassificationBSAB = classificationBSAB,
					Color = color,
					CommodityCode = commodityCode,
					DiameterCeilingShowerHead = diameterCeilingShowerHead,
					Drawing2D = drawing2DUrl,
					Drawing3D = drawing3DUrl,
					DrawingPDF = drawingPDFUrl,
					ExplodedViewUrl = explodedViewPDFUrl,
					FlowClass = flowClass,
					GTIN = gtin,
					InstallationGuideUrl = installationGuidePDFUrl,
					LengthProtrusionOutletHead = lengthProtrusionOutletHead,
					LockableHead = lockableHead,
					MachineShutdown = machineShutdown,
					MaxFlow = maxFlow,
					PackageDepth = packageDepth,
					PackageHeight = packageHeight,
					PackageWeight = packageWeight,
					PackageWidth = packageWidth,
					PackageVolume = packageVolume,
					PreasureControlled = preasureControlled,
					PressureDropDiagramPDFUrl = pressureDropDiagramPDFUrl,
					ProductDepth = productDepth,
					ProductHeight = productHeight,
					ProductSheetPDFUrl = productSheetPDFUrl,
					ProductWidth = productWidth,
					RSKNumber = rskNumber,
					SizeCeilingShowerHead = sizeCeilingShowerHead,
					TemperatureControl = temperatureControl,
					TotalBuildingHeight = totalBuildingHeight,
					TurnableHead = turnableHead,
					ProductCategory = item.ProductCategory,
					PriceWithVat = item.PriceWithVat,
					VatPercentage = item.VatPercentage,
					UnitOfMeasurement = unitOfMeasurement?.Localizations.CurrentCulture.Name ?? unitOfMeasurement?.Id
				};

				return resultItem;
			}
			catch (Exception ex)
			{
				//this.Log().Error("Create item for variant " + (priceAgentItemArgs.Variant?.Id ?? string.Empty), ex);
				return null;
			}
		}

		private string GetBaseUrl()
		{
			if (_baseUrl != null)
			{
				return _baseUrl;
			}

			var hostName = _routeRequestLookupInfoAccessor.RouteRequestLookupInfo.DomainName.Id;

			if (hostName.Contains("/"))
			{
				hostName = hostName.Substring(0, hostName.IndexOf("/", StringComparison.Ordinal));
			}

			int? port = null;
			if (hostName.Contains(":"))
			{
				var s = hostName.Split(':');
				hostName = s.First();
				port = int.Parse(s.Last());
			}

			var uriBuilder = new UriBuilder(_routeRequestLookupInfoAccessor.RouteRequestLookupInfo.IsSecureConnection ? Uri.UriSchemeHttps : Uri.UriSchemeHttp, hostName, port ?? (_routeRequestLookupInfoAccessor.RouteRequestLookupInfo.IsSecureConnection ? 443 : 80));

			_baseUrl = new Uri(uriBuilder.Uri.ToString()).AbsoluteUri.TrimEnd('/');
			return _baseUrl;
		}

		private string GetAbsoluteUrl(string url)
		{
			if (!string.IsNullOrEmpty(url))
			{
				return url.Contains("//") ? url : GetBaseUrl() + VirtualPathUtility.ToAbsolute(url);
			}

			return string.Empty;
		}
	}
}

TapwellPriceAgentItemResult.cs

using Litium.Web.Products.PriceAgents;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tapwell.Integration.TapwellPriceAgent
{
    [Serializable]
    public class TapwellPriceAgentItemResult : PriceAgentItemResult
    {
        public string ConnectionInlet { get; set; }
        public string NumberOfGrips { get; set; }
        public decimal BuildingHeightUnderneathOutlet { get; set; }
        public bool BuiltIn { get; set; }
        public string ClassificationBK04 { get; set; }
        public string ClassificationBSAB { get; set; }
        public string Color { get; set; }
        public string CommodityCode { get; set; }
        public decimal DiameterCeilingShowerHead { get; set; }
        public string Drawing2D { get; set; }
        public string Drawing3D { get; set; }
        public string DrawingPDF { get; set; }
        public string ExplodedViewUrl { get; set; }
        public string FlowClass { get; set; }
        public string GTIN { get; set; }
        public string InstallationGuideUrl { get; set; }
        public decimal LengthProtrusionOutletHead { get; set; }
        public IList<string> LockableHead { get; set; }
        public bool MachineShutdown { get; set; }
        public string MaxFlow { get; set; }
        public decimal PackageDepth { get; set; }
        public decimal PackageHeight { get; set; }
        public decimal PackageWeight { get; set; }
        public decimal PackageWidth { get; set; }
        public decimal PackageVolume { get; set; }
        public bool PreasureControlled { get; set; }
        public string PressureDropDiagramPDFUrl { get; set; }
        public decimal ProductDepth { get; set; }
        public decimal ProductHeight { get; set; }
        public string ProductSheetPDFUrl { get; set; }
        public decimal ProductWidth { get; set; }
        public int RSKNumber { get; set; }
        public string SizeCeilingShowerHead { get; set; }
        public bool TemperatureControl { get; set; }
        public decimal TotalBuildingHeight { get; set; }
        public bool TurnableHead { get; set; }
        public string UnitOfMeasurement { get; set; }
    }
}

TapwellChannelFeedDTO.cs

using Litium.Web.Products.PriceAgents;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Tapwell.Integration.TapwellPriceAgent
{
    [Serializable()]
    [System.ComponentModel.DesignerCategory("code")]
    [System.Xml.Serialization.XmlType(AnonymousType = true)]
    [System.Xml.Serialization.XmlRoot(ElementName = "Channel", Namespace = "", IsNullable = false)]
    public class TapwellChannelFeedDto
    {
        //[System.Xml.Serialization.XmlElement("title")]
        public string Title { get; set; }
        //[System.Xml.Serialization.XmlElement("description")]
        public string Description { get; set; }
        //[System.Xml.Serialization.XmlElement("items")]
        public List<TapwellPriceAgentItemResult> Items { get; set; }
    }
}

Litium version: [7.4.0]

Please check this topic and see if you have similar issue:

I have added like this. Do i need to add types too That too didn’t invoke

<plugins>
        <assemblies>
          <add assembly="Tapwell.Integration.TapwellPriceAgent"/>
        </assemblies>
        <types>
        </types>
</plugins>

Not sure why you inherit both from IPriceAgentItemFactory and PriceAgentItemFactory since you only use the Create method.
And also Create method should override the base Virtual method.

public override PriceAgentItemResult Create(PriceAgentItemArgs priceAgentItemArgs)

Seems like i gave the wrong assembly

it worked when i corrected it

<plugins>
        <assemblies>
          <add assembly="Tapwell.Integration"/>
        </assemblies>
        <types>
        </types>
</plugins>
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.