Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: added pricebook support for variants and product level records #208

Open
wants to merge 6 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -287,9 +287,9 @@ function enableInstantSearch(config) {
<div class="price">
${isPricingLazyLoad && html`<span class="price-placeholder" data-product-id="${productId}"></span>`}
${!isPricingLazyLoad && html`
${ (hit.displayPrice < hit.price || (hit.promotionalPrice && hit.promotionalPrice < hit.price)) && html`
${ (hit.displayPrice < hit.price || (hit.promotionalPrice && hit.promotionalPrice < hit.price) || (hit.highestPriceBookPrice && hit.highestPriceBookPrice > hit.displayPrice)) && html`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You've made things more clear with the calculateDisplayPrice function, but you have complexified this part.
It's a good occasion to improve the transformItem function and generate prices ready to be displayed:

  • a strikeout price if there is one
  • a displayPrice

So the HTML doesn't need such complex logic

<span class="strike-through list">
<span class="value"> ${hit.currencySymbol} ${hit.price} </span>
<span class="value"> ${hit.currencySymbol} ${((hit.promotionalPrice && hit.promotionalPrice < hit.price) || (hit.displayPrice < hit.price)) ? hit.price : hit.highestPriceBookPrice} </span>
</span>
`}
<span class="sales">
Expand Down Expand Up @@ -332,29 +332,7 @@ function enableInstantSearch(config) {
if (item.price && item.price[algoliaData.currencyCode] !== null) {
item.price = item.price[algoliaData.currencyCode]
}
// If no promotionalPrice, use the pricebooks to display the strikeout price
if (!item.promotionalPrice &&
item.pricebooks &&
item.pricebooks[algoliaData.currencyCode] &&
item.pricebooks[algoliaData.currencyCode].length > 0
) {
const prices = item.pricebooks[algoliaData.currencyCode].filter(pricebook => {
if (pricebook.onlineFrom && pricebook.onlineFrom > Date.now()) {
return false;
}
if (pricebook.onlineTo && pricebook.onlineTo < Date.now()) {
return false;
}
htuzel marked this conversation as resolved.
Show resolved Hide resolved
return true;
}).map(pricebook => pricebook.price);
const maxPrice = prices.reduce((acc, currentValue) => {
return Math.max(acc, currentValue);
});
if (maxPrice > item.price) {
item.promotionalPrice = item.price;
item.price = maxPrice;
}
}

// currency symbol
item.currencySymbol = algoliaData.currencySymbol;
item.quickShowUrl = algoliaData.quickViewUrlBase + '?pid=' + (item.masterID || item.objectID);
Expand Down Expand Up @@ -386,6 +364,10 @@ function enableInstantSearch(config) {
item.displayPrice = price;
item.calloutMsg = calloutMsg;

item.highestPriceBookPrice = item.pricebooks && item.pricebooks[algoliaData.currencyCode] && item.pricebooks[algoliaData.currencyCode].length > 0 ? item.pricebooks[algoliaData.currencyCode].reduce((max, pricebook) => {
return Math.max(max, pricebook.price);
}, 0) : undefined;

// Master-level indexing
if (item.variants) {
item.variants.forEach(variant => {
Expand Down Expand Up @@ -457,6 +439,10 @@ function enableInstantSearch(config) {

item.displayPrice = variantPrice;
item.calloutMsg = variantCalloutMsg;

item.highestPriceBookPrice = selectedVariant.pricebooks && selectedVariant.pricebooks[algoliaData.currencyCode] && selectedVariant.pricebooks[algoliaData.currencyCode].length > 0 ? selectedVariant.pricebooks[algoliaData.currencyCode].reduce((max, pricebook) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't makes things more clear: a 262 characters line with a ternary check at the end that triggers a function is quite hard to process.
You should break it down.

return Math.max(max, pricebook.price);
}, 0) : undefined;
}
}
return item;
Expand Down Expand Up @@ -792,8 +778,20 @@ function calculateDisplayPrice(item) {
}
}

if (item.pricebooks && item.pricebooks[algoliaData.currencyCode] && item.pricebooks[algoliaData.currencyCode].length > 0) {

const lowestPrice = item.pricebooks[algoliaData.currencyCode].reduce((min, pricebook) => {
return Math.min(min, pricebook.price);
}, Infinity);

return {
price: lowestPrice,
calloutMsg: '',
}
}

return {
price: item.price,
price: algoliaData.recordModel === 'master-level' ? (variant.price[algoliaData.currencyCode] ? variant.price[algoliaData.currencyCode] : variant.price) : item.price,
calloutMsg: '',
}
}
Loading