Skip to content

Pass an explicit context to the product attribute formatter - #936

Merged
KrzysztofPajak merged 2 commits into
developfrom
refactor/business-explicit-context-attribute-formatter
Oct 17, 2026
Merged

KrzysztofPajak merged 2 commits into
developfrom
refactor/business-explicit-context-attribute-formatter

Conversation

@KrzysztofPajak

@KrzysztofPajak KrzysztofPajak commented Oct 17, 2026 •

Copy link
Copy Markdown
Member

Type: feature

Issue

ProductAttributeFormatter (Grand.Business.Catalog) read the customer, store, working currency, tax display, working language and request host from WorkContext / StoreContext. The attribute descriptions it builds (cart, wishlist, order items, out-of-stock subscriptions, message tokens) therefore depended on who called it. Outside a request, for example a campaign or a stock notification sent from a scheduled task, it read whatever context happened to be there. Follows #931 (CalculationContext) and #932 (the same change for CheckoutAttributeFormatter).

Solution

ProductAttributeFormatter no longer takes IContextAccessor. It now has the same shape as ICheckoutAttributeFormatter since #932:

IProductAttributeFormatter.FormatAttributes(product, attributes, CalculationContext context, Language language, DomainHost host, separator, …)

  • context is required when renderPrices is true (ArgumentNullException otherwise), so the attribute-combination tokens (prices off) pass null. host is read only for file-upload links; without a host the link is not rendered.
  • The parameterless overload FormatAttributes(product, attributes) is gone.
  • Storefront cart handlers use their own request values. The cart requests (GetAddToCart, GetShoppingCart, GetMiniShoppingCart, GetWishlist, GetMiniWishlist) implement the new ICartCalculationRequest, and one request.CalculationContext() extension builds the context for all of them. Host is added to GetAddToCart, GetMiniShoppingCart, GetWishlist and GetMiniWishlist (which also gains TaxDisplayType); GetWishlistHandler and GetMiniWishlistHandler no longer take IContextAccessor.
  • Shared wishlist: GetWishlist gains CurrentCustomer, the visitor. Attribute price adjustments, unit price, subtotal and discount are calculated for the visitor, as before; Customer (the owner) is still used for the name and the warnings.
  • Admin passes the request context with the customer whose cart is shown and the cart item's store (with { Customer = customer, Store = priceStore }), so the attribute adjustments match the unit price next to them.
  • Admin "add product to order" stores the attribute description rendered for the order: its store, currency, language, tax display and the store's primary host, not the admin's.
  • PlaceOrderCommandHandler formats order items and checkout attributes with the order's context, the working language of the request (PlaceOrderContainer.WorkingLanguage, as before) and its host (PlaceOrderContainer.Host).
  • Message tokens: GetShoppingCartTokensCommand carries Context and Host (in place of Customer and Store), GetAttributeCombinationTokensCommand carries Language and Host, and LiquidObjectBuilder.AddShoppingCartTokens / AddAttributeCombinationTokens take them.
  • CampaignService no longer takes IContextAccessor: both SendCampaign overloads take the currency, tax display and host, and CampaignController passes the admin request's values, as before. MessageProviderService passes the values it already holds; its own ambient reads are left for the Marketing/Messages PR.

Fixed on the way (found in review of this PR):

  • {{ShoppingCart}} tokens: an item without attributes repeated the previous item's attribute description and picture, and an item whose product was deleted failed the whole message. Each item now starts empty, and deleted products are skipped.
  • Admin customer → out-of-stock subscriptions: the attributes check was inverted, so the description was always empty and Attributes == null threw.

Deliberate behavior changes (off the normal storefront path, where the values are the same):

  • Message tokens: in {{ShoppingCart}} and {{AttributeCombination}}, attribute names now use the message's language instead of the language of whoever triggered the message. Example: a campaign sent from an English admin panel to a Polish customer.
  • Campaign cart prices: attribute price adjustments in campaign emails use the campaign's store instead of the store of the admin request.
  • Admin add product to order: see above; the description is in the order's currency and language.
  • The old fallback to the customer's LanguageId field when there was no working language is gone, because every caller now passes a language.

