craftcms / commerce

Fully integrated ecommerce for Craft CMS.
https://craftcms.com/commerce
Other
217 stars 170 forks source link

[5.x]: Promotional Prices not populated on LineItem #3634

Closed peteeveleigh closed 1 week ago

peteeveleigh commented 1 month ago

What happened?

Description

I'm not sure if this is a bug or not. but when updating a LineItem, the promotional properties are not being set. They are being set within the LineItem snapshot. See the dump below.

The purchasable for this LineItem is on promotion. The regular price is $6.38 and the promotional price is $5.38.

Within the LineItem data you will see that the price and salePrice are shown as $6.38 but the promotionalPrice is null. Within the Snapshot data the promotional prices are shown.

{
    "id": 33,
    "weight": 0,
    "length": 0,
    "height": 0,
    "width": 0,
    "qty": 4,
    "note": "",
    "privateNote": "",
    "purchasableId": 21,
    "orderId": 11,
    "lineItemStatusId": null,
    "taxCategoryId": 1,
    "shippingCategoryId": 1,
    "dateCreated": "2024-08-14T16:30:37+00:00",
    "dateUpdated": "2024-08-16T09:48:38+00:00",
    "uid": "c2b8b91f-7e22-42a8-a6d7-3e5be835de39",
    "adjustments": [],
    "description": "Bacon - Nom Nom Bacon",
    "options": [],
    "optionsSignature": "d751713988987e9331980363e24189ce",
    "onPromotion": false,                    <---- HERE (this item is on promotion)
    "price": 6.38,
    "promotionalPrice": null,                     <---- HERE
    "salePrice": 6.38,
    "sku": "NOM-01",
    "total": 25.52,
    "fulfilledTotalQuantity": 0,
    "priceAsCurrency": "$6.38",
    "promotionalPriceAsCurrency": "$0.00",                    <---- HERE
    "promotionalAmountAsCurrency": "$0.00",                    <---- HERE
    "salePriceAsCurrency": "$6.38",
    "subtotalAsCurrency": "$25.52",
    "totalAsCurrency": "$25.52",
    "discountAsCurrency": "$0.00",
    "shippingCostAsCurrency": "$0.00",
    "taxAsCurrency": "$0.00",
    "taxIncludedAsCurrency": "$0.00",
    "adjustmentsTotalAsCurrency": "$0.00",
    "subtotal": 25.52,
    "snapshot": {
        "isDefault": true,
        "sortOrder": 1,
        "deletedWithProduct": false,
        "width": null,
        "height": null,
        "length": null,
        "weight": null,
        "freeShipping": false,
        "promotable": true,                    <---- HERE
        "availableForPurchase": true,
        "minQty": 2,
        "maxQty": 5,
        "inventoryItemId": 3,
        "inventoryTracked": false,
        "eagerLoadInfo": null,
        "id": 21,
        "tempId": null,
        "draftId": null,
        "revisionId": null,
        "isProvisionalDraft": false,
        "uid": "ac6b1d65-de80-40cc-b041-951f1d7a82e4",
        "siteSettingsId": 21,
        "fieldLayoutId": 2,
        "enabled": true,
        "archived": false,
        "siteId": 1,
        "title": "Nom Nom Bacon",
        "slug": "__temp_eargccjpxwyehgqhzlotiplzhtoakwdbytsx",
        "uri": null,
        "dateCreated": "2024-08-13T16:48:20+00:00",
        "dateUpdated": "2024-08-14T17:06:22+00:00",
        "dateLastMerged": null,
        "dateDeleted": null,
        "deletedWithOwner": null,
        "trashed": false,
        "isNewForSite": false,
        "forceSave": false,
        "fieldId": null,
        "canonicalId": 21,
        "cpEditUrl": "#",
        "isDraft": false,
        "isRevision": false,
        "isUnpublishedDraft": false,
        "ref": null,
        "status": "enabled",
        "structureId": null,
        "url": "https://foster-checkout-v2-dev-site.ddev.site/products/bacon?variant=21",
        "isAvailable": true,
        "isPromotable": true,                     <---- HERE
        "price": 6.38,
        "promotionalPrice": 5.38,                    <---- HERE
        "basePrice": 6.38,
        "basePromotionalPrice": 5.38,                    <---- HERE
        "onPromotion": true,                    <---- HERE
        "salePrice": 5.38,
        "sku": "NOM-01",
        "stock": 0,
        "product": {
            "postDate": "2024-08-13T16:47:24+00:00",
            "expiryDate": null,
            "typeId": 1,
            "defaultVariantId": 21,
            "defaultSku": "NOM-01",
            "defaultHeight": null,
            "defaultLength": null,
            "defaultWidth": null,
            "defaultWeight": null,
            "taxCategory": null,
            "name": null,
            "eagerLoadInfo": null,
            "id": 19,
            "tempId": null,
            "draftId": null,
            "revisionId": null,
            "isProvisionalDraft": false,
            "uid": "5e3134ec-4b91-4763-9a78-fb6a06ba12df",
            "siteSettingsId": 19,
            "fieldLayoutId": 1,
            "enabled": true,
            "archived": false,
            "siteId": 1,
            "title": "Bacon",
            "slug": "bacon",
            "uri": "products/bacon",
            "dateCreated": "2024-08-13T16:47:24+00:00",
            "dateUpdated": "2024-08-14T17:03:33+00:00",
            "dateLastMerged": null,
            "dateDeleted": null,
            "deletedWithOwner": null,
            "trashed": false,
            "isNewForSite": false,
            "forceSave": false,
            "canonicalId": 19,
            "cpEditUrl": "https://foster-checkout-v2-dev-site.ddev.site/admin/commerce/products/general/19-bacon",
            "isDraft": false,
            "isRevision": false,
            "isUnpublishedDraft": false,
            "ref": null,
            "status": "live",
            "structureId": null,
            "url": "https://foster-checkout-v2-dev-site.ddev.site/products/bacon"
        },
        "description": "Bacon - Nom Nom Bacon",
        "purchasableId": 21,
        "options": [],
        "sales": []
    }
}

