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

Commit 1ca5c87

Browse files
authored
Merge pull request #46 from grayloon/coupons
Add Coupon Code Endpoints
2 parents ede0696 + 66b2c72 commit 1ca5c87

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,13 @@ Returns information for the cart for the authenticated customer. Must use a sing
108108
Magento::api('carts')->mine();
109109
```
110110

111+
`/V1/carts/mine/coupons/{couponCode}`
112+
113+
Apply a coupon to a specified cart.
114+
```php
115+
Magento::api('carts')->couponCode($couponCode);
116+
```
117+
111118
#### Cart Items (quoteCartItemRepositoryV1)
112119

113120
`/V1/carts/mine/items/`
@@ -225,6 +232,13 @@ Estimate shipping by address and return list of available shipping methods.
225232
Magento::api('guestCarts')->estimateShippingMethods($cartId);
226233
```
227234

235+
`/V1/guest-carts/{cartId}/coupons/{couponCode}`
236+
237+
Apply a coupon to a specified cart.
238+
```php
239+
Magento::api('guestCarts')->couponCode($cartId, $couponCode);
240+
```
241+
228242
<a id="orders"></a>
229243
### Orders (salesOrderRepositoryV1)
230244

src/Api/Carts.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,25 @@ public function paymentInformation($body = [])
9191

9292
return $this->post('/carts/mine/payment-information', $body);
9393
}
94+
95+
/**
96+
* Apply a coupon to a specified cart.
97+
*
98+
* @param string $couponCode
99+
* @return void
100+
*/
101+
public function couponCode($couponCode)
102+
{
103+
return $this->put('/carts/mine/coupons/'.$couponCode);
104+
}
105+
106+
/**
107+
* Removes coupon(s) from specified cart.
108+
*
109+
* @return void
110+
*/
111+
public function removeCoupons()
112+
{
113+
return $this->delete('/carts/mine/coupons');
114+
}
94115
}

src/Api/GuestCarts.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,4 +133,26 @@ public function removeItem($cartId, $itemId)
133133
{
134134
return $this->delete('/guest-carts/'.$cartId.'/items/'.$itemId);
135135
}
136+
137+
/**
138+
* Apply a coupon to a specified cart.
139+
*
140+
* @param string $cartId
141+
* @param string $couponCode
142+
* @return void
143+
*/
144+
public function couponCode($cartId, $couponCode)
145+
{
146+
return $this->put('/guest-carts/'.$cartId.'/coupons/'.$couponCode);
147+
}
148+
149+
/**
150+
* Removes coupon(s) from specified cart.
151+
*
152+
* @return void
153+
*/
154+
public function removeCoupons($cartId)
155+
{
156+
return $this->delete('/guest-carts/'.$cartId.'/coupons');
157+
}
136158
}

tests/Api/CartsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,26 @@ public function test_must_pass_a_single_store_code_to_payment_information()
129129

130130
MagentoFacade::api('carts')->paymentInformation([]);
131131
}
132+
133+
public function test_can_add_coupon_code()
134+
{
135+
Http::fake([
136+
'*rest/default/V1/carts/mine/coupons/foo' => Http::response('foo', 200),
137+
]);
138+
139+
$api = MagentoFacade::setStoreCode('default')->api('carts')->couponCode('foo');
140+
141+
$this->assertTrue($api->ok());
142+
}
143+
144+
public function test_can_remove_coupon_code()
145+
{
146+
Http::fake([
147+
'*rest/default/V1/carts/mine/coupons' => Http::response('foo', 200),
148+
]);
149+
150+
$api = MagentoFacade::setStoreCode('default')->api('carts')->removeCoupons();
151+
152+
$this->assertTrue($api->ok());
153+
}
132154
}

tests/Api/GuestCartsTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,26 @@ public function test_can_remove_items_from_cart()
123123

124124
$this->assertTrue($api->ok());
125125
}
126+
127+
public function test_can_add_coupon()
128+
{
129+
Http::fake([
130+
'*rest/all/V1/guest-carts/foo/coupons/bar' => Http::response([], 200),
131+
]);
132+
133+
$api = MagentoFacade::api('guestCarts')->couponCode('foo', 'bar');
134+
135+
$this->assertTrue($api->ok());
136+
}
137+
138+
public function test_can_remove_coupon()
139+
{
140+
Http::fake([
141+
'*rest/all/V1/guest-carts/foo/coupons' => Http::response([], 200),
142+
]);
143+
144+
$api = MagentoFacade::api('guestCarts')->removeCoupons('foo');
145+
146+
$this->assertTrue($api->ok());
147+
}
126148
}

0 commit comments

Comments
 (0)