Conditions

Conditions are one of the most powerful and complicated features of the Decimal platform. Conditions are what allow you to filter your rates based upon the product and cart properties. First, you need to know what properties are available to you. The order object looks like this:

{
    "cart": {
        "price": 12000, //cents
        "weight": 1587.57 //grams (3.5lbs)
        "currency": "USD"
    },
    "items": {
        {
            "title": "Cookie Monster Gift Basket",
            "quantity": 1,
            "sku": "ABC1",
            "weight": 1360.78,
            "price" 9900,
            "tags": ["Gift-Basket"]
        },
        {
            "title": "This one goes up to 11 // T-Shirt // Small",
            "quantity": 2,
            "sku" "TEE3",
            "weight": 113.4,
            "tags": ["CA"]
        }
    }
}

Operators

All of these properties can be used to place conditions on. For example, if you would like to take the Free Shipping rate shown in Free and Flat Rates and have it only for carts over $100:

"cheapest": {
    "total_price": 0,
    "display_text": "Free Shipping",
    "condition": {
        "on": "cart.price",
        "operator": "EQUALS_GREATER",
        "value": 10000
    }
}

The "on" property says what property on the order object for this condition to look at, which in this case is the cart price, which in the above example is 12000 ($120.00). The operator is how we want this to be compared; in this case we are saying, 'equal to or greater than'.

The available operators are:

"EQUALS",
"NOT_EQUALS"
"GREATER"
"LESS"
"EQUALS_GREATER"
"EQUALS_LESS"
"INCLUDES"
"NOT_INCLUDES"

An operator of "GREATER" and a value of 9999 would have been the same as above. EQUALS and a value of 10000 would only show this rate if the price is exactly $100.00 and NOT_EQUALS would only show the rate if it is not $100.00.

Sets

You may also want a condition on the products, which can cause a bit of an issue, since the number of products is variable. This is where a Set comes in. Let's say you only want the Free Shipping to show if the cart includes a Gift Basket. If all Gift Baskets have the Product Type as "Gift Basket", then this is what the condition would look like:

"cheapest": {
    "total_price": 0,
    "display_text": "Free Shipping",
    "condition": {
        "on": "cart.price",
        "operator": "EQUALS_GREATER",
        "value": 10000,
        "and": [
            {
                "set": {
                    "on": "items",
                    "operator": "ALL"
                },
                "on": "type",
                "operator": "EQUALS",
                "value": "Gift Basket"
            }
        ]
    }
}

It is taking the original conditon and adding another condition on the "and" poperty. This means the condition will only be true if the cart price is above $100 AND the type of all products in the cart equals Gift Basket. However, let's say you don't have a Product type of Gift Basket, or you want to have this rule apply to items with many types. Well, you can simply create a new tag! Let's say you gave all the products that you wanted this rule to apply to, the tag "Gift Basket" then the condition would be this:

"cheapest": {
    "total_price": 0,
    "display_text": "Free Shipping",
    "condition": {
        "on": "cart.price",
        "operator": "EQUALS_GREATER",
        "value": 10000,
        "and": [
            {
                "set": {
                    "on": "items",
                    "operator": "ALL"
                },
                "on": "tags",
                "operator": "INCLUDES",
                "value": "Gift Basket"
            }
        ]
    }
}

Here we are checking that the list of product tags includes a tag called "Gift Basket".

The set operators are:

"ALL"
"SOME"
"NONE"

results matching ""

    No results matching ""