Skip to content

Commit 61a1a0a

Browse files
committed
Allow transfer of guest shopping cart to my shopping cart
1 parent ac9e148 commit 61a1a0a

File tree

3 files changed

+139
-7
lines changed

3 files changed

+139
-7
lines changed

README.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,93 @@ String stockId = client.inventory().saveStockItems(productSku, inventory_for_sku
258258

259259
### Guest Shopping Cart
260260

261+
The sample code below shows how to create a new guest shopping cart, add/update/delete items in the shopping cart:
262+
263+
Note that creating guest shopping cart does not require login
264+
265+
```java
266+
MagentoClient client = new MagentoClient(Mediator.url);
267+
String cartId = client.guestCart().newCart();
268+
269+
CartItem item = new CartItem();
270+
item.setQty(1);
271+
item.setSku("product_dynamic_758");
272+
273+
// add new item to shopping cart
274+
item = client.guestCart().addItemToCart(cartId, item);
275+
System.out.println("cartItem: " + JSON.toJSONString(item, SerializerFeature.PrettyFormat));
276+
277+
// update item in the shopping cart
278+
item.setQty(3);
279+
item = client.guestCart().updateItemInCart(cartId, item);
280+
System.out.println("cartItem: " + JSON.toJSONString(item, SerializerFeature.PrettyFormat));
281+
282+
// delete item in the shopping cart
283+
boolean deleted = client.guestCart().deleteItemInCart(cartId, item.getItem_id());
284+
285+
Cart cart = client.guestCart().getCart(cartId);
286+
CartTotal cartTotal = client.getGuestCart().getCartTotal(cartId);
287+
288+
System.out.println("cart: " + JSON.toJSONString(cart, SerializerFeature.PrettyFormat));
289+
System.out.println("cartTotal: " + JSON.toJSONString(cartTotal, SerializerFeature.PrettyFormat));
290+
```
291+
292+
The sample code belows show how to transfer a guest cart to my cart after user login:
293+
294+
```bash
295+
MagentoClient client = new MagentoClient(Mediator.url);
296+
297+
String cartId = client.guestCart().newCart();
298+
299+
CartItem item = new CartItem();
300+
item.setQty(1);
301+
item.setSku("product_dynamic_758");
302+
303+
item = client.guestCart().addItemToCart(cartId, item);
304+
305+
client.loginAsClient("username", "password");
306+
boolean result = client.myCart().transferGuestCartToMyCart(cartId);
307+
308+
Cart cart = client.myCart().getCart();
309+
CartTotal cartTotal = client.myCart().getCartTotal();
310+
311+
```
312+
313+
### My Shopping Cart
314+
315+
The sample code below shows how to create my shopping cart, add/update/delete items in the shopping cart:
316+
317+
Note that creating my shopping cart requires login
318+
319+
```java
320+
MagentoClient client = new MagentoClient(Mediator.url);
321+
client.loginAsClient("username", "password");
322+
String quoteId = client.myCart().newQuote();
323+
324+
CartItem item = new CartItem();
325+
item.setQty(1);
326+
item.setSku("product_dynamic_758");
327+
328+
// add new item to shopping cart
329+
item = client.myCart().addItemToCart(quoteId, item);
330+
System.out.println("cartItem: " + JSON.toJSONString(item, SerializerFeature.PrettyFormat));
331+
332+
// update item in the shopping cart
333+
item.setQty(3);
334+
item = client.myCart().updateItemInCart(quoteId, item);
335+
System.out.println("cartItem: " + JSON.toJSONString(item, SerializerFeature.PrettyFormat));
336+
337+
// delete item in the shopping cart
338+
boolean deleted = client.myCart().deleteItemInCart(item.getItem_id());
339+
340+
Cart cart = client.myCart().getCart();
341+
CartTotal cartTotal = client.myCart().getCartTotal();
342+
343+
System.out.println("cart: " + JSON.toJSONString(cart, SerializerFeature.PrettyFormat));
344+
System.out.println("cartTotal: " + JSON.toJSONString(cartTotal, SerializerFeature.PrettyFormat));
345+
```
346+
347+
261348
The sample code below shows how to create a new guest shopping cart, add/update/delete items in the shopping cart:
262349

263350
Note that creating guest shopping cart does not require login

src/main/java/com/github/chen0040/magento/services/MagentoMyCartManager.java

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import com.alibaba.fastjson.JSON;
55
import com.alibaba.fastjson.serializer.SerializerFeature;
66
import com.github.chen0040.magento.MagentoClient;
7+
import com.github.chen0040.magento.models.Account;
78
import com.github.chen0040.magento.models.Cart;
89
import com.github.chen0040.magento.models.CartItem;
910
import com.github.chen0040.magento.models.CartTotal;
@@ -20,6 +21,8 @@ public class MagentoMyCartManager extends MagentoHttpComponent {
2021
protected final MagentoClient client;
2122
private static final String relativePath = "rest/V1/carts";
2223
private static final String cartId = "mine";
24+
private long customerId = -1L;
25+
private long storeId = -1L;
2326

2427
public MagentoMyCartManager(MagentoClient client) {
2528
super(client.getHttpComponent());
@@ -55,7 +58,7 @@ public Cart getCart() {
5558
return null;
5659
}
5760

58-
System.out.println(json);
61+
5962

6063
Cart cart = JSON.parseObject(json, Cart.class);
6164
return cart;
@@ -68,7 +71,7 @@ public CartTotal getCartTotal() {
6871
return null;
6972
}
7073

71-
System.out.println(json);
74+
7275

7376
CartTotal cartTotal = JSON.parseObject(json, CartTotal.class);
7477
return cartTotal;
@@ -88,7 +91,7 @@ public CartItem addItemToCart(String quoteId, CartItem item) {
8891
return null;
8992
}
9093

91-
System.out.println(json);
94+
9295

9396
CartItem saved = JSON.parseObject(json, CartItem.class);
9497

@@ -110,7 +113,7 @@ public CartItem updateItemInCart(String quoteId, CartItem item) {
110113
return null;
111114
}
112115

113-
System.out.println(json);
116+
114117

115118
CartItem saved = JSON.parseObject(json, CartItem.class);
116119

@@ -125,9 +128,30 @@ public boolean deleteItemInCart(int itemId) {
125128
return false;
126129
}
127130

128-
System.out.println(json);
131+
129132

130133
return json.equalsIgnoreCase("true");
131134
}
132135

136+
public boolean transferGuestCartToMyCart(String guestCartId) {
137+
if(customerId == -1L) {
138+
Account account = client.getMyAccount();
139+
customerId = account.getId();
140+
storeId = account.getStore_id();
141+
}
142+
Map<String, Object> request = new HashMap<>();
143+
request.put("customerId", customerId);
144+
request.put("storeId", storeId);
145+
String json = JSON.toJSONString(request, SerializerFeature.BrowserCompatible);
146+
json = putSecure(baseUri() + "/rest/V1/guest-carts/" + guestCartId, json);
147+
148+
if(!validate(json)){
149+
return false;
150+
}
151+
152+
153+
154+
return true;
155+
}
156+
133157
}

src/test/java/com/github/chen0040/magento/MagentoClientMyCartUnitTest.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,13 @@ public void test_updateItemInCart(){
7878
public void test_deleteItemInCart(){
7979
MagentoClient client = new MagentoClient(Mediator.url);
8080
client.loginAsClient(Mediator.customerUsername, Mediator.customerPassword);
81-
String cartId = client.myCart().newQuote();
81+
String quoteId = client.myCart().newQuote();
8282

8383
CartItem item = new CartItem();
8484
item.setQty(1);
8585
item.setSku("product_dynamic_758");
8686

87-
item = client.myCart().addItemToCart(cartId, item);
87+
item = client.myCart().addItemToCart(quoteId, item);
8888
boolean result = client.myCart().deleteItemInCart(item.getItem_id());
8989

9090

@@ -96,5 +96,26 @@ public void test_deleteItemInCart(){
9696
logger.info("cartTotal: \r\n{}", JSON.toJSONString(cartTotal, SerializerFeature.PrettyFormat));
9797
}
9898

99+
@Test
100+
public void test_transferGuestCartToMyCart(){
101+
MagentoClient client = new MagentoClient(Mediator.url);
102+
103+
String cartId = client.guestCart().newCart();
104+
105+
CartItem item = new CartItem();
106+
item.setQty(1);
107+
item.setSku("product_dynamic_758");
108+
109+
item = client.guestCart().addItemToCart(cartId, item);
110+
111+
client.loginAsClient(Mediator.customerUsername, Mediator.customerPassword);
112+
boolean result = client.myCart().transferGuestCartToMyCart(cartId);
113+
114+
Cart cart = client.myCart().getCart();
115+
CartTotal cartTotal = client.myCart().getCartTotal();
99116

117+
logger.info("result: {}", result);
118+
logger.info("cart: \r\n{}", JSON.toJSONString(cart, SerializerFeature.PrettyFormat));
119+
logger.info("cartTotal: \r\n{}", JSON.toJSONString(cartTotal, SerializerFeature.PrettyFormat));
120+
}
100121
}

0 commit comments

Comments
 (0)