Realtime pricing

In case you want to display realtime pricing or customer specific pricing, you can make that happen using the twn.*.success hooks:

  • twn.request.success for search
  • twn.request.navigation.success for lister pages

Before you start:

  • Make sure you have an API or endpoint where the prices can be retrieved.
  • Make sure you marked an element in your product tile for the price to show.

Example

This example applies custom prices to search results and lister pages:

  • there is a API POST method available that expects a list of product ids.
  • the price is already on the tile, but can be overwritten
interface RequestSuccessEvent {
  // see https://docs.tweakwise.com/reference/tweakwise-js-typescript for full object typings
  data: IApiNavigation;
}

const formatter = new Intl.NumberFormat("nl-NL", {
  style: "currency",
  currency: "EUR"
});

function tw__applyPricing(data: IApiNavigation){
  
  // gather all product ids, note: these are id's known in tweakwise
  const productids = data.items.map(o => o.itemno);

  // fetch the pricing, batched
  fetch('http://api.yourshop.com/pricing/', {
    method: 'POST', 
    body: JSON.stringify(productids) 
  }).then(function(result: { itemno: string; price: number; }[]){

    // for every result, find the correct tile and overwrite the price
    result.forEach(function(item){
      // the price is formatted using build-in 
      var element = document.querySelector('[data-item-id="#twn-' + itemno + '"] [data-property="price"]');
      if(element){
        element.innerHTML = formatter.format(price);
      }
    });

  });
  
}

window["twn-starter-config"].on = {
  // ...
  'twn.request.success': function (event: RequestSuccessEvent) {
  	tw__applyPricing(event.data);
  },
  'twn.request.navigation.success': function (event: RequestSuccessEvent) {
  	tw__applyPricing(event.data);
  }
});

Good to know

  • It is recommended to batch pricing calls for performance reasons.