📣 The forum has moved to GitHub

We’ve switched to GitHub Discussions as the hub for our community. This will improve the experience of collaborating for everyone, sharing what you’re working on, and discussing ideas for how Swell can be improved. Existing threads will remain open, but new posts are disabled.

Go to GitHub →

I want to update that total discount to the final cart (calculated off the swell) with API

I want to use our own discount system. we will calculate the discount on our end and I want to update that total discount to the final cart. how would I do that?

I have added this discount object. it is coming in cart object too. but the discount is not applied to the final price

{

"account_logged_in": null,

"billing": {},

"shipping": {},

"currency": "USD",

"checkout_id": "7dea72039bd5a63107e87bf7d4ee028d",

"active": true,

"sub_total": 41.95,

"item_discount": 0,

"item_tax": 0,

"item_quantity": 1,

"item_shipment_weight": 0,

"discount_total": 0,

"shipment_price": 0,

"shipment_total": 0,

"tax_total": 0,

"tax_included_total": 0,

"grand_total": 41.95,

"guest": true,

"giftcard_total": 0,

"abandoned_notifications": 0,

"date_abandoned_next": null,

"checkout_url": "https://minoan.swell.store/checkout/7dea72039bd5a63107e87bf7d4ee028d",

"date_created": "2021-10-20T07:36:31.507Z",

"status": "active",

"number": "100445",

"date_abandoned": "2021-10-20T10:36:31.559Z",

"date_updated": "2021-10-20T07:45:00.827Z",

"discounts": [

{

"id": "custom_discount",

"amount": 12.99

}

],

"items": [

{

"product_id": "606dc505ae29d24449ca37bc",

"quantity": 1,

"options": [

{

"id": "60f71408c3494a26511689c1",

"value": "Default Title",

"name": "Title",

"variant": true,

"value_id": "60f71408c3494a26511689c2"

}

],

"metadata": {

"propertyId": "staylokal-capemay",

"brandId": 93,

"images": []

},

"id": "616fc6ffe3624d20a94ad758",

"variant_id": "60f71408c3494a2651168a07",

"price": 41.95,

"orig_price": 41.95,

"delivery": "shipment",

"stock_tracking": true,

"shipment_weight": 0,

"price_total": 41.95,

"discount_total": 0,

"discount_each": 0,

"tax_total": 0,

"tax_each": 0

}

],

"promotion_ids": [],

"shipment_delivery": true,

"shipment_discount": 0,

"taxes": null,

"id": "616fc6ffe3624d20a94ad6f6"

}


as you can see custom discount is coming but discount_total is 0

so where should I add the discount in cart so it will be applied to cart

