I have an issue where sales and sale pricing seem not to be present in the cart. This is on an ecommerce flow where this was previously all working but I had moved onto other dev work on this site for many weeks, applying Craft and Commerce updates over that time - and have now returned to the ecommerce flow to find this not working.
Craft Pro 3.3.20.1
Commerce 2.2.14
What is happening is that when I have a sale for a given product, the discounted sale price does not appear to be in the cart (Craft returns the base price) and the lineItems do not appear to have any sales in place for a given item. I stripped this back to a barebones template:
{% set cart = craft.commerce.carts.cart %}
{% if cart.isEmpty() %}
Empty
{% else %}
{% set lineItems = cart.lineItems %}
{% for item in cart.lineItems %}
{% set variant = item.purchasable %}
{% set product = variant.product %}
<p>{{ product.title }}</p>
<p>
{% if item.onSale %}
Sale: {{ item.salePrice }} - Was: {{ item.price }}</span>
{% else %}
No Sale: {{ item.price }}
{% endif %}
</p>
{% endfor %}
{% endif %}
<pre>{{ dump(lineItems) }}</pre>
itemTotal:{{ dump(cart.itemTotal) }}<br><br>
itemSubtotal:{{ dump(cart.itemSubtotal) }}<br><br>
total:{{ dump(cart.total) }}<br><br>
totalDiscount:{{ dump(cart.totalDiscount) }}<br><br>
totalTaxablePrice:{{ dump(cart.totalTaxablePrice) }}<br><br>
totalShippingCost:{{ dump(cart.totalShippingCost) }}<br><br>
totalSaleAmount:{{ dump(cart.totalSaleAmount) }}<br><br>
getTotal():{{ cart.getTotal() }}<br><br>
getItemSubtotal():{{ cart.getItemSubtotal() }}<br><br>
getItemTotal():{{ cart.getItemTotal() }}<br><br>
So what happens:
- I add a product to the cart which has an applicable sale (I've tested it has one by triple-checking the sale logic in the CP against the product and checking the product details in twig via
{% set productSales = product.defaultVariant.getSales() %}
to confirm it has applicable sales. - Once in the cart, via the template above, the returned prices all reflect the base price - no sale price.
- The
sales
object has nothing in it fromlineItems
anditem.onSale
isfalse
(though I noticelineItems[0].snapshot.onSale
istrue
(but I'm not certain what that is supposed to represent).
I'd normally conclude I was doing something wrong, but I believe my code follows the Commerce template examples and was working fine when I last worked on it (awhile back... maybe October).
I wondered if some commerce update has fundamentally changed something about how this should all work but could find no clues in the changelog.
Can anyone confirm there isn't something fundamentally wrong with my cart code above - or has something changed in Commerce? Or could there actually be a bug?
EDIT: The non-sale (base price) I see in the cart is reflected in the actual order details in the CP.