Skip to main content
Beyond the platform defaults documented on the Calculated Attributes page, you can create custom calculated attributes for specific business needs. The recipes below are drop-in JSONLogic formulas you can paste into the Value Formula builder.
Description: The percentage discount calculated from compare_at_price to current price. Returns 0 if no discount is available.Use case: Sort products by discount amount or filter for products on sale. Create “Biggest Discounts” collections or exclude heavily discounted items from premium merchandising.
{
  "if": [
    {
      "and": [
        { "var": "_attribute:price_range.to" },
        {
          ">": [
            { "var": "_attribute:price_range.to" },
            { "var": "_attribute:price_range.from" }
          ]
        }
      ]
    },
    {
      "*": [
        {
          "/": [
            {
              "-": [
                { "var": "_attribute:price_range.to" },
                { "var": "_attribute:price_range.from" }
              ]
            },
            { "var": "_attribute:price_range.to" }
          ]
        },
        100
      ]
    },
    0
  ]
}
Attributes: Filterable, Sortable | Value Type: Number (percentage)Example: A product with compare_at_price of 100andcurrentpriceof100 and current price of 75 has a discount percentage of 25.
Description: The total number of variants for this product. Useful for filtering or sorting by product complexity.Use case: Identify products with many options (high variant count) or single-variant products. Filter out products with too many variants from certain collections.
{
  "count": [{ "var": "_raw:raw.variants" }]
}
Attributes: Filterable, Sortable | Value Type: Number (count)Example: A t-shirt available in 5 colors and 4 sizes (20 combinations) has a variant count of 20.
Description: Average profit margin across variants, calculated as (price - cost) / price for each variant with cost data. Excluded from the product if no variants have cost information (the formula evaluates to null).Use case: Sort products by profitability or filter for high-margin items. Create “Best Margin” collections for merchandising or exclude low-margin products from promotional campaigns.
{
  "if": [
    [
      {
        ">": [
          {
            "reduce": [
              {
                "map": [
                  { "var": "_raw:raw.variants" },
                  {
                    "if": [
                      {
                        "and": [
                          { "!=": [{ "var": "cost" }, null] },
                          { ">": [{ "var": "price" }, 0] }
                        ]
                      },
                      1,
                      0
                    ]
                  }
                ]
              },
              {
                "+": [
                  { "var": "accumulator" },
                  { "var": "current" }
                ]
              },
              0
            ]
          },
          0
        ]
      }
    ],
    {
      "/": [
        {
          "reduce": [
            {
              "map": [
                { "var": "_raw:raw.variants" },
                {
                  "if": [
                    {
                      "and": [
                        { "!=": [{ "var": "cost" }, null] },
                        { ">": [{ "var": "price" }, 0] }
                      ]
                    },
                    {
                      "/": [
                        {
                          "-": [
                            { "var": "price" },
                            { "var": "cost" }
                          ]
                        },
                        { "var": "price" }
                      ]
                    },
                    0
                  ]
                }
              ]
            },
            {
              "+": [
                { "var": "accumulator" },
                { "var": "current" }
              ]
            },
            0
          ]
        },
        {
          "reduce": [
            {
              "map": [
                { "var": "_raw:raw.variants" },
                {
                  "if": [
                    {
                      "and": [
                        { "!=": [{ "var": "cost" }, null] },
                        { ">": [{ "var": "price" }, 0] }
                      ]
                    },
                    1,
                    0
                  ]
                }
              ]
            },
            {
              "+": [
                { "var": "accumulator" },
                { "var": "current" }
              ]
            },
            0
          ]
        }
      ]
    },
    null
  ]
}
Attributes: Filterable, Sortable | Value Type: Number (decimal)Example: A product with two variants (price=10,cost=10, cost=5 and price=20,cost=20, cost=10) has an average margin of 0.5 (50%).

See also