Skip to content

Commit d32c2cc

Browse files
nrostrow-metaNoah Ostrowski
andauthored
Dynamic checkout delete cart item api (#622)
* Setting up new custom API to enable deleting item from cart * boolean to bool * Tweaking API error handling --------- Co-authored-by: Noah Ostrowski <[email protected]>
1 parent 3f760d2 commit d32c2cc

File tree

5 files changed

+176
-0
lines changed

5 files changed

+176
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Sales\Api;
22+
23+
/**
24+
* Delete item from Magento cart
25+
*/
26+
interface DeleteCartItemApiInterface
27+
{
28+
/**
29+
* Delete item from Magento cart
30+
*
31+
* @param string $externalBusinessId
32+
* @param string $cartId
33+
* @param string $itemId
34+
* @return bool
35+
* @throws \Magento\Framework\Exception\UnauthorizedTokenException
36+
* @throws \Magento\Framework\Exception\NoSuchEntityException
37+
* @throws \Magento\Framework\Exception\CouldNotSaveException
38+
* @throws \Magento\Framework\Exception\LocalizedException
39+
*/
40+
public function deleteCartItem(string $externalBusinessId, string $cartId, string $itemId): bool;
41+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* Copyright (c) Meta Platforms, Inc. and affiliates.
7+
*
8+
* Licensed under the Apache License, Version 2.0 (the "License");
9+
* you may not use this file except in compliance with the License.
10+
* You may obtain a copy of the License at
11+
*
12+
* http://www.apache.org/licenses/LICENSE-2.0
13+
*
14+
* Unless required by applicable law or agreed to in writing, software
15+
* distributed under the License is distributed on an "AS IS" BASIS,
16+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* See the License for the specific language governing permissions and
18+
* limitations under the License.
19+
*/
20+
21+
namespace Meta\Sales\Model\Api;
22+
23+
use Magento\Framework\Exception\LocalizedException;
24+
use Magento\Framework\Exception\NoSuchEntityException;
25+
use Magento\Quote\Model\GuestCart\GuestCartItemRepository;
26+
use Meta\BusinessExtension\Helper\FBEHelper;
27+
use Meta\BusinessExtension\Model\Api\CustomApiKey\Authenticator;
28+
use Meta\Sales\Api\DeleteCartItemApiInterface;
29+
use Meta\Sales\Helper\OrderHelper;
30+
31+
class DeleteCartItemApi implements DeleteCartItemApiInterface
32+
{
33+
/**
34+
* @var Authenticator
35+
*/
36+
private Authenticator $authenticator;
37+
38+
/**
39+
* @var OrderHelper
40+
*/
41+
private OrderHelper $orderHelper;
42+
43+
/**
44+
* @var GuestCartItemRepository
45+
*/
46+
private GuestCartItemRepository $guestCartItemRepository;
47+
48+
/**
49+
* @var FBEHelper
50+
*/
51+
private FBEHelper $fbeHelper;
52+
53+
/**
54+
* @param Authenticator $authenticator
55+
* @param OrderHelper $orderHelper
56+
* @param GuestCartItemRepository $guestCartItemRepository
57+
* @param FBEHelper $fbeHelper
58+
*/
59+
public function __construct(
60+
Authenticator $authenticator,
61+
OrderHelper $orderHelper,
62+
GuestCartItemRepository $guestCartItemRepository,
63+
FBEHelper $fbeHelper
64+
) {
65+
$this->authenticator = $authenticator;
66+
$this->orderHelper = $orderHelper;
67+
$this->guestCartItemRepository = $guestCartItemRepository;
68+
$this->fbeHelper = $fbeHelper;
69+
}
70+
71+
/**
72+
* Delete item from Magento cart
73+
*
74+
* @param string $externalBusinessId
75+
* @param string $cartId
76+
* @param string $itemId
77+
* @return bool
78+
* @throws \Magento\Framework\Exception\UnauthorizedTokenException
79+
* @throws \Magento\Framework\Exception\NoSuchEntityException
80+
* @throws \Magento\Framework\Exception\CouldNotSaveException
81+
* @throws \Magento\Framework\Exception\LocalizedException
82+
*/
83+
public function deleteCartItem(string $externalBusinessId, string $cartId, string $itemId): bool
84+
{
85+
$this->authenticator->authenticateRequest();
86+
$storeId = $this->orderHelper->getStoreIdByExternalBusinessId($externalBusinessId);
87+
try {
88+
return $this->guestCartItemRepository->deleteById($cartId, $itemId);
89+
} catch (NoSuchEntityException $e) {
90+
if (strpos($e->getMessage(), 'cartId') !== false) {
91+
$le = new LocalizedException(__(
92+
"No such entity with cartId = %1",
93+
$cartId
94+
));
95+
} else {
96+
$le = $e;
97+
}
98+
$this->fbeHelper->logExceptionImmediatelyToMeta(
99+
$le,
100+
[
101+
'store_id' => $storeId,
102+
'event' => 'delete_cart_item_api',
103+
'event_type' => 'no_such_entity_exception',
104+
'extra_data' => [
105+
'cart_id' => $cartId,
106+
'item_id' => $itemId
107+
]
108+
]
109+
);
110+
throw $le;
111+
} catch (\Throwable $e) {
112+
$this->fbeHelper->logExceptionImmediatelyToMeta(
113+
$e,
114+
[
115+
'store_id' => $storeId,
116+
'event' => 'delete_cart_item_api',
117+
'event_type' => 'error_deleting_cart_item',
118+
'extra_data' => [
119+
'cart_id' => $cartId,
120+
'item_id' => $itemId
121+
]
122+
]
123+
);
124+
throw $e;
125+
}
126+
}
127+
}

app/code/Meta/Sales/etc/acl.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<resource id="Meta_Sales::add_cart_items" title="Add Cart Items" sortOrder="101"/>
1010
<resource id="Meta_Sales::cart_shipping_options" title="Cart Shipping Options" sortOrder="102"/>
1111
<resource id="Meta_Sales::cart_totals" title="Cart Totals" sortOrder="103"/>
12+
<resource id="Meta_Sales::delete_cart_item" title="Delete Cart Item" sortOrder="108"/>
1213
</resource>
1314
</resource>
1415
</resources>

app/code/Meta/Sales/etc/di.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<preference for="Meta\Sales\Api\AddCartItemsApiResponseInterface" type="Meta\Sales\Model\Api\AddCartItemsApiResponse"/>
88
<preference for="Meta\Sales\Api\CartShippingOptionsApiInterface" type="Meta\Sales\Model\Api\CartShippingOptionsApi"/>
99
<preference for="Meta\Sales\Api\CartTotalsApiInterface" type="Meta\Sales\Model\Api\CartTotalsApi"/>
10+
<preference for="Meta\Sales\Api\DeleteCartItemApiInterface" type="Meta\Sales\Model\Api\DeleteCartItemApi"/>
1011
<type name="Magento\Sales\Api\OrderRepositoryInterface">
1112
<plugin name="facebook_order_extension" type="Meta\Sales\Plugin\OrderGet"/>
1213
</type>

app/code/Meta/Sales/etc/webapi.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,10 @@
2525
<resource ref="anonymous"/>
2626
</resources>
2727
</route>
28+
<route url="/V1/meta/:externalBusinessId/:cartId/:itemId/deleteCartItem" method="DELETE">
29+
<service class="Meta\Sales\Api\DeleteCartItemApiInterface" method="deleteCartItem"/>
30+
<resources>
31+
<resource ref="anonymous"/>
32+
</resources>
33+
</route>
2834
</routes>

0 commit comments

Comments
 (0)