Breaking changes

  • IProductAttributeFormatter: FormatAttributes(product, attributes) is removed, and FormatAttributes(product, attributes, Customer customer, …) became FormatAttributes(product, attributes, CalculationContext, Language, DomainHost, …).
  • ProductAttributeFormatter constructor no longer takes IContextAccessor.
  • ICampaignService.SendCampaign(campaign, emailAccount, subscriptions | email) gains Currency currency, TaxDisplayType taxDisplayType, DomainHost host; CampaignService constructor no longer takes IContextAccessor.
  • GetShoppingCartTokensCommand: Customer and Store replaced by Context and Host. GetAttributeCombinationTokensCommand gains Language and Host.
  • LiquidObjectBuilder.AddShoppingCartTokens(CalculationContext, DomainHost, Language) and AddAttributeCombinationTokens(product, combination, Language, DomainHost).
  • OrderViewModelService constructor gains ILanguageService.
  • GetWishlist gains CurrentCustomer; GetMiniWishlist gains Host and TaxDisplayType. Senders must fill them.

Plugins that call or implement these must be rebuilt.

Testing

  1. dotnet build GrandNode.slnx: succeeds.
  2. dotnet test for Catalog, Checkout, Messages, Marketing, Web, Web.Admin, Web.Common: all green.
  3. New tests:
    • ProductAttributeFormatterTests: names in the given language, price adjustment priced and formatted for the given context, no context needed without prices, a context required with prices, file link to the given host, no link without a host
    • GetShoppingCartTokensCommandHandlerTests: forwards the message's context, language and host; an item without attributes does not repeat the previous item's description; a deleted product is skipped
  4. Manual check on a fresh install with sample data, using a product with priced attributes (i7 +$150, 32 GB +$90):
    • cart page, mini cart, wishlist, mini wishlist and the add-to-cart / add-to-wishlist responses show the same attribute text and adjustments; a shared wishlist opened by an anonymous visitor renders them too
    • checkout in EUR: the order item stores Intel Core i7 [+€142.50], 32 GB [+€85.50] in English
    • admin working in USD adds an i9 desktop to that EUR order: the stored description is Intel Core i9 [+€361.00]
    • admin customer cart and wishlist grids (Customer/GetCartList, ShoppingCart/GetCartDetails, GetWishlistDetails) show the descriptions with adjustments
    • admin out-of-stock subscriptions tab: a subscription with attributes shows them; empty and null attributes show nothing and do not fail
    • campaign test and mass email with a {% for item in ShoppingCart.Items %} body: a deleted product is skipped and an item without attributes has an empty description

ProductAttributeFormatter no longer reads IContextAccessor. FormatAttributes
takes the CalculationContext the prices are rendered for, the language of the
attribute names and the store host used for file download links. Callers pass
the values they already hold: the storefront cart handlers their request
(Host added to GetAddToCart, GetMiniShoppingCart and GetWishlist), the admin
panel the request context with the customer whose cart is shown, PlaceOrder
its command, and the message tokens the message's language and host.
Copilot AI balanced review requested due to automatic review settings October 17, 2026 05:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

- Shopping cart tokens: reset the attribute description and picture for each
  item and skip items whose product was deleted.
- Customer out-of-stock subscriptions: fix the inverted attributes check.
- PlaceOrder: render order item attribute descriptions in the working
  language again (PlaceOrderContainer.WorkingLanguage).
- Shared wishlist: price attribute adjustments, unit price and subtotal for
  the viewing customer (GetWishlist.CurrentCustomer), as before.
- Admin add product to order: format the stored description for the order's
  store, currency, language, tax display and primary host.
- Admin cart and wishlist grids: price attribute adjustments for the cart
  item's store.
- CampaignService: take currency, tax display and host from the caller
  instead of the ambient context.
- GetMiniWishlist carries Host and TaxDisplayType; its handler no longer
  reads the ambient context.
- ProductAttributeFormatter: require a context when rendering prices and
  render no download link without a host.
- Cart requests share ICartCalculationRequest and one CalculationContext()
  extension instead of three copies.
@KrzysztofPajak
KrzysztofPajak merged commit 4b282c4 into develop Oct 17, 2026
9 checks passed
@KrzysztofPajak
KrzysztofPajak deleted the refactor/business-explicit-context-attribute-formatter branch October 17, 2026 13:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants