Albert Heijn bonus checker!

by lmeurs

JavaScript

/**
 * AH XML sitemaps:
 * - https://www.ah.nl/robots.txt
 *
 * AH Bonus search:
 * - https://www.ah.nl/zoeken/bonus?rq=mondwater&searchType=global
 * - https://www.ah.nl/service/rest/zoeken/bonus?rq=mondwater&searchType=global
 *
 * Jumbo API:
 * - https://www.jumbo.com/INTERSHOP/rest/WFS/Jumbo-Grocery-Site/webapi/productQuickView?id=156072STK
 */

var urls = [
  'https://www.ah.nl/producten/product/wi96398/ah-conference-handperen',
	'https://www.ah.nl/producten/product/wi384325/ah-mondwater-extra-fresh-mint',
  'https://www.ah.nl/producten/product/wi200281/colgate-caries-protection-tandpasta',
  'https://www.ah.nl/producten/product/wi441710/ah-buitenbeentjes-kastanjechampignons',
  'https://www.ah.nl/producten/product/wi421139/ah-snoepfruit-peer',
  'https://www.ah.nl/producten/product/wi160809/lindeman-s-south-africa-sauvignon-blanc-chardonnay',
];

var types = {
  'BONUS': {incl: true, min: 1},
  '2e halve prijs': {incl: false, min: 2},
  '1 + 1 gratis': {incl: false, min: 2},
  '25% korting': {incl: true, min: 1},
  '3 voor 2.00': {incl: false, min: 3},
  '1 EURO KORTING': {incl: true, min: 1},
};

var restUrl = urls[5].replace(/^(https?:\/\/[\w.\-:]+)/i, '$1/service/rest');

$.getJSON(restUrl, function(data) {
//	console.log(data);
  
	var product,
    result;

	$.each(data._embedded.lanes, function(i,lane) {
    if (lane.type === 'ProductDetailLane') {
      product = lane._embedded.items[0]._embedded.product;
      return false;
    }
  });

  if (product) {
    console.log(product);
    result = 'Description: ' + product.description
     + '\nPrice: ' + product.priceLabel.now
     + '\nDiscount: ' + product.discount.label + ' (' + product.discount.type.name + ')';

		console.log(result);
  }
});