diff --git a/README.md b/README.md index fc3d696..cabd269 100644 --- a/README.md +++ b/README.md @@ -108,6 +108,13 @@ Returns information for the cart for the authenticated customer. Must use a sing Magento::api('carts')->mine(); ``` +`/V1/carts/mine/coupons/{couponCode}` + +Apply a coupon to a specified cart. +```php +Magento::api('carts')->couponCode($couponCode); +``` + #### Cart Items (quoteCartItemRepositoryV1) `/V1/carts/mine/items/` @@ -225,6 +232,13 @@ Estimate shipping by address and return list of available shipping methods. Magento::api('guestCarts')->estimateShippingMethods($cartId); ``` +`/V1/guest-carts/{cartId}/coupons/{couponCode}` + +Apply a coupon to a specified cart. +```php +Magento::api('guestCarts')->couponCode($cartId, $couponCode); +``` + ### Orders (salesOrderRepositoryV1) diff --git a/src/Api/Carts.php b/src/Api/Carts.php index fe60026..2f7d164 100644 --- a/src/Api/Carts.php +++ b/src/Api/Carts.php @@ -91,4 +91,25 @@ public function paymentInformation($body = []) return $this->post('/carts/mine/payment-information', $body); } + + /** + * Apply a coupon to a specified cart. + * + * @param string $couponCode + * @return void + */ + public function couponCode($couponCode) + { + return $this->put('/carts/mine/coupons/'.$couponCode); + } + + /** + * Removes coupon(s) from specified cart. + * + * @return void + */ + public function removeCoupons() + { + return $this->delete('/carts/mine/coupons'); + } } diff --git a/src/Api/GuestCarts.php b/src/Api/GuestCarts.php index 00a49bd..b1d5776 100644 --- a/src/Api/GuestCarts.php +++ b/src/Api/GuestCarts.php @@ -133,4 +133,26 @@ public function removeItem($cartId, $itemId) { return $this->delete('/guest-carts/'.$cartId.'/items/'.$itemId); } + + /** + * Apply a coupon to a specified cart. + * + * @param string $cartId + * @param string $couponCode + * @return void + */ + public function couponCode($cartId, $couponCode) + { + return $this->put('/guest-carts/'.$cartId.'/coupons/'.$couponCode); + } + + /** + * Removes coupon(s) from specified cart. + * + * @return void + */ + public function removeCoupons($cartId) + { + return $this->delete('/guest-carts/'.$cartId.'/coupons'); + } } diff --git a/tests/Api/CartsTest.php b/tests/Api/CartsTest.php index 8109178..c5cfe5c 100644 --- a/tests/Api/CartsTest.php +++ b/tests/Api/CartsTest.php @@ -129,4 +129,26 @@ public function test_must_pass_a_single_store_code_to_payment_information() MagentoFacade::api('carts')->paymentInformation([]); } + + public function test_can_add_coupon_code() + { + Http::fake([ + '*rest/default/V1/carts/mine/coupons/foo' => Http::response('foo', 200), + ]); + + $api = MagentoFacade::setStoreCode('default')->api('carts')->couponCode('foo'); + + $this->assertTrue($api->ok()); + } + + public function test_can_remove_coupon_code() + { + Http::fake([ + '*rest/default/V1/carts/mine/coupons' => Http::response('foo', 200), + ]); + + $api = MagentoFacade::setStoreCode('default')->api('carts')->removeCoupons(); + + $this->assertTrue($api->ok()); + } } diff --git a/tests/Api/GuestCartsTest.php b/tests/Api/GuestCartsTest.php index ef5b24a..a840c88 100644 --- a/tests/Api/GuestCartsTest.php +++ b/tests/Api/GuestCartsTest.php @@ -123,4 +123,26 @@ public function test_can_remove_items_from_cart() $this->assertTrue($api->ok()); } + + public function test_can_add_coupon() + { + Http::fake([ + '*rest/all/V1/guest-carts/foo/coupons/bar' => Http::response([], 200), + ]); + + $api = MagentoFacade::api('guestCarts')->couponCode('foo', 'bar'); + + $this->assertTrue($api->ok()); + } + + public function test_can_remove_coupon() + { + Http::fake([ + '*rest/all/V1/guest-carts/foo/coupons' => Http::response([], 200), + ]); + + $api = MagentoFacade::api('guestCarts')->removeCoupons('foo'); + + $this->assertTrue($api->ok()); + } }