Stacking rules (CatalogPricingRule) for discounts

I am looking for confirmation that the API supports the option to create a CatalogPricingRule with multiple criteria. For example a discount that applies for only certain customer groups and on only certain items.

Within the Square Dashboard interface (not API) the discount creation allows for the selection of one rule type per discount (Item/Category OR Quantity OR Customer Group). I would like to create a discount that has multiple/stacking rules so that it is based on both Items and Customer Group.

I tried creating this via the API explorer and it did appear to work, but I am unable to test it on the Square POS (using Sandbox) so I cannot confirm that it works. Furthermore, the discount does not appear to have multiple rules showing up on the Square Dashboard when I view it that way, though I realize that there might be a limitation with how it is displayed.

Here is a picture of the API response to the Upsert Catalog Object request:

  1. Does this achieve the desired outcome of creating a rule for use with a discount that will automatically apply on a sale when the eligible products and eligible customers are added?
  2. I assume that the associated discount should not be edited via the Square Dashboard, is that correct?

Thank you!

:wave: Yes, this is the correct assumption with the pricing rule you created. Since your using sandbox you can test this bay creating an order with the Orders API and paying for it using the Payments API with our test values. :slightly_smiling_face:

1 Like

Hi, I am a beginner with my Square POS and would really like to implement this code for my business, but do not know really where to start. Any help would be greatly appreciated!

Have you worked with APIs before? Square lets developers build an ecosystem for businesses to seamlessly accept payments online, in store, or on the go through Payments and eCommerce APIs. Our API’s are best utilized by someone with API coding experience or working directly with a developer.

If you don’t have experience in building API tools and would like to accept payments online, we recommend connecting your Square account with one of our eCommerce partners. :slightly_smiling_face:

Hey,

Im trying to progamatically add an automatic discount to Square that includes multiple options (customer group and product category) where have I gone wrong?

'import { axios } from “@pipedream/platform”;

export default defineComponent({
props: {
square: {
type: “app”,
app: “square”,
},
discountName: {
type: “string”,
label: “Discount Name”,
description: “The name of the discount to create”,
default: “Active Member”,
},
discountPercentage: {
type: “integer”,
label: “Discount Percentage”,
description: “The percentage of the discount”,
default: 5,
},
categoryId: {
type: “string”,
label: “Category ID”,
description: “The ID of the category to which the discount should apply”,
},
customerGroupId: {
type: “string”,
label: “Customer Group ID”,
description: “The ID of the customer group to apply the discount to”,
},
},
async run({ steps, $ }) {
// Generate a unique idempotency key to prevent duplicate requests
const idempotencyKey = unique-id-${Date.now()};

// Prepare the catalog objects for upsert
const discountObject = {
  id: "#Discount",
  type: "DISCOUNT",
  discount_data: {
    name: this.discountName,
    discount_type: "FIXED_PERCENTAGE",
    percentage: this.discountPercentage.toString(),
  },
  present_at_all_locations: true,
};

const productSetObject = {
  id: "#ProductSet",
  type: "PRODUCT_SET",
  product_set_data: {
    name: "Specific Category Products",
    category_ids_any: [this.categoryId],
  },
  present_at_all_locations: true,
};

const pricingRuleObject = {
  id: "#PricingRule",
  type: "PRICING_RULE",
  pricing_rule_data: {
    name: `${this.discountName} Rule`,
    discount_id: "#Discount",
    match_products_id: "#ProductSet",
    customer_group_ids_any: [this.customerGroupId],
    application_mode: "AUTOMATIC",
  },
  present_at_all_locations: true,
};

// Prepare the batch upsert data
const batchUpsertData = {
  idempotency_key: idempotencyKey,
  batches: [
    {
      objects: [discountObject, productSetObject, pricingRuleObject],
    },
  ],
};

try {
  // Make the BatchUpsertCatalogObjects API call
  const response = await axios($, {
    method: "POST",
    url: `https://connect.squareup.com/v2/catalog/batch-upsert`,
    headers: {
      Authorization: `Bearer ${this.square.$auth.oauth_access_token}`,
      "Content-Type": "application/json",
      "Square-Version": "2023-10-18",
    },
    data: batchUpsertData,
  });

  // Log the response for debugging
  console.log("Batch Upsert Response:", response.data);

  return response.data;
} catch (error) {
  // Log detailed error information
  console.error("Error creating automatic discount:", error.response?.data || error.message);
  throw new Error("Failed to create automatic discount.");
}

},
});’

Hi @Brewed-Online ,
I think it might take a while to debug your implementation. I feel it would be best that we just start a new topic.

It’s hard to say, since you’ve designed the code to do everything at once.
I think while you’re still tinkering an developing, try making each Catalog API object one at a time with individual API calls. Try creating them with our API Explorer or Postman first, then see if you can recreate the same objects in your application. Then try and put it all together.

If that does not work, please share your application id, so I can review your API Logs.

All good bud, I worked it out. I’ll hopefully be releasing it as a super simple cheap app in the next week as this is something I’ve been missing for some time now, I’m sure I’m not the only one.

Feel free to consider publishing your app to the Square App Marketplace.