@ecomplus/utils
Version:
JS utility functions to E-Com Plus (not only) related apps
74 lines (73 loc) • 2.71 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
require("core-js/modules/es.symbol.js");
require("core-js/modules/es.symbol.description.js");
require("core-js/modules/es.object.to-string.js");
require("core-js/modules/es.symbol.iterator.js");
require("core-js/modules/es.array.iterator.js");
require("core-js/modules/es.string.iterator.js");
require("core-js/modules/web.dom-collections.iterator.js");
function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); }
/**
* @method
* @memberof ecomUtils
* @name onPromotion
* @description Check if item has promotional price.
* @param {Object.<string, *>} product - Item (product or variation) body object
* @returns {boolean}
*
* @example
* // Simple test with no promotion date range
* // Full object ref.: https://developers.e-com.plus/docs/api/#/store/products/
* ecomUtils.onPromotion({ sku: 'TEST', name: 'Test', price: 140.56 })
* // => false
* ecomUtils.onPromotion({ price: 100, base_price: 110 })
* // => true
* ecomUtils.onPromotion({ price: 190, base_price: 170 })
* // => false
*
* @example
* // With date range
* const product = { sku: 'abc', price: 20.9, base_price: 30.9, price_effective_date: {} }
* product.price_effective_date.start = '2019-06-01T16:03:45.035Z'
* ecomUtils.onPromotion(product)
* // => true
* product.price_effective_date.end = '2019-06-10T16:03:45.035Z'
* ecomUtils.onPromotion(product)
* // => false
* product.price_effective_date.end = '2021-08-12T00:00:00.000Z'
* ecomUtils.onPromotion(product)
* // => true
* product.price_effective_date.start = '2021-01-01T00:00:00.000Z'
* ecomUtils.onPromotion(product)
* // => false
*/
var onPromotion = function onPromotion(product) {
if (_typeof(product) !== 'object' || product === null) {
// prevent fatal error
console.error(new Error('`product` must be an object'));
return false;
}
var promoDates = product.price_effective_date;
if (promoDates) {
var now = new Date();
if (promoDates.start) {
// start date and time in ISO 8601
if (new Date(promoDates.start) > now) {
return false;
}
}
if (promoDates.end) {
// promotion end date and time in ISO 8601
if (new Date(promoDates.end) < now) {
return false;
}
}
}
// default to no promotion
return !!(product.base_price > product.price);
};
var _default = exports.default = onPromotion;