Conditionally mandatory attributes
How to make an attribute mandatory only when another attribute has a specific value.
Many channels require an attribute only in specific cases. For example, a marketplace may require the attribute cell_composition only for products that contain batteries. A conditionally mandatory attribute covers this case: it stays optional by default and becomes mandatory as soon as any of its conditions is met.
How conditions work
Every conditionally mandatory attribute has at least one condition. A condition compares the value of another attribute of the same export template, called the trigger attribute, to a value you define:
| Setting | Description |
|---|---|
| Trigger Attribute | Another attribute of the same export template whose value activates the condition. An attribute can't trigger itself |
| Operator | equals, not equals, is present, or is empty |
| Trigger Value | The value the platform compares the trigger attribute's value to. Not used with the is present and is empty operators |
| Description | An optional hint that tells users where to find or set the trigger attribute, for example, Product > Compliance > Batteries Required |
When any condition of an attribute is met, the attribute becomes mandatory. The attribute stays optional only while none of its conditions are met.
With the is present operator, the condition is met whenever the trigger attribute has any non-empty value. With the is empty operator, the condition is met when the trigger attribute has no value. Neither operator needs a trigger value.
Set an attribute as conditionally mandatory
To make an attribute conditionally mandatory in the Developer Portal, take these steps:
On the export template setup page, go to the section Attributes and analyzer tests, and select the name of the attribute you want to set up.
In Type, select Conditional. The Conditions Editor appears with one empty condition.
Set up the condition by choosing a Trigger Attribute and an Operator. For equals and not equals, also enter a Trigger Value (is present and is empty don't need one). You can also enter a Description to help users find the trigger attribute.
To add another condition, select Add Condition. The attribute becomes mandatory as soon as any condition is met.
Select Save.
Your changes don't become available in the platform until you select Publish. See Publish export template changes.
Manage conditionally mandatory attributes via the API
Attributes in the Export Template Management API carry an optional type field with the values optional, mandatory, or conditionally_mandatory, plus a conditions array. The type field takes precedence over the mandatory boolean when you send both.
In V2, you change an attribute by sending the complete export template with PUT /V2/export-templates/{templateId}, so one request can change several attributes at once. This example makes the existing attribute cell_composition conditionally mandatory. It uses jq to edit the JSON:
# 1. Read the export template
curl "https://export-template-api.productsup.com/V2/export-templates/{templateId}" \
-H "Authorization: Bearer <token>" \
-o template.json
# 2. Build the request body: remove the read-only fields and the logo URL, then
# change the attribute. cell_composition and its condition are example values.
jq 'del(.exportTemplateId, .state, .draftVersion, .unpublishedChanges, .attributeCount, .siteCount, .capabilities, .owner, .createdAt, .updatedAt, .logoUrl)
| .tags |= map(del(.id))
| .customFormFields |= map(del(.id))
| .attributes |= map(del(.id) | .analyzerTests |= map(del(.id, .systemManaged)))
| (.attributes[] | select(.name == "cell_composition")) |= (
.type = "conditionally_mandatory"
| .conditions = [{
"triggerAttribute": "batteries_required",
"triggerValue": "true",
"operator": "equals",
"description": "Product > Compliance > Batteries Required"
}]
)' template.json > payload.json
# 3. Send the complete export template back, with the draft version from step 1
curl -X PUT "https://export-template-api.productsup.com/V2/export-templates/{templateId}" \
-H "Authorization: Bearer <token>" \
-H "Content-Type: application/json" \
-H "If-Match: $(jq .draftVersion template.json)" \
-d @payload.jsonPUT replaces the complete export template. Attributes, tags, and custom form fields that are missing from the request body are deleted. Build the body from the GET response, as in step 2.
If draftVersion changed since step 1, the request returns 409 Conflict. Repeat the three steps.
To change only one attribute without sending the complete export template, send the same type and conditions fields with PATCH /V1/export-templates/{templateId}/attributes/{attributeId}. V2 has no endpoint for a single attribute yet.
Each condition accepts these fields:
| Field | Required | Description |
|---|---|---|
triggerAttribute | Yes | The name of another attribute of the same export template whose value activates the condition |
triggerValue | Conditional | The value the trigger attribute's value is compared to. Required for equals and not_equals; omit or leave empty for is_present and is_empty |
operator | No | equals, not_equals, is_present, or is_empty. Defaults to equals |
description | No | A human-readable hint that tells users where to find or set the trigger attribute |
The API validates these rules:
- An attribute with the type
conditionally_mandatoryneeds at least one condition. - Attributes with the types
optionalandmandatorycan't have conditions. - The trigger attribute must be an existing attribute of the same export template.
- An attribute can't reference itself as a trigger attribute.
- A trigger value is required unless the operator is
is_presentoris_empty.
You can also send the type and conditions fields in the attributes array of POST /V2/export-templates, and with the V1 attribute endpoints, including bulk create and bulk update. All endpoints that return attributes include the type and conditions fields in their responses. See Replace an export template for every field of the PUT request.
Your API changes don't become available in the platform until you publish the export template with POST /V2/export-templates/{templateId}/publish. See Publishing changes.
Related
How is this guide?