Skip to content

Commit

Permalink
expand variants and currency to show price on products page
Browse files Browse the repository at this point in the history
Merge branch 'fix/show-prices-on-products-page' into 'master'

See merge request schema/origin-storefront!187
  • Loading branch information
sstakhovski committed Jun 22, 2022
2 parents 82cc86d + e6722b6 commit 7689e55
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 32 deletions.
32 changes: 1 addition & 31 deletions components/ProductThumb.vue
Original file line number Diff line number Diff line change
Expand Up @@ -199,37 +199,7 @@ export default {
computed: {
...mapState(['currency', 'cartIsUpdating']),
displayPrice() {
if (this.$swell.currency.list().length > 1) {
return getPriceInCurrency(this.product, this.currency);
}
/* If the product's price is 0, this could mean that
only the options were set a price, so we display the price from
the variant with the cheapest in-stock price so the price reads as
"starting from X" */
if (this.product.variants) {
const cheapestVariantPrice = this.product.variants.results.reduce(
(previousValue, currentValue) => {
const isInStock =
!this.product.stockTracking ||
this.product.stockPurchasable ||
currentValue.stockStatus === 'in_stock';
const priceIsLower =
currentValue.price < previousValue && currentValue.price > 0;
if (isInStock && priceIsLower) return currentValue.price;
return previousValue;
},
Number.MAX_SAFE_INTEGER,
);
if (cheapestVariantPrice !== Number.MAX_SAFE_INTEGER)
return cheapestVariantPrice;
}
return 0;
return getPriceInCurrency(this.product, this.currency);
},
},
Expand Down
7 changes: 6 additions & 1 deletion pages/products/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ export default {
this.products = [...Array(limit).keys()];
// Fetch all products
const products = await $swell.products.list({ page, limit });
const products = await $swell.products.list({
page,
limit,
expand: ['variants'],
$currency: $swell.currency.list().map((currency) => currency.code),
});
// Set component data
if (products) {
Expand Down

0 comments on commit 7689e55

Please sign in to comment.