Copied to clipboard

Flag this post as spam?

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


  • Joppe Ruessink 8 posts 118 karma points
    Feb 23, 2024 @ 12:51
    Joppe Ruessink
    0

    Is it possible to implement own IQuerySpecification for Umbraco Commerce / Vendr?

    Hi,

    For an Umbraco Commerce / Vendr site, I am working on implementing custom filters for the shopping carts and orders view in Umbraco's back office. One of the custom filters I want to implement is to be able to filter based on Shipping County. I have the logic for showing how to actually display the filters in the back-office working. My logic also selects the correct Guid of the country via the switch statement.

    I noticed that the IOrderQuerySpecificationFactory had no implementation for filtering by country. Since these countries are not held at property level of the order, using the properties filter option unfortunately also drops out.

    In the end, I made the choice to implement an IQuerySpecification

    What am i missing?

    [
      AdvancedFilter(
        "hasShippingCountry",
        "Filter on order with shipping country",
        "Select shipping country below",
        Group = "Custom",
        EditorView = "radiobuttons",
        EditorConfig = "{\"items\":[{\"id\":1,\"value\":\"United Kingdom\"},{\"id\":2,\"value\":\"Netherlands\"},{\"id\":3,\"value\":\"Belgium\"},{\"id\":4,\"value\":\"Germany\"},{\"id\":5,\"value\":\"France\"},{\"id\":6,\"value\":\"Denmark\"},{\"id\":7,\"value\":\"Austria\"},{\"id\":8,\"value\":\"Luxembourge\"}]}"
      )
    ]
    public class ShippingCountryFilter : OrderAdvancedFilterBase
    {
      private readonly ICountryService _countryService;
      private readonly StoreReadOnly _store;
    
      public ShippingCountryFilter(ICountryService countryService, IStoreService storeService)
      {
        _countryService = countryService;
        _store = storeService.GetStore(ConfiguratorConsts.StoreName);
      }
    
      public override IQuerySpecification<OrderReadOnly> Apply(IQuerySpecification<OrderReadOnly> query, IOrderQuerySpecificationFactory where, string value)
      {
        //Checking if an selecting is made
        if(!string.IsNullOrEmpty(value))
        {
          //Initializing variables
          CountryReadOnly selectedCountry = null;
    
          //Switching between countries
          string countryIdentifier = value.Replace(" ", "");
          switch (countryIdentifier)
          {
            case "UnitedKingdom":
              selectedCountry = _countryService.GetCountry(_store.Id, "GB");
              break;
            case "Netherlands":
              selectedCountry = _countryService.GetCountry(_store.Id, "NL");
              break;
            case "Belgium":
              selectedCountry = _countryService.GetCountry(_store.Id, "BE");
              break;
            case "Germany":
              selectedCountry = _countryService.GetCountry(_store.Id, "DE");
              break;
            case "France":
              selectedCountry = _countryService.GetCountry(_store.Id, "FR");
              break;
            case "Denmark":
              selectedCountry = _countryService.GetCountry(_store.Id, "DK");
              break;
            case "Austria":
              selectedCountry = _countryService.GetCountry(_store.Id, "AT");
              break;
            case "Luxembourge":
              selectedCountry = _countryService.GetCountry(_store.Id, "LU");
              break;
            default:
              //Not applying any query and returning.
              return query;
          }
    
          //Checking if the country could be found
          if(selectedCountry != null)
          {
            var querySpecification = new ShippingCountryQuerySpecification(selectedCountry.Id);
            query = query.And(querySpecification);
            return query;
          }
        }
    
        //Not applying query and returning.
        return query;
      }
    
      public override bool CanApply(string value)
      {
        return true;
      }
    }
    
    public class ShippingCountryQuerySpecification :
      IQuerySpecification<OrderReadOnly>,
      ISpecification<OrderReadOnly>,
      IVisitable
    {
      public Guid ShippingCountryId { get; set; }
    
      public ShippingCountryQuerySpecification() {}
    
      public ShippingCountryQuerySpecification(Guid shippingCountryId)
      {
        this.ShippingCountryId = shippingCountryId;
      }
    
      public void Accept(IVisitor visitor) => visitor.Visit(this);
    
      public bool IsSatisfiedBy(OrderReadOnly item)
      {
        if(item.ShippingInfo?.CountryId != null)
        {
          return item.ShippingInfo.CountryId == ShippingCountryId;
        }
    
        return false;
      }
    }
    

    Greetings, Joppe

  • Joppe Ruessink 8 posts 118 karma points
    Feb 28, 2024 @ 14:58
    Joppe Ruessink
    0

    Unfortunately, I have not yet been able to find a definitive solution to this problem. However, I have since contacted Umbraco support who indicate that there is no standard solution for extending the IQuerySpecification.

    Maybe, there is a way to 'hack' it but it will be tricky. When I have found a possible solution I will come back to this post.

Please Sign in or register to post replies

Write your reply to:

Draft