Answers

  • Hey @Rajan Bharti , is it possible within your flow to create a new discount in Swell, and then apply it to the cart?


    // https://swell.store/docs/api/#create-a-coupon
    await swell.post('/coupons', {...} 
    
    await swell.put('/carts/{id}', { "discounts": [...] }
    
    


    Alternatively, order totals can be calculated dynamically using a real-time order webhook that talks to an external service.

  • Hi Greg,

    Thank you for your response. let me give you a little bit of context for our use case. We are using a custom discount service on our own. where we have the total discount based on the cart on the checkout page. So Now before the payment and shipping detail, we want to apply that discount to the cart.

    What I'm understanding from your suggestion is, when the user is on the checkout page I create a coupon of the total discount which is already calculated via an external service, and then apply it to the cart.

    By this way every time before checkout a coupon will be created and applied to the cart (if any discount is applicable on the cart value).


    I understood we can use the above method to achieve what we are looking for, but I'm wondering if there is any way we can directly inject the discount just like we can add the shipping charges into the cart.

  • Hi There, When can I expect the reply on this thread?

  • Greg
    edited November 2021

    Hey @Rajan Bharti , a coupon can be created dynamically in this way, and this might be useful for tracking purposes. Alternatively, item discounts can be set directly, without a corresponding coupon. This may be preferred with a higher volume of discounts, like in the case of dynamic calculation.

    The following console query can be used to simulate editing the item discount directly:

    /carts/[cart id]/items/[item id]

    {"$set": { "discounts": [{ "amount": 0.5 }] }}

  • Hey @Greg, I considered the first option you mentioned earlier too, but when I dynamically generate the coupon of discount calculated by us and apply it to the cart it is visible on the console on the browser. now anyone can copy that code, abandon the current cart and use it on some other order with less cart value. that can lead us to fraud issues.

    So I was thinking to inject the total discount value into the cart. So I try to update a cart with a discounts array. cart is updated successfully and the response has an array of discounts in it. but it is not reflected in grand total.

    Here is the code snippet I used.

    params = {"$set": {"discounts": [{"amount": discount}]}}
    
    response = requests.put(base_swell_url + 'carts/' + cart_id, json=params)
    if response.status_code == 200 and is_json(response.text):
        return response.json()
    
    Cart Id-> 6190ff5c34fa94654bda1e35
    

    Response


    {

     "account_logged_in": null,

     "billing": {},

     "shipping": {

     "price": 0,

     "service": "general",

     "service_name": "Standard"

     },

     "currency": "USD",

     "checkout_id": "0437f88a3a18441535129aaffb387f1c",

     "active": false,

     "sub_total": 485,

     "item_discount": 0,

     "item_tax": 0,

     "item_quantity": 3,

     "item_shipment_weight": 0,

     "discount_total": 0,

     "shipment_price": 0,

     "shipment_total": 0,

     "tax_total": 0,

     "tax_included_total": 0,

     "grand_total": 485,

     "guest": true,

     "giftcard_total": 0,

     "abandoned_notifications": 0,

     "date_abandoned_next": null,

     "checkout_url": "https://minoan.swell.store/checkout/0437f88a3a18441535129aaffb387f1c",

     "date_created": "2021-11-14T12:21:48.466Z",

     "status": "abandoned",

     "number": "100553",

     "date_abandoned": "2021-11-14T20:36:18.287Z",

     "date_updated": "2021-11-26T07:12:48.801Z",

     "discounts": [

     {

      "amount": 13.33

     }

     ],

     "items": [

     {

      "product_id": "606d98a386b692368f745b01",

      "quantity": 2,

      "options": [

      {

       "id": "60f7521e6d657c1b45e01cf8",

       "value": "Queen",

       "name": "Size",

       "variant": true,

       "value_id": "60f7521e6d657c1b45e01cfa"

      }

      ],

      "metadata": {

      "propertyId": "marram",

      "brandId": 17,

      "images": []

      },

      "id": "6190ff7f34fa94654bda2412",

      "variant_id": "60f7521f6d657c1b45e01d63",

      "price": 145,

      "orig_price": 145,

      "delivery": "shipment",

      "stock_tracking": true,

      "shipment_weight": 0,

      "price_total": 290,

      "discount_total": 0,

      "discount_each": 0,

      "tax_total": 0,

      "tax_each": 0

     },

     {

      "product_id": "606db074baf0bd3da699daf8",

      "quantity": 1,

      "options": [

      {

       "id": "60f72e925dd0a53c7dab2a17",

       "value": "Queen",

       "name": "Size",

       "variant": true,

       "value_id": "60f72e925dd0a53c7dab2a19"

      }

      ],

      "metadata": {

      "propertyId": "marram",

      "brandId": 17,

      "images": []

      },

      "id": "619103e0bd1c30486d481f91",

      "variant_id": "60f72e925dd0a53c7dab2aa7",

      "price": 195,

      "orig_price": 195,

      "delivery": "shipment",

      "stock_tracking": true,

      "shipment_weight": 0,

      "price_total": 195,

      "discount_total": 0,

      "discount_each": 0,

      "tax_total": 0,

      "tax_each": 0

     }

     ],

     "promotion_ids": [],

     "shipment_delivery": true,

     "shipment_discount": 0,

     "taxes": null,

     "abandoned": true,

     "id": "6190ff5c34fa94654bda1e35"

    }

  • Hi @Rajan Bharti , instead of setting the discounts at the top-level of the cart, can you try to set them within the cart items? This should help calculate the totals correctly.

  • Hi @Greg, In our case, it's not necessary to have a discount on all line items in the cart. there can be cases when discounts can be on a group of line items. So it's not possible to add discounts on line items. Is there a way we can add it on cart level?

  • @Rajan Bharti , even though the discount isn't needed on all of the line items in the cart, I'd still recommend spreading out the total discount on the line items, in order to preserve the existing discount logic.

    Alternatively, if the discount really cannot be set at the item level, you can update the formula for how the discount_total is calculated, using a custom field of your choosing:

    PUT /:models/carts/fields/discount_total

    {"$set": { "formula": "item_discount + custom_discount" }}


    Now, when you set the custom_discount field on the cart, it will be included in the discount_total and grand_total calculations, alongside regular discounts.


    Hope this helps!

  • This seems like a better approach. let me try and get back to you if something comes up.

Sign In or Register to comment.