πŸ“£ 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 β†’

Programatically Setting Tax with API

For various reasons, we need to set the Tax for the cart items via the API, as we have specific tax logic we need to create for each customer. We are trying to do that using the API. Sample Code is below. We are able to easily update the QTY of items in a cart and other cart items via the sample code, but we are unable to update "tax_total" and "tax_each" for the items field. How are these set? Are these set automatically based on the value of "items.taxes.id" in the items.taxes array? If so, how do we override this and set the "tax_total" programatically for each cart?


//map over item object from cart that we received from another async function & change values

const newObject = itemsArray.map((item) => {

console.log ("item", item)

return {

...item,

//tax_total: 10, //changing tax_total does not work

//tax_each: 10/item.quantity //we assume this is done automatically

quantity: item.quantity + 1

};

});

const upCartSwell = async function (cart_id) {

return await new Promise((resolve, reject) => {

const client = swell.init(SWELLSTORE, SWELLKEY);

client.put('/carts/{id}', {

id: cart_id,

items: newObject,

item_tax_included: true


})

.then((result) => {

client.close()

resolve(result);

})

.catch(err => {

reject(err);

});

});

}

const swellCart = await upCartSwell(cart_id)

return swellCart;

Answers

  • Hey @Adriana Fruchter , that's right; taxes are automatically calculated at checkout based on the various rules and rates configured. When defining taxes manually, you can set the field taxes_fixed to true, in order to prevent the system from overriding a custom taxes array. The taxes can then be added via item.taxes = [{ name: 'vat', amount: 5.5 }]


    By the way, the tax fields and formulas can be viewed (and updated) at /:models/orders.

  • Adriana Fruchter
    edited November 2021

    Thanks, but a few things. We need to set taxes on the cart model, not the order model, as this is for a cart, which we then turn into an order. As regards to the cart model, where is this field, taxes_fixed. It is not anywhere in the cart model https://swell.store/docs/api/#the-cart-model. Also, there is no such thing as a name on object on item.taxes. The only fields on the item tax for the cart or the order, for that matter, are: item.taxes.id and item.taxes.amount. Where is there a item.taxes.name? I assume you meant the taxes array on the cart object, but this requires an id of an existing tax object in the admin, so just adding a name doesn't do a thing. Is there system id for the VAT name? If so what is it? You can't save just a name on the taxes object. It requires an ID. Please clarify your fix, as it doesn't work as suggested.

  • Adriana Fruchter
    edited November 2021

    Can you please post an actual API call, as your solution doesn't work. There is no name key on item.taxes, and setting item.taxes requires an id of an existing tax object, which if it exists gets automatically applied to the order, overriding any custom tax. There is a name key on the taxes object, but once again that requires an id. Please explain exactly how you call the API to create custom taxes and what the settings need to be on the backend.

  • There apparently is a tax_each and tax_total in the items array for the cart. Why can't these be set manually? Setting them seems to do nothing even if taxes_fixed = true. so there doesn't seem to be any way to set taxes programatically, as taxes_fixed = true seems to still rely on some backend Tax Rule, which would defeat the whole purpose.

  • @Adriana Fruchter , the formulas used to calculate the various tax fields can be viewed in /:models/carts. As you can see, tax_total is calculated by summing the tax amounts of the cart items, by default. By including tax objects on the items, the cart will calculate the corresponding totals. The 'name' attribute ordinarily refers to the name of the tax rule, but the 'amount' is what gets included in the calculation.

    I'd recommend using the above approach, but if tax_total needs to be set directly, the field's formula can be updated, possibly to reference a custom field you define:


    PUT /:models/carts/fields/items/fields/tax_total

    Β { "formula": "custom_tax"}


    Now when setting a 'custom_tax' attribute on the cart item, the tax total will be updated accordingly.

  • Adriana Fruchter
    edited November 2021

    OK, thanks. That didn't work. What worked for us (haven't completely tested it, but seems to work on a few test cases) is as follows:

    1. Delete all tax rules from Swell backend.
    2. On the cart, set taxes_fixed = TRUE
    3. On the cart items.taxes ARRAY, set items.taxes.amount to a Fixed Dollar Amount (can be figured out in the code itself with a custom formula on a per item basis, so in theory there could be a different taxes per item ). So the taxes.amount looks like: taxes: [{amount: taxAmount}] and the taxAmount is just a variable we calculate using our own backend tax calculator.

    In case anyone ever needs this, our cart items looks sort of like this:

    //loop thru cart items for a specific cart and set the taxAmount

    const updateCartItemsSwell = async function (cartItems) {

    const newObject = cartItems.map((item) => {

    const taxAmount = await calculateCustomTax (item) // a custom function we use to calc tax

    return {

    ...item,

    taxes: [{amount: taxAmount}], //required this eliminates the tax object if assigned

    };

    });

    return newObject

    }

Sign In or Register to comment.