I have custom Drupal commerce price resolver that changes the base price for a product based on the value of a cookie (or absence of one). The resolver adds appropriate cookie cache context to the purchaseable entity. For debugging purposes, cookie expiry is 5 minutes.

Prices are rendered by the Calculated Price Formatter, which adds the cookie cache context from the purchaseable entity to the render element metadata.

This works as expected anywhere the product variation price is displayed directly. When the cookie expires, the displayed price is reset.

But for order items in the cart (block, form or summary), which have a line price using the ORDER ITEM total rendered via the Calculated Price Formatter, the cached render element is not being expired with the cookie, and these continue to show the modified price until something else happens to force a cache rebuild.

Does anyone have any ideas about how to ensure the cookie cache context gets bubbled up and expired appropriately for order items with calculated prices that have cache dependencies?

  • Argh. For some reason the cart views do not actually use the configured "commerce_price_calculated" formatter. The formatter setting is ignored and the order item price is formatted using the commerce_price_default. So there is no merging of cache metadata. I smell a bug. – John Pitcairn Mar 15 '19 at 6:04
  • Apparently this is by design, and it is a bug that the formatter can be selected. Which leaves my question open - how to add a cookie cache context to the cart block that will force a cart refresh on the next page load after the cookie expires. Adding the context in hook_block_build_BLOCK_BASE_ID_alter() does not refresh the cart. – John Pitcairn Mar 17 '19 at 2:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.