-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
61 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
const onPromotion = body => { | ||
let promoDates = body.price_effective_date | ||
if (promoDates) { | ||
let 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 !!(body.base_price > body.price) | ||
} | ||
|
||
/** | ||
* @method | ||
* @memberof ecomUtils | ||
* @name onPromotion | ||
* @description Check if item has promotional price. | ||
* @param {object} body - Object (product or variation) body | ||
* @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 | ||
* | ||
* @example | ||
* // Importing this method standalone | ||
* import onPromotion from '@ecomplus/utils/dist/methods/onPromotion' | ||
*/ | ||
|
||
export default onPromotion |