Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit ac54bc9

Browse files
authored
Merge pull request #40 from grayloon/remove-items-from-cart
Remove items from cart via API
2 parents 2f8b321 + d09fce7 commit ac54bc9

File tree

5 files changed

+57
-0
lines changed

5 files changed

+57
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,11 @@ Add/update the specified cart item with a customer token. Must have a store code
110110
Magento::api('cartItems')->addItem($cartId, $sku, $quantity);
111111
```
112112

113+
Remove the specified cart item with a customer token. Must have a store code.
114+
```php
115+
Magento::api('cartItems')->removeItem($itemId);
116+
```
117+
113118
#### Cart Totals (quoteCartTotalRepositoryV1)
114119

115120
`/V1/carts/mine/totals`
@@ -180,6 +185,11 @@ Add/update the specified cart item.
180185
Magento::api('guestCarts')->addItem($cartId, $sku, $quantity);
181186
```
182187

188+
Remove the specified cart item.
189+
```php
190+
Magento::api('guestCarts')->removeItem($cartId, $itemId);
191+
```
192+
183193
`/V1/guest-carts/{cartId}/estimate-shipping-methods`
184194

185195
Estimate shipping by address and return list of available shipping methods.

src/Api/CartItems.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,17 @@ public function addItem($quoteId, $sku, $quantity)
3636
],
3737
]);
3838
}
39+
40+
/**
41+
* Remove the specified cart item.
42+
*
43+
* @param int $itemId
44+
* @return array
45+
*/
46+
public function removeItem($itemId)
47+
{
48+
$this->validateSingleStoreCode();
49+
50+
return $this->delete('/carts/mine/items/'.$itemId);
51+
}
3952
}

src/Api/GuestCarts.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,16 @@ public function paymentInformation($cartId, $body = [])
121121
{
122122
return $this->post('/guest-carts/'.$cartId.'/payment-information', $body);
123123
}
124+
125+
/**
126+
* Remove the specified item from the specified cart.
127+
*
128+
* @param string $cartId
129+
* @param int $itemId
130+
* @return array
131+
*/
132+
public function removeItem($cartId, $itemId)
133+
{
134+
return $this->delete('/guest-carts/'.$cartId.'/items/'.$itemId);
135+
}
124136
}

tests/Api/CartItemsTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,15 @@ public function test_must_pass_a_single_store_code_to_cart_items_add_item()
4444

4545
MagentoFacade::api('cartItems')->addItem('foo', 'bar', 1);
4646
}
47+
48+
public function test_can_call_cart_item_remove_item()
49+
{
50+
Http::fake([
51+
'*rest/default/V1/carts/mine/items/1' => Http::response([], 200),
52+
]);
53+
54+
$api = MagentoFacade::setStoreCode('default')->api('cartItems')->removeItem(1);
55+
56+
$this->assertTrue($api->ok());
57+
}
4758
}

tests/Api/GuestCartsTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,4 +112,15 @@ public function test_can_call_guest_carts_payment_information()
112112

113113
$this->assertTrue($api->ok());
114114
}
115+
116+
public function test_can_remove_items_from_cart()
117+
{
118+
Http::fake([
119+
'*rest/all/V1/guest-carts/foo/items/1' => Http::response([], 200),
120+
]);
121+
122+
$api = MagentoFacade::api('guestCarts')->removeItem('foo', 1);
123+
124+
$this->assertTrue($api->ok());
125+
}
115126
}

0 commit comments

Comments
 (0)