I'm trying to show the LineItem subtotal. But if the purchasable is on promotion then I am showing the regular price subtotal with a strike-through, and then show the subtotal based on the promo price. That's all fine and I've worked around things.

However, I'm building a reactive checkout, and updating the displayed subtotals when the item quantity changes. In order to show the promotional price subtotal I am having to do some twig arithmetic to multiply the promo price (from the lineItem purchasable, by the lineItem quantity).

To show that as a currency, I'm using the commerceCurrency filter. It would be awesome if the promotional subtotal was included in the lineItem object somewhere.

Maybe this is a feature request rather than a bug report. 🤔

Steps to reproduce

  1. Make a purchasable promotable with a promotional price
  2. Add it to your cart

Expected behavior

I'm really not sure if this is by design and perhaps I should be using the LineItem snapshot data in my cart template. But I would expect the LineItem data to contain the promotional pricing if the item is currently on promotion.

Actual behavior

The LineItem data does not contain information about promotional prices for the purchasable

Craft CMS version

5.3.3

Craft Commerce version

5.x-dev

PHP version

8.2.18

Operating system and version

Linux 6.6.32-linuxkit

Database type and version

MySQL 8.0.33

Image driver and version

Imagick 3.7.0 (ImageMagick 6.9.11-60)

Installed plugins and versions

linear[bot] commented 1 month ago

PT-2080 [5.x]: Promotional Prices not populated on LineItem

nfourtythree commented 3 weeks ago

Hi @peteeveleigh

Thank you for the feedback. I have just tried this with a variant that has a promotional price and is on promotion. Using the example templates I added the variant to the cart and I am seeing all the data I expect to:

CleanShot 2024-08-20 at 08 55 22@2x

