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

CIF-2183 - Do not show pricing information for null values #622

Merged
merged 9 commits into from
Jul 15, 2021
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import java.text.NumberFormat;
import java.util.Locale;

import org.apache.commons.lang.StringUtils;

import com.adobe.cq.commerce.core.components.internal.models.v1.Utils;
import com.adobe.cq.commerce.core.components.models.common.Price;
import com.adobe.cq.commerce.magento.graphql.PriceRange;
Expand Down Expand Up @@ -45,6 +47,7 @@ public class PriceImpl implements Price {
private Boolean isDiscounted;
private Boolean isRange;
private boolean isStartPrice;
private boolean isEmpty = false;

public PriceImpl(PriceRange range, Locale locale) {
this(range, locale, false);
Expand All @@ -57,6 +60,12 @@ public PriceImpl(PriceRange range, Locale locale, boolean isStartPrice) {

this.regularPriceMin = range.getMinimumPrice().getRegularPrice().getValue();
this.finalPriceMin = range.getMinimumPrice().getFinalPrice().getValue();

// Price values could be null, do not display price if they are
if (this.regularPriceMin == null || this.finalPriceMin == null) {
this.isEmpty = true;
}

this.discountAmountMin = range.getMinimumPrice().getDiscount().getAmountOff();
this.discountPercentMin = range.getMinimumPrice().getDiscount().getPercentOff();

Expand All @@ -75,6 +84,11 @@ private NumberFormat getPriceFormatter() {
return priceFormatter;
}

@Override
public boolean isEmpty() {
return isEmpty;
}

@Override
public Boolean isRange() {
if (isRange == null) {
Expand Down Expand Up @@ -111,7 +125,10 @@ public Double getRegularPrice() {

@Override
public String getFormattedRegularPrice() {
return getPriceFormatter().format(regularPriceMin);
if (regularPriceMin != null) {
return getPriceFormatter().format(regularPriceMin);
}
return StringUtils.EMPTY;
}

@Override
Expand All @@ -121,7 +138,10 @@ public Double getFinalPrice() {

@Override
public String getFormattedFinalPrice() {
return getPriceFormatter().format(finalPriceMin);
if (finalPriceMin != null) {
return getPriceFormatter().format(finalPriceMin);
}
return StringUtils.EMPTY;
}

@Override
Expand All @@ -131,7 +151,10 @@ public Double getDiscountAmount() {

@Override
public String getFormattedDiscountAmount() {
return getPriceFormatter().format(discountAmountMin);
if (discountAmountMin != null) {
return getPriceFormatter().format(discountAmountMin);
}
return StringUtils.EMPTY;
}

@Override
Expand All @@ -146,7 +169,7 @@ public Double getRegularPriceMax() {

@Override
public String getFormattedRegularPriceMax() {
return isRange() ? getPriceFormatter().format(regularPriceMax) : "";
return isRange() && regularPriceMax != null ? getPriceFormatter().format(regularPriceMax) : StringUtils.EMPTY;
}

@Override
Expand All @@ -156,7 +179,7 @@ public Double getFinalPriceMax() {

@Override
public String getFormattedFinalPriceMax() {
return isRange() ? getPriceFormatter().format(finalPriceMax) : "";
return isRange() && finalPriceMax != null ? getPriceFormatter().format(finalPriceMax) : StringUtils.EMPTY;
}

@Override
Expand All @@ -166,7 +189,7 @@ public Double getDiscountAmountMax() {

@Override
public String getFormattedDiscountAmountMax() {
return isRange() ? getPriceFormatter().format(discountAmountMax) : "";
return isRange() && discountAmountMax != null ? getPriceFormatter().format(discountAmountMax) : StringUtils.EMPTY;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@
*/
public interface Price {

/**
* Indicates if a product price is not available. If a product price is not available, most methods return null or an empty string.
*
* @return <code>false</code> if price is available, <code>true</code> if no price is available.
*/
boolean isEmpty();
buuhuu marked this conversation as resolved.
Show resolved Hide resolved

Boolean isRange();

Boolean isDiscounted();
Expand All @@ -29,28 +36,58 @@ public interface Price {

Double getRegularPrice();

/**
* Returns formatted regular price.
*
* @return formatted price as string, might be empty if no price is available.
*/
String getFormattedRegularPrice();

Double getFinalPrice();

/**
* Returns formatted final price.
*
* @return formatted price as string, might be empty if no price is available.
*/
String getFormattedFinalPrice();

Double getDiscountAmount();

/**
* Returns formatted discount amount.
*
* @return formatted discount amount as string, might be empty if no price is available.
*/
String getFormattedDiscountAmount();

Double getDiscountPercent();

Double getRegularPriceMax();

/**
* Returns formatted maximum regular price.
*
* @return formatted price as string, might be empty if no price is available.
*/
String getFormattedRegularPriceMax();

Double getFinalPriceMax();

/**
* Returns formatted maximum final price.
*
* @return formatted price as string, might be empty if no price is available.
*/
String getFormattedFinalPriceMax();

Double getDiscountAmountMax();

/**
* Returns formatted maximum discount amount.
*
* @return formatted discount amount as string, might be empty if no price is available.
*/
String getFormattedDiscountAmountMax();

Double getDiscountPercentMax();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright 2021 Adobe. All rights reserved.
*
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/

package com.adobe.cq.commerce.core.components.internal.models.v1.common;

import java.util.Locale;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Answers;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import com.adobe.cq.commerce.core.components.models.common.Price;
import com.adobe.cq.commerce.magento.graphql.CurrencyEnum;
import com.adobe.cq.commerce.magento.graphql.Money;
import com.adobe.cq.commerce.magento.graphql.PriceRange;

import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class PriceTest {

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private PriceRange priceRange;

@Mock(answer = Answers.RETURNS_DEEP_STUBS)
private Money money;

@Before
public void setUp() {
when(money.getCurrency()).thenReturn(CurrencyEnum.USD);

when(priceRange.getMinimumPrice().getFinalPrice()).thenReturn(money);
when(priceRange.getMinimumPrice().getRegularPrice()).thenReturn(money);

}

@Test
public void testNotEmptyPrices() {
when(money.getValue()).thenReturn(12.34);
when(priceRange.getMinimumPrice().getDiscount().getAmountOff()).thenReturn(23.45);

Price price = new PriceImpl(priceRange, Locale.US, false);

Assert.assertFalse(price.isEmpty());

Assert.assertEquals("$12.34", price.getFormattedRegularPrice());
Assert.assertEquals("$12.34", price.getFormattedFinalPrice());
Assert.assertEquals("$23.45", price.getFormattedDiscountAmount());
}

@Test
public void testEmptyPrices() {
when(money.getValue()).thenReturn(null);
when(priceRange.getMinimumPrice().getDiscount().getAmountOff()).thenReturn(null);

Price price = new PriceImpl(priceRange, Locale.US, false);

Assert.assertTrue(price.isEmpty());

Assert.assertEquals("", price.getFormattedRegularPrice());
Assert.assertEquals("", price.getFormattedFinalPrice());
Assert.assertEquals("", price.getFormattedDiscountAmount());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,18 @@ class Product {
price.currency = range.minimum_price.final_price.currency;
price.regularPrice = range.minimum_price.regular_price.value;
price.finalPrice = range.minimum_price.final_price.value;
price.discountAmount = range.minimum_price.discount.amount_off;
price.discountPercent = range.minimum_price.discount.percent_off;
if (range.minimum_price.discount) {
price.discountAmount = range.minimum_price.discount.amount_off;
buuhuu marked this conversation as resolved.
Show resolved Hide resolved
price.discountPercent = range.minimum_price.discount.percent_off;
}

if (range.maximum_price) {
price.regularPriceMax = range.maximum_price.regular_price.value;
price.finalPriceMax = range.maximum_price.final_price.value;
price.discountAmountMax = range.maximum_price.discount.amount_off;
price.discountPercentMax = range.maximum_price.discount.percent_off;
if (range.maximum_price.discount) {
price.discountAmountMax = range.maximum_price.discount.amount_off;
price.discountPercentMax = range.maximum_price.discount.percent_off;
}
}

price.discounted = !!(price.discountAmount && price.discountAmount > 0);
Expand Down Expand Up @@ -131,6 +135,11 @@ class Product {
* Update price in the DOM.
*/
_updatePrice(price, optionalSku) {
// Only update if prices are not null
if (!price.regularPrice || !price.finalPrice) {
return;
}

let youSave = this._formatter.get('You save');
let innerHTML = '';
if (!price.range) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,18 @@ class ProductCollection {
price.currency = range.minimum_price.final_price.currency;
price.regularPrice = range.minimum_price.regular_price.value;
price.finalPrice = range.minimum_price.final_price.value;
price.discountAmount = range.minimum_price.discount.amount_off;
price.discountPercent = range.minimum_price.discount.percent_off;
if (range.minimum_price.discount) {
price.discountAmount = range.minimum_price.discount.amount_off;
price.discountPercent = range.minimum_price.discount.percent_off;
}

if (range.maximum_price) {
price.regularPriceMax = range.maximum_price.regular_price.value;
price.finalPriceMax = range.maximum_price.final_price.value;
price.discountAmountMax = range.maximum_price.discount.amount_off;
price.discountPercentMax = range.maximum_price.discount.percent_off;
if (range.maximum_price.discount) {
price.discountAmountMax = range.maximum_price.discount.amount_off;
price.discountPercentMax = range.maximum_price.discount.percent_off;
}
}

price.discounted = !!(price.discountAmount && price.discountAmount > 0);
Expand Down Expand Up @@ -105,6 +109,11 @@ class ProductCollection {

const price = this._state.prices[item.dataset.sku];

// Only update if prices are available and not null
if (!price || !price.regularPrice || !price.finalPrice) {
return;
}

let innerHTML = '';
if (!price.range) {
if (price.discounted) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@
~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
<sly data-sly-template.price="${@ priceRange=priceRange, displayYouSave, sku}">
<div class="price" data-sly-test=${!priceRange.isRange} data-product-sku=${sku}>
<div class="price" data-sly-test="${!priceRange.isEmpty && !priceRange.isRange}" data-product-sku=${sku}>
<sly data-sly-test.isDiscounted=${priceRange.isDiscounted}>
<span class="regularPrice">${priceRange.formattedRegularPrice}</span>
<span class="discountedPrice">${priceRange.formattedFinalPrice}</span>
<span class="you-save" data-sly-test=${displayYouSave}>${'You save' @i18n} ${priceRange.formattedDiscountAmount} (${priceRange.discountPercent}%)</span>
</sly>
<span data-sly-test=${!isDiscounted}><sly data-sly-test=${priceRange.isStartPrice}>${'Starting at' @i18n} </sly>${priceRange.formattedRegularPrice}</span>
</div>
<div class="price range" data-sly-test=${priceRange.isRange} data-product-sku=${sku}>
<div class="price range" data-sly-test="${!priceRange.isEmpty && priceRange.isRange}" data-product-sku=${sku}>
<sly data-sly-test.isDiscounted=${priceRange.isDiscounted}>
<span class="regularPrice">${'From' @i18n} ${priceRange.formattedRegularPrice} ${'To' @i18n} ${priceRange.formattedRegularPriceMax}</span>
<span class="discountedPrice">${'From' @i18n} ${priceRange.formattedFinalPrice} ${'To' @i18n} ${priceRange.formattedFinalPriceMax}</span>
Expand Down
27 changes: 27 additions & 0 deletions ui.apps/test/components/commerce/product/productTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -310,5 +310,32 @@ describe('Product', () => {
assert.equal(youSave, 'You save $5.00 (50%)');
});
});

it('displays a null price', () => {
const priceRange = {
'sample-sku': {
minimum_price: {
regular_price: {
value: null,
currency: 'USD'
},
final_price: {
value: null,
currency: 'USD'
}
}
}
};
window.CIF.CommerceGraphqlApi.getProductPrices.resetBehavior();
window.CIF.CommerceGraphqlApi.getProductPrices.resolves(priceRange);

productRoot.dataset.loadClientPrice = true;
let product = new Product({ element: productRoot });

return product._initPrices().then(() => {
let price = productRoot.querySelector(Product.selectors.price).innerText;
assert.equal(price, '');
});
});
});
});
Loading