cocoPIM

Go further · Module 12 bis

Automate with rules

A rule writes a value to your product records when a condition is true: a description filled in, a product added to a category, an attribute updated. You choose the conditions, the actions and when the rule applies.

12 min read

What is a rule for?

A rule writes a value to your product records when a condition is true. It fills an empty description, adds a product to a category or updates an attribute. You decide the conditions, the actions and when it applies.

Calculated fields and AI fields and rules look alike, but their roles differ. A calculated field is read-only and is recalculated on every change. A rule writes a value that you can still edit by hand.

Anatomy of a rule

A rule reads like a sentence, in three blocks:

  • IF: the conditions to meet.
  • THEN: the actions to run when the conditions are true.
  • ELSE: the actions to run when they are false. This block is optional.

A rule without a condition applies to all products. The editor reminds you of this under the IF block.

You do not have to open each rule to understand it. In the list, a sentence sums up each rule under its name. At the top of the editor, the How this rule reads card draws it: the If block, then the Then and Otherwise branches side by side. The diagram follows the form at every change, even before you save.

List of the catalog rules: priority, name, summary sentence, state
Each rule reads as one sentence under its name. The order of the list is the order of execution.

Each item a rule cites is shown by its name, followed by its code in brackets, for example “Scented candles (scented_candles)”. The code is the one you find in your imports and exports.

Editor of a rule: the How this rule reads card, then the General, IF and THEN blocks of the form
At the top, the diagram of the rule. Below, the form that defines it.

The IF block

Conditions can cover an attribute, the family, the categories, the channel or the language. You choose whether the rule applies when all (AND) the conditions are true, or when at least one (OR) is true.

You can group conditions together, and a group has its own AND or OR choice. A group holds only one level: no group inside a group.

The THEN and ELSE blocks

The order of actions matters. If two actions write to the same cell, the last one wins. An action does one of two things:

  • Write a value to an attribute of the product.
  • Add to categories: the product joins one or more categories. A rule never removes a product from a category.

Write a value

To write a value, you choose the target attribute, then the source of the value:

  • Fixed value: a value entered once and for all.
  • Copy of an attribute: the value of another attribute is copied. If it is empty, nothing is written.
  • Expression: a value calculated from your attributes, in Template or Formula mode, with the same syntax as calculated fields.

Expressions are available for text and number fields.

The three ways to write

Each action chooses a write mode. This is the key point: the default mode is Fill if empty.

Fill if empty

The rule writes only when the field is empty. It never touches an existing value. This is the safest mode, and the one the editor proposes by default.

Replace

The rule writes every time and overwrites the existing value. It also overwrites what was just entered by hand. To protect your manual entries, keep “Fill if empty”.

Add to existing

The rule completes a list or a text, without repeating what is already there. In a list, it adds the missing items. In a text, it appends the fragment with the chosen separator.

Channel and language

An attribute can vary by channel and by language. A rule applies to each relevant channel and language, and writes each cell separately.

By default, an action reads and writes the cell being evaluated. You can also pin a specific channel or language: the rule then writes only in that cell.

An action that writes a non-translated attribute cannot depend on a “per language” condition unless the language is specified. The editor flags this.

The order of rules

Rules run by increasing priority: rule 10 runs before rule 20. Each rule runs only once. It sees what earlier rules have written, not what later ones will write.

The editor warns you when a rule writes an attribute that an earlier rule already read. You can then change the priority.

When rules apply

  • On each product save: active rules apply to every product record saved.
  • On existing products: run Execute on the whole catalog, on a selection or on filters.

Enabling a rule does not change your existing products. For those, run Execute.

After a bulk edit of an attribute, the enabled rules run on their own, once, when the edit is over, on the products it changed. That run shows up in the latest runs.

The same goes for a product import: the rules run once, when the import is over, on the products it created or updated.

Connectors and the API do not trigger rules for now. After an inbound sync, run Execute.

Only one execution or simulation runs at a time in a space. Variants inherit from their master product.

Try before you enable

A rule starts disabled. Test it and simulate it before you enable it.

  1. Test on a product

    Choose a product. The editor shows each condition, true or false, with the value it read, and what each action would do. A test writes nothing.

  2. Simulate

    The simulation produces a report of what would change, across the whole catalog or a selection. It writes nothing.

  3. Enable

    Once the result is right, enable the rule. It will apply to the next product saves.

Run a simulation or an execution

Simulate and Execute open the same window. There you choose the mode, the rules and the products concerned.

  • Mode: Simulate (no write) or Execute (writes).
  • Rules: all active rules, or a selection. Disabled rules run only in a simulation.
  • Products: the whole catalog, the current selection or the products in the current filter.

An execution asks for confirmation with the number of products concerned. A simulation changes nothing: it produces a report. From that report, Apply these changes runs the matching execution. Simulate first: an execution cannot be undone.

Find out what a rule did

Every value written by a rule stays traceable:

  • The Rule badge on a field of the product record. Click Why this value? to see the rule, when it ran and its write mode.
  • The rules history of a product record, which lists every value written by rules on that product.
  • The history of the rule in its editor.
  • The Rules tab of the History page, for the whole space.

Values written by rules are kept for 12 months. The detail of a simulation is kept for 7 days.

Three step-by-step examples

Fill the description from the short description

This example needs no condition: the rule applies to all products.

  • IF: no condition.
  • THEN: write Description, value = copy of Short description, mode Fill if empty.
  • ELSE: nothing.

There is no need to add a “description is empty” condition. The Fill if empty mode handles it.

Categorize by brand

  • IF: the Brand is equal to “Acme”.
  • THEN: add to categories “Candles”. The category is added on each channel whose tree contains it.
  • ELSE: nothing.

Flag as incomplete when the price is empty

Completeness is calculated by CocoPIM. To drive it with a rule, create a Yes / No attribute, To complete.

  • IF: the Price is empty.
  • THEN: To complete = Yes, mode Replace.
  • ELSE: To complete = No, mode Replace.

You can then filter the product list on the To complete attribute. The brands and names in these examples are invented.

Limits to know

  • A rule does not run at a chosen date or time. A date condition compares against a fixed date.
  • A rule does not read another product.
  • A space can hold up to 100 rules.
  • An execution cannot be cancelled: simulate first.
  • A rule does not write calculated fields, media or links between products.