Also, check an AJAX call to get the cart and the data is there:

        "lineItems": [
            {
                "id": 115759,
                "weight": 0,
                "length": 0,
                "height": 0,
                "width": 0,
                "qty": 1,
                "note": "",
                "privateNote": "",
                "purchasableId": 332,
                "orderId": 604803,
                "lineItemStatusId": null,
                "taxCategoryId": 1,
                "shippingCategoryId": 1,
                "dateCreated": "2024-08-20T07:47:35+00:00",
                "dateUpdated": "2024-08-20T07:49:56+00:00",
                "uid": "2d25bd6a-ba4d-41c4-abe7-17981ed4885c",
                "adjustments": [],
                "description": "Bayview Trail - One Size",
                "options": [],
                "optionsSignature": "d751713988987e9331980363e24189ce",
                "onPromotion": true,
                "price": 459,
                "promotionalPrice": 399.99,
                "salePrice": 399.99,
                "sku": "bayview-trail",
                "total": 399.99,
                "fulfilledTotalQuantity": 0,
                "priceAsCurrency": "$459.00",
                "promotionalPriceAsCurrency": "$399.99",
                "promotionalAmountAsCurrency": "$59.01",
                "salePriceAsCurrency": "$399.99",
                "subtotalAsCurrency": "$399.99",
                "totalAsCurrency": "$399.99",
                "discountAsCurrency": "$0.00",
                "shippingCostAsCurrency": "$0.00",
                "taxAsCurrency": "$0.00",
                "taxIncludedAsCurrency": "$0.00",
                "adjustmentsTotalAsCurrency": "$0.00",
                "subtotal": 399.99,
                "snapshot": {
                    "isDefault": true,
                    "sortOrder": 1,
                    "deletedWithProduct": false,
                    "width": 0,
                    "height": 0,
                    "length": 0,
                    "weight": 0,
                    "freeShipping": false,
                    "promotable": true,
                    "availableForPurchase": true,
                    "minQty": null,
                    "maxQty": null,
                    "inventoryItemId": 74,
                    "inventoryTracked": false,
                    "eagerLoadInfo": null,
                    "id": 332,
                    "tempId": null,
                    "draftId": null,
                    "revisionId": null,
                    "isProvisionalDraft": false,
                    "uid": "ebf26c72-0bbc-4e1d-b3fd-ec17bd1008af",
                    "siteSettingsId": 332,
                    "fieldLayoutId": 5,
                    "enabled": true,
                    "archived": false,
                    "siteId": 1,
                    "title": "One Size",
                    "slug": null,
                    "uri": null,
                    "dateCreated": "2020-11-17T18:37:49+00:00",
                    "dateUpdated": "2024-08-20T07:34:09+00:00",
                    "dateLastMerged": null,
                    "dateDeleted": null,
                    "deletedWithOwner": null,
                    "trashed": false,
                    "isNewForSite": false,
                    "forceSave": false,
                    "fieldId": null,
                    "canonicalId": 332,
                    "cpEditUrl": "#",
                    "isDraft": false,
                    "isRevision": false,
                    "isUnpublishedDraft": false,
                    "ref": null,
                    "status": "enabled",
                    "structureId": null,
                    "url": "https://craft5.ddev.site/product/bayview-trail?variant=332",
                    "isAvailable": true,
                    "isPromotable": true,
                    "price": 459,
                    "promotionalPrice": 399.99,
                    "basePrice": 459,
                    "basePromotionalPrice": 399.99,
                    "onPromotion": true,
                    "salePrice": 399.99,
                    "sku": "bayview-trail",
                    "stock": 0,
                    "product": {
                        "postDate": "2020-11-17T17:18:00+00:00",
                        "expiryDate": null,
                        "typeId": 2,
                        "defaultVariantId": 332,
                        "defaultSku": "bayview-trail",
                        "defaultHeight": 0,
                        "defaultLength": 0,
                        "defaultWidth": 0,
                        "defaultWeight": 0,
                        "taxCategory": null,
                        "name": null,
                        "eagerLoadInfo": null,
                        "id": 331,
                        "tempId": null,
                        "draftId": null,
                        "revisionId": null,
                        "isProvisionalDraft": false,
                        "uid": "a73bfc8f-94fe-43b3-b75a-ccbe6233a0e0",
                        "siteSettingsId": 331,
                        "fieldLayoutId": 4,
                        "enabled": true,
                        "archived": false,
                        "siteId": 1,
                        "title": "Bayview Trail",
                        "slug": "bayview-trail",
                        "uri": "product/bayview-trail",
                        "dateCreated": "2020-11-17T18:37:49+00:00",
                        "dateUpdated": "2024-08-20T07:33:45+00:00",
                        "dateLastMerged": null,
                        "dateDeleted": null,
                        "deletedWithOwner": null,
                        "trashed": false,
                        "isNewForSite": false,
                        "forceSave": false,
                        "canonicalId": 331,
                        "cpEditUrl": "https://craft5.ddev.site/admin/commerce/products/bike/331-bayview-trail?site=default",
                        "isDraft": false,
                        "isRevision": false,
                        "isUnpublishedDraft": false,
                        "ref": null,
                        "status": "live",
                        "structureId": null,
                        "url": "https://craft5.ddev.site/product/bayview-trail"
                    },
                    "description": "Bayview Trail - One Size",
                    "purchasableId": 332,
                    "options": [],
                    "sales": []
                }
            }
        ],

Could you double-check that the variant that you are trying to add is set as promotable?

Thanks

peteeveleigh commented 3 weeks ago

Hey @nfourtythree Thanks for looking into it. Yes I'll definitely test it again later today or tomorrow. It did seem odd that it wasn't working.

nfourtythree commented 1 week ago

Closing this as stale for now, if you have any further issues or updates please reopen.

Thanks!