SFRA Server-side Javascript - Source: app_storefront_base/cartridge/scripts/helpers/structuredDataHelper.js menu

SFRA / Server-side JS / Source: app_storefront_base/cartridge/scripts/helpers/structuredDataHelper.js

  1. 'use strict';
  2. /**
  3. * Get product schema information
  4. * @param {Object} product - Product Object
  5. *
  6. * @returns {Object} - Product Schema object
  7. */
  8. function getProductSchema(product) {
  9. var schema = {
  10. '@context': 'http://schema.org/',
  11. '@type': 'Product',
  12. name: product.productName,
  13. description: product.shortDescription,
  14. mpn: product.id,
  15. sku: product.id
  16. };
  17. if (product.brand) {
  18. schema.brand = {
  19. '@type': 'Thing',
  20. name: product.brand
  21. };
  22. }
  23. if (product.images && product.images.large) {
  24. schema.image = [];
  25. product.images.large.forEach(function (image) {
  26. schema.image.push(image.absURL);
  27. });
  28. }
  29. if (product.price) {
  30. schema.offers = {
  31. url: require('dw/web/URLUtils').url('Product-Show', 'pid', product.id)
  32. };
  33. if (product.price.type === 'range') {
  34. schema.offers['@type'] = 'AggregateOffer';
  35. schema.offers.priceCurrency = product.price.currency;
  36. schema.offers.lowprice = product.price.min;
  37. schema.offers.highprice = product.price.max;
  38. } else {
  39. schema.offers['@type'] = 'Offer';
  40. if (product.price.sales) {
  41. schema.offers.priceCurrency = product.price.sales.currency;
  42. schema.offers.price = product.price.sales.decimalPrice;
  43. } else if (product.price.list) {
  44. schema.offers.priceCurrency = product.price.list.currency;
  45. schema.offers.price = product.price.list.decimalPrice;
  46. }
  47. }
  48. schema.offers.availability = 'http://schema.org/InStock';
  49. if (product.available) {
  50. if (product.availability && product.availability.messages[0] === require('dw/web/Resource').msg('label.preorder', 'common', null)) {
  51. schema.offers.availability = 'http://schema.org/PreOrder';
  52. }
  53. } else {
  54. schema.offers.availability = 'http://schema.org/OutOfStock';
  55. }
  56. }
  57. return schema;
  58. }
  59. /**
  60. * Get product listing page schema information
  61. * @param {List} productIds - Product Ids
  62. *
  63. * @returns {Object} - Listing Schema object
  64. */
  65. function getListingPageSchema(productIds) {
  66. var schema = {
  67. '@context': 'http://schema.org/',
  68. '@type': 'ItemList',
  69. itemListElement: []
  70. };
  71. Object.keys(productIds).forEach(function (item) {
  72. var productID = productIds[item].productID;
  73. schema.itemListElement.push({
  74. '@type': 'ListItem',
  75. position: Number(item) + 1,
  76. url: require('dw/web/URLUtils').abs('Product-Show', 'pid', productID).toString()
  77. });
  78. });
  79. return schema;
  80. }
  81. module.exports = {
  82. getProductSchema: getProductSchema,
  83. getListingPageSchema: getListingPageSchema
  84. };