Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -18,7 +18,6 @@
import com.adobe.cq.commerce.core.components.models.retriever.AbstractProductRetriever;
import com.adobe.cq.commerce.magento.graphql.ProductInterfaceQuery;
import com.adobe.cq.commerce.magento.graphql.ProductInterfaceQueryDefinition;
import com.adobe.cq.commerce.magento.graphql.ProductPricesQueryDefinition;
import com.adobe.cq.commerce.magento.graphql.SimpleProductQuery;
import com.adobe.cq.commerce.magento.graphql.SimpleProductQueryDefinition;

Expand All @@ -29,20 +28,13 @@ class ProductRetriever extends AbstractProductRetriever {
}

/* --- GraphQL queries --- */
private ProductPricesQueryDefinition generateOldPriceQuery() {
return q -> q
.regularPrice(rp -> rp
.amount(a -> a
.currency()
.value()));
}

private SimpleProductQueryDefinition generateSimpleProductQuery() {
return (SimpleProductQuery q) -> {
q.sku()
.name()
.image(i -> i.label().url())
.price(generateOldPriceQuery());
.priceRange(r -> r
.minimumPrice(generatePriceQuery()));

// Apply product variant query hook
if (variantQueryHook != null) {
Expand All @@ -57,10 +49,12 @@ protected ProductInterfaceQueryDefinition generateProductQuery() {
q.name()
.image(i -> i.url())
.urlKey()
.price(generateOldPriceQuery())
.onConfigurableProduct(cp -> cp
.variants(v -> v
.product(generateSimpleProductQuery())));
.product(generateSimpleProductQuery()))
.priceRange(r -> r
.maximumPrice(generatePriceQuery())
.minimumPrice(generatePriceQuery())));

// Apply product query hook
if (productQueryHook != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
import org.apache.sling.models.annotations.injectorspecific.Self;

import com.adobe.cq.commerce.core.components.client.MagentoGraphqlClient;
import com.adobe.cq.commerce.core.components.internal.models.v1.Utils;
import com.adobe.cq.commerce.core.components.internal.models.v1.common.PriceImpl;
import com.adobe.cq.commerce.core.components.models.common.Price;
import com.adobe.cq.commerce.core.components.models.productteaser.ProductTeaser;
import com.adobe.cq.commerce.core.components.models.retriever.AbstractProductRetriever;
import com.adobe.cq.commerce.core.components.utils.SiteNavigation;
Expand Down Expand Up @@ -64,8 +65,12 @@ public class ProductTeaserImpl implements ProductTeaser {
private Pair<String, String> combinedSku;
private AbstractProductRetriever productRetriever;

private Locale locale;

@PostConstruct
protected void initModel() {
locale = currentPage.getLanguage(false);

productPage = SiteNavigation.getProductPage(currentPage);
if (productPage == null) {
productPage = currentPage;
Expand Down Expand Up @@ -116,13 +121,14 @@ public String getCallToAction() {
return properties.get("cta", null);
}

@Override
public Price getPriceRange() {
return new PriceImpl(getProduct().getPriceRange(), locale);
}

@Override
public String getFormattedPrice() {
Double price = getPrice();
if (price != null) {
return getPriceFormatter().format(price);
}
return null;
return getPriceRange().getFormattedFinalPrice();
}

@Override
Expand All @@ -148,20 +154,6 @@ public String getImage() {
return null;
}

private String getCurrency() {
if (getProduct() != null) {
return getProduct().getPrice().getRegularPrice().getAmount().getCurrency().toString();
}
return null;
}

private Double getPrice() {
if (getProduct() != null) {
return getProduct().getPrice().getRegularPrice().getAmount().getValue();
}
return null;
}

private SimpleProduct findVariant(ConfigurableProduct configurableProduct, String variantSku) {
List<ConfigurableVariant> variants = configurableProduct.getVariants();
if (variants == null || variants.isEmpty()) {
Expand All @@ -170,15 +162,4 @@ private SimpleProduct findVariant(ConfigurableProduct configurableProduct, Strin
return variants.stream().map(v -> v.getProduct()).filter(sp -> variantSku.equals(sp.getSku())).findFirst().orElse(null);
}

private NumberFormat getPriceFormatter() {
if (priceFormatter == null) {
// Initialize NumberFormatter with locale from current page.
// Alternatively, the locale can potentially be retrieved via
// the storeConfig query introduced with Magento 2.3.1
Locale locale = currentPage.getLanguage(false);
priceFormatter = Utils.buildPriceFormatter(locale, getProduct() != null ? getCurrency() : null);
}
return priceFormatter;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import org.osgi.annotation.versioning.ProviderType;

import com.adobe.cq.commerce.core.components.models.common.Price;
import com.adobe.cq.commerce.core.components.models.retriever.AbstractProductRetriever;

/**
Expand All @@ -32,10 +33,9 @@ public interface ProductTeaser {
String getName();

/**
* Returns formatted price string with currency for this {@code ProductTeaser}
*
* @return formatted price string with currency or {@code null}
* @deprecated Please use getPriceRange() instead.
*/
@Deprecated
String getFormattedPrice();

/**
Expand Down Expand Up @@ -67,6 +67,13 @@ public interface ProductTeaser {
*/
String getCallToAction();

/**
* Returns the price range.
*
* @return Price range instance.
*/
Price getPriceRange();

/**
* Returns in instance of the product retriever for fetching product data via GraphQL.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void verifyProduct(String resourcePath) throws Exception {
Assert.assertEquals(siteNavigation.toPageUrl(productPage, product.getUrlKey()), productTeaser.getUrl());

NumberFormat priceFormatter = NumberFormat.getCurrencyInstance(Locale.US);
Money amount = product.getPrice().getRegularPrice().getAmount();
Money amount = product.getPriceRange().getMinimumPrice().getFinalPrice();
priceFormatter.setCurrency(Currency.getInstance(amount.getCurrency().toString()));
Assert.assertEquals(priceFormatter.format(amount.getValue()), productTeaser.getFormattedPrice());

Expand Down Expand Up @@ -159,7 +159,7 @@ public void verifyProductVariant() throws Exception {
Assert.assertEquals(siteNavigation.toProductUrl(productPage, product.getUrlKey(), variantSku), productTeaser.getUrl());

NumberFormat priceFormatter = NumberFormat.getCurrencyInstance(Locale.US);
Money amount = variant.getPrice().getRegularPrice().getAmount();
Money amount = variant.getPriceRange().getMinimumPrice().getFinalPrice();
priceFormatter.setCurrency(Currency.getInstance(amount.getCurrency().toString()));
Assert.assertEquals(priceFormatter.format(amount.getValue()), productTeaser.getFormattedPrice());

Expand Down
Loading