Code examples

Below are some examples of custom pricing rules that show how to use our Python custom rule engine.

Please note that your code should be written in a robust way that accounts for missing / incomplete values. For example, if your code relies on a specific attribute, do not assume that attribute always exists for every product. Instead, look for that attribute, and use it if it's available, but also add some default values in case it's missing.

Custom pricing rules

Competitor price with custom variable

ctx = get_context()
vars = ctx.get_variables()
percent = vars['percent']
competitor = vars['competitor_name']
competitors = ctx.get_competitor_prices()
recommendation = ctx.get_shelf_price()
ctx.set_explanation('-')
for i in range(len(competitors)):
    if attrs[i].get('competitor') == competitor:
        recommendation = attrs[i].get('price') * float(percent)
        ctx.set_explanation('change price to {}% of competitor {}'.format(percent, competitor))
ctx.set_recommendation(recommendation)

Explanation

The above example generates a recommendation based on a percentage of a specific competitor price, with default to current shelf price.

  • The competitor name and percent value are taken from variables defined in the pricing group in lines 2-4.
  • We set some defaults for recommendation value and pricing explanation in lines 6-7.
  • We iterate over the list of competitor prices, and if the right competitor is found, we set the recommendation to a percentage of that competitor's price

Attribute-based recommendation

ctx = get_context()
recommendation = ctx.get_shelf_price()
ctx.set_explanation('-')
attrs = ctx.get_attrs()
for i in range(len(attrs)):
  if attrs[i].get('name') == 'base_cost':
    recommendation = float(attrs[i].get('value')) + 15
    ctx.set_explanation('set to cost + 15EU')
    ctx.set_recommendation(recommendation)

Explanation

The above example generates a recommendation based on the value of an imaginary attribute named base_cost with an added fixed value of 15.

  • We set some defaults for recommendation value and pricing explanation in lines 2-3.
  • We iterate over the list of product attributes, and if we find the base_cost attribute, we use its value to set the recommendation

After pricing - Post Processor : Pricing of the main product leads to pricing of the other channels' products.

ctx = get_context()
channels = ctx.get_client_channels()
product_id = ctx.get_product_id()
current_channel = ctx.get_channel()
for channel in channels:
    if current_channel != channel:
        ctx.send_to_pricing_by_channel(product_id, channel ) 

Explanation

The above example leads to pricing of other channels' products, after a pricing of specific product.

  • We get the list of all client channels.
  • We get current channel name.
  • We get current product_id.
  • We iterate over the list of channels, and send other channels' products to pricing.