Skip to content

Commit ae20b21

Browse files
authored
CIF-1334 - Set default quantities of grouped products (#240)
* CIF-1334 - Set default quantities of grouped products - use default quantities of grouped products * CIF-1334 - Set default quantities of grouped products - enable/disable add-to-cart on load based on product quantities * CIF-1334 - Set default quantities of grouped products - fix issue with possible null value * CIF-1334 - Set default quantities of grouped products - fix issue with possible null value
1 parent b62ec2c commit ae20b21

File tree

8 files changed

+115
-18
lines changed

8 files changed

+115
-18
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*******************************************************************************
2+
*
3+
* Copyright 2020 Adobe. All rights reserved.
4+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License. You may obtain a copy
6+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software distributed under
9+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
10+
* OF ANY KIND, either express or implied. See the License for the specific language
11+
* governing permissions and limitations under the License.
12+
*
13+
******************************************************************************/
14+
15+
package com.adobe.cq.commerce.core.components.internal.models.v1.product;
16+
17+
import com.adobe.cq.commerce.core.components.models.common.Price;
18+
import com.adobe.cq.commerce.core.components.models.product.GroupItem;
19+
20+
public class GroupItemImpl implements GroupItem {
21+
22+
private String name;
23+
private String sku;
24+
private Price priceRange;
25+
private Double defaultQuantity;
26+
27+
@Override
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
@Override
37+
public String getSku() {
38+
return sku;
39+
}
40+
41+
public void setSku(String sku) {
42+
this.sku = sku;
43+
}
44+
45+
@Override
46+
public Price getPriceRange() {
47+
return priceRange;
48+
}
49+
50+
public void setPriceRange(Price priceRange) {
51+
this.priceRange = priceRange;
52+
}
53+
54+
@Override
55+
public Double getDefaultQuantity() {
56+
return defaultQuantity;
57+
}
58+
59+
public void setDefaultQuantity(Double defaultQuantity) {
60+
this.defaultQuantity = defaultQuantity;
61+
}
62+
}

bundles/core/src/main/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImpl.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import com.adobe.cq.commerce.core.components.internal.models.v1.common.PriceImpl;
4141
import com.adobe.cq.commerce.core.components.models.common.Price;
4242
import com.adobe.cq.commerce.core.components.models.product.Asset;
43+
import com.adobe.cq.commerce.core.components.models.product.GroupItem;
4344
import com.adobe.cq.commerce.core.components.models.product.Product;
4445
import com.adobe.cq.commerce.core.components.models.product.Variant;
4546
import com.adobe.cq.commerce.core.components.models.product.VariantAttribute;
@@ -211,7 +212,7 @@ public List<Variant> getVariants() {
211212
}
212213

213214
@Override
214-
public List<Variant> getGroupedProductItems() {
215+
public List<GroupItem> getGroupedProductItems() {
215216
// Don't return any items if the current product is not of type GroupedProduct.
216217
if (!isGroupedProduct()) {
217218
return Collections.emptyList();
@@ -296,15 +297,16 @@ private Variant mapVariant(ConfigurableVariant variant) {
296297
return productVariant;
297298
}
298299

299-
private Variant mapGroupedProductItem(GroupedProductItem item) {
300+
private GroupItem mapGroupedProductItem(com.adobe.cq.commerce.magento.graphql.GroupedProductItem item) {
300301
ProductInterface product = item.getProduct();
301302

302-
VariantImpl productVariant = new VariantImpl();
303-
productVariant.setName(product.getName());
304-
productVariant.setSku(product.getSku());
305-
productVariant.setPriceRange(new PriceImpl(product.getPriceRange(), locale));
303+
GroupItemImpl groupedProductItem = new GroupItemImpl();
304+
groupedProductItem.setName(product.getName());
305+
groupedProductItem.setSku(product.getSku());
306+
groupedProductItem.setPriceRange(new PriceImpl(product.getPriceRange(), locale));
307+
groupedProductItem.setDefaultQuantity(item.getQty());
306308

307-
return productVariant;
309+
return groupedProductItem;
308310
}
309311

310312
private List<Asset> filterAndSortAssets(List<MediaGalleryEntry> assets) {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*******************************************************************************
2+
*
3+
* Copyright 2020 Adobe. All rights reserved.
4+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License. You may obtain a copy
6+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
7+
*
8+
* Unless required by applicable law or agreed to in writing, software distributed under
9+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
10+
* OF ANY KIND, either express or implied. See the License for the specific language
11+
* governing permissions and limitations under the License.
12+
*
13+
******************************************************************************/
14+
15+
package com.adobe.cq.commerce.core.components.models.product;
16+
17+
import com.adobe.cq.commerce.core.components.models.common.Price;
18+
19+
/**
20+
* GroupItem is a view model interface representing the item of a grouped product.
21+
*/
22+
public interface GroupItem {
23+
24+
String getName();
25+
26+
String getSku();
27+
28+
Price getPriceRange();
29+
30+
Double getDefaultQuantity();
31+
}

bundles/core/src/main/java/com/adobe/cq/commerce/core/components/models/product/Product.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public interface Product {
7070

7171
List<Variant> getVariants();
7272

73-
List<Variant> getGroupedProductItems();
73+
List<GroupItem> getGroupedProductItems();
7474

7575
List<Asset> getAssets();
7676

bundles/core/src/test/java/com/adobe/cq/commerce/core/components/internal/models/v1/product/ProductImplTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import com.adobe.cq.commerce.common.ValueMapDecorator;
3737
import com.adobe.cq.commerce.core.components.models.common.Price;
3838
import com.adobe.cq.commerce.core.components.models.product.Asset;
39+
import com.adobe.cq.commerce.core.components.models.product.GroupItem;
3940
import com.adobe.cq.commerce.core.components.models.product.Variant;
4041
import com.adobe.cq.commerce.core.components.models.product.VariantAttribute;
4142
import com.adobe.cq.commerce.core.components.models.product.VariantValue;
@@ -346,18 +347,19 @@ public void testGroupedProduct() throws IOException {
346347

347348
productModel = context.request().adaptTo(ProductImpl.class);
348349

349-
List<Variant> items = productModel.getGroupedProductItems();
350+
List<GroupItem> items = productModel.getGroupedProductItems();
350351
Assert.assertTrue(productModel.isGroupedProduct());
351352
Assert.assertEquals(3, items.size());
352353

353354
GroupedProduct gp = (GroupedProduct) product;
354355
for (int i = 0; i < items.size(); i++) {
355-
Variant item = items.get(i);
356+
GroupItem item = items.get(i);
356357
ProductInterface pi = gp.getItems().get(i).getProduct();
357358

358359
Assert.assertEquals(pi.getSku(), item.getSku());
359360
Assert.assertEquals(pi.getName(), item.getName());
360361
Assert.assertEquals(pi.getPriceRange().getMinimumPrice().getFinalPrice().getValue(), item.getPriceRange().getFinalPrice(), 0);
362+
Assert.assertEquals(gp.getItems().get(i).getQty(), item.getDefaultQuantity(), 0);
361363
}
362364
}
363365

ui.apps/src/main/content/jcr_root/apps/core/cif/components/commerce/product/v1/product/clientlib/js/addToCart.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class AddToCart {
3737

3838
const groupedProducts = document.querySelector(AddToCart.selectors.groupedProducts);
3939
if (groupedProducts) {
40-
this._element.disabled = true;
40+
this._onQuantityChanged(); // init
4141
// Disable/enable add to cart based on the selected quantities of a grouped product
4242
document.querySelectorAll(AddToCart.selectors.quantity).forEach(selection => {
4343
selection.addEventListener('change', this._onQuantityChanged.bind(this));

ui.apps/src/main/content/jcr_root/apps/core/cif/components/commerce/product/v1/product/groupedProducts.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ <h3>${item.name}</h3>
2929
</td>
3030
<td data-th="{'Quantity' @ i18n}">
3131
<sly data-sly-use.template="quantity.html"
32-
data-sly-call="${template.quantity @ product=item, allowZeroQuantity=true}"></sly>
32+
data-sly-call="${template.quantity @ product=item, allowZeroQuantity=true, defaultQuantity=item.defaultQuantity}"></sly>
3333
</td>
3434
</tr>
3535
</sly>

ui.apps/src/main/content/jcr_root/apps/core/cif/components/commerce/product/v1/product/quantity.html

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@
1111
~ governing permissions and limitations under the License.
1212
~
1313
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/-->
14-
<template data-sly-template.quantity="${ @ product, allowZeroQuantity}">
14+
<template data-sly-template.quantity="${ @ product, allowZeroQuantity, defaultQuantity}">
1515
<div class="quantity__root">
1616
<span class="fieldIcons__root" style="--iconsBefore:0; --iconsAfter:1;">
1717
<span class="fieldIcons__input">
1818
<select aria-label="product's quantity" class="select__input field__input" name="quantity" data-product-sku="${product.sku}">
19-
<option value="0" data-sly-test="${allowZeroQuantity}">0</option>
20-
<option value="1">1</option>
21-
<option value="2">2</option>
22-
<option value="3">3</option>
23-
<option value="4">4</option>
19+
<option value="0" data-sly-test="${allowZeroQuantity}" selected="${defaultQuantity && defaultQuantity == 0}">0</option>
20+
<option value="1" selected="${defaultQuantity == null || defaultQuantity == 1}">1</option>
21+
<option value="2" selected="${defaultQuantity && defaultQuantity == 2}">2</option>
22+
<option value="3" selected="${defaultQuantity && defaultQuantity == 3}">3</option>
23+
<option value="4" selected="${defaultQuantity && defaultQuantity == 4}">4</option>
2424
</select>
2525
</span>
2626
<span class="fieldIcons__before"></span>

0 commit comments

Comments
 (0)