- πΊπΈUnited States derekw
I've gotten >< this close to a solution.
I want to apply a coupon that takes $100 off every matching purchased variation... and see that reflected on the relevant cart line items.
I am using a rendered product variation view mode 'cart' to display selected attributes and the calculated price in the cart view.
So I took the suggestions in this issue:
1) Extended the commerce_order PriceCalculated formatter to add the current user's cart to the context.
2) used hook_field_formatter_info_alter to set the commerce_price_calculated class to my extended formatter.3) Extended the PriceCalculator and changed added to the prepareOrder function to check for the cart in the context (set in the custom formatter), and use that rather than creating a fake one. (... but maybe I should just copy the coupons from the user's cart to the fake order.)
4) Used mymodule.services.yml to override commerce_order.price_calculator and set the class to my custom priceCalculator.
5) Extended the CouponRedeption views area to reload the page vs just updating the total price.
That got me pretty close, but the rendered product variation 'cart' view on the line items was not refreshing, although the unit price and totals for the line items in the cart were.
6) So I used hook_preprocess_commerce_product_variation and invalidated the cache tags for cart and summary view modes.
The coupon discounts are shown in the cart line item rendered variations along with promotions, etc.
But... when I check out and the order confirmation email rolls in... the line item total is right but the coupon discount is no longer shown in the rendered product variation (using the same summary view which works in the checkout and review pane).
So frustrating for something that seems like it should just work out of the box: Apply a coupon that affects specific product variations, and see that coupon reflected in the calculated price.
- π·πΈSerbia bojanz
The calculated price formatter was never designed to show discounts coming from coupons. In fact, it was never designed to be used in a real order / cart context. The cart knows all of its discounts and coupons, it doesn't need the calculated price formatter to repeat the calculations. The point of the formatter is to show a product in the catalog with a valid offer applied (e.g. price list for that day/customer, discount for that product category, etc).
Should we be making an effort to forbid using this formatter on a cart / order item?
- πΊπΈUnited States derekw
That's very helpful to understand. I always know when I have to do a lot of workarounds that I'm probably just going about it the wrong way, kicking against the goads. (The price difference formatter for order line items also does not factor in coupons applied.)
To show the adjustments (including coupons) applied to a line item in the cart, it looks like "the way" is to create a commerce_adjustment field formatter to display the order item adjustments field, since it does not seem to have a formatter out of the box.