Skip to content

Commit 563dd3e

Browse files
authored
Merge pull request #49 from aligent/feature/implement-cart-api
Feature/implement cart api
2 parents 2c9a453 + 4fbaea4 commit 563dd3e

File tree

14 files changed

+416
-1
lines changed

14 files changed

+416
-1
lines changed

RELEASE_NOTES.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
### New Features
22

3-
Allow the use of [parameters in ProductsApi::Get](https://developer.bigcommerce.com/api-reference/store-management/catalog/products/getproductbyid).
3+
- Implement the [Cart API](https://developer.bigcommerce.com/api-reference/store-management/carts/cart/createacart).
4+
- Implement the [Cart Items API](https://developer.bigcommerce.com/api-reference/store-management/carts/cart-items/addcartlineitem)
5+
- Implement the [Cart Redirect URLS API](https://developer.bigcommerce.com/api-reference/store-management/carts/cart-redirect-urls/createcartredirecturl)
6+
- Allow the use of [parameters in ProductsApi::Get](https://developer.bigcommerce.com/api-reference/store-management/catalog/products/getproductbyid).
47

58
Here's an example using PHP 8:
69

@@ -12,3 +15,4 @@ $product = $api->catalog()->product(123)->get(include_fields: ['description', 's
1215

1316
Fix issue with ProductVariant::sku_id not being nullable #47 (thanks @Yorgv)
1417

18+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace BigCommerce\ApiV3\Api\Carts;
4+
5+
use BigCommerce\ApiV3\Api\Generic\DeleteResource;
6+
use BigCommerce\ApiV3\Api\Generic\UuidResourceWithUuidParentApi;
7+
use BigCommerce\ApiV3\ResourceModels\Cart\CartItem;
8+
use BigCommerce\ApiV3\ResponseModels\Cart\CartResponse;
9+
use GuzzleHttp\RequestOptions;
10+
11+
class CartItemsApi extends UuidResourceWithUuidParentApi
12+
{
13+
use DeleteResource;
14+
15+
private const CARTS_ENDPOINT = 'carts/%s/items';
16+
private const CART_ENDPOINT = 'carts/%s/items/%s';
17+
18+
/**
19+
* Create a direct link to a Cart.
20+
*/
21+
public const INCLUDE_REDIRECT_URLS = 'redirect_urls';
22+
23+
/**
24+
* The Cart returns an abbreviated result. Use this to return physical items product options.
25+
*/
26+
public const INCLUDE_PHYSICAL_ITEMS = 'line_items.physical_items.options';
27+
28+
/**
29+
* The Cart returns an abbreviated result. Use this to return digital items product options.
30+
*/
31+
public const INCLUDE_DIGITAL_ITEMS = 'line_items.digital_items.options';
32+
33+
public function add(CartItem $cartItem, ?string $include = null): CartResponse
34+
{
35+
$query = $include ? ['include' => $include] : [];
36+
$response = $this->getClient()->getRestClient()->post(
37+
$this->multipleResourceUrl(),
38+
[
39+
RequestOptions::JSON => $cartItem,
40+
RequestOptions::QUERY => $query,
41+
]
42+
);
43+
44+
return new CartResponse($response);
45+
}
46+
47+
public function update(CartItem $cartItem, ?string $include = null): CartResponse
48+
{
49+
$query = $include ? ['include' => $include] : [];
50+
$response = $this->getClient()->getRestClient()->put(
51+
$this->singleResourceUrl(),
52+
[
53+
RequestOptions::JSON => $cartItem,
54+
RequestOptions::QUERY => $query,
55+
]
56+
);
57+
58+
return new CartResponse($response);
59+
}
60+
61+
public function multipleResourceUrl(): string
62+
{
63+
return sprintf(self::CARTS_ENDPOINT, $this->getParentUuid());
64+
}
65+
66+
public function singleResourceUrl(): string
67+
{
68+
return sprintf(self::CART_ENDPOINT, $this->getParentUuid(), $this->getUuid());
69+
}
70+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace BigCommerce\ApiV3\Api\Carts;
4+
5+
use BigCommerce\ApiV3\Api\Generic\UuidResourceWithUuidParentApi;
6+
use BigCommerce\ApiV3\ResponseModels\Cart\CartRedirectUrlsResponse;
7+
8+
class CartRedirectUrlsApi extends UuidResourceWithUuidParentApi
9+
{
10+
private const REDIRECT_URL = 'carts/%d/redirect_urls';
11+
12+
public function create(): CartRedirectUrlsResponse
13+
{
14+
$response = $this->getClient()->getRestClient()->put(
15+
sprintf(self::REDIRECT_URL, $this->getParentUuid())
16+
);
17+
18+
return new CartRedirectUrlsResponse($response);
19+
}
20+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
3+
namespace BigCommerce\ApiV3\Api\Carts;
4+
5+
use BigCommerce\ApiV3\Api\Generic\CreateResource;
6+
use BigCommerce\ApiV3\Api\Generic\DeleteResource;
7+
use BigCommerce\ApiV3\Api\Generic\GetResource;
8+
use BigCommerce\ApiV3\Api\Generic\UuidResourceApi;
9+
use BigCommerce\ApiV3\ResourceModels\Cart\Cart;
10+
use BigCommerce\ApiV3\ResponseModels\Cart\CartResponse;
11+
use GuzzleHttp\RequestOptions;
12+
13+
class CartsApi extends UuidResourceApi
14+
{
15+
use GetResource;
16+
use CreateResource;
17+
use DeleteResource;
18+
19+
private const CARTS_ENDPOINT = 'carts';
20+
private const CART_ENDPOINT = 'carts/%s';
21+
22+
public function get(): CartResponse
23+
{
24+
return new CartResponse($this->getResource());
25+
}
26+
27+
public function create(Cart $cart): CartResponse
28+
{
29+
return new CartResponse($this->createResource($cart));
30+
}
31+
32+
public function updateCustomerId(int $customerId): CartResponse
33+
{
34+
$response = $this->getClient()->getRestClient()->put(
35+
$this->singleResourceUrl(),
36+
[
37+
RequestOptions::JSON => ['customer_id' => $customerId],
38+
]
39+
);
40+
41+
return new CartResponse($response);
42+
}
43+
44+
public function singleResourceUrl(): string
45+
{
46+
return sprintf(self::CART_ENDPOINT, $this->getUuid());
47+
}
48+
49+
public function multipleResourceUrl(): string
50+
{
51+
return self::CARTS_ENDPOINT;
52+
}
53+
54+
public function item(string $id): CartItemsApi
55+
{
56+
$itemsApi = $this->items();
57+
$itemsApi->setUuid($id);
58+
59+
return $itemsApi;
60+
}
61+
62+
public function items(): CartItemsApi
63+
{
64+
$itemsApi = new CartItemsApi($this->getClient());
65+
$itemsApi->setParentUuid($this->getUuid());
66+
67+
return $itemsApi;
68+
}
69+
70+
public function redirectUrls(): CartRedirectUrlsApi
71+
{
72+
$redirectsApi = new CartRedirectUrlsApi();
73+
$redirectsApi->setParentUuid($this->getUuid());
74+
75+
return $redirectsApi;
76+
}
77+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
namespace BigCommerce\ApiV3\Api\Generic;
4+
5+
abstract class UuidResourceWithUuidParentApi extends UuidResourceApi
6+
{
7+
private string $parentUuid;
8+
9+
public function getParentUuid(): string
10+
{
11+
return $this->parentUuid;
12+
}
13+
14+
public function setParentUuid(string $parentUuid): void
15+
{
16+
$this->parentUuid = $parentUuid;
17+
}
18+
}

src/BigCommerce/Client.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace BigCommerce\ApiV3;
44

5+
use BigCommerce\ApiV3\Api\Carts\CartsApi;
56
use BigCommerce\ApiV3\Api\Catalog\CatalogApi;
67
use BigCommerce\ApiV3\Api\Orders\OrdersApi;
78
use BigCommerce\ApiV3\Api\Customers\CustomersApi;
@@ -129,6 +130,18 @@ public function theme(string $uuid): ThemesApi
129130
return $api;
130131
}
131132

133+
public function carts(): CartsApi
134+
{
135+
return new CartsApi($this);
136+
}
137+
138+
public function cart(string $uuid): CartsApi
139+
{
140+
$api = $this->carts();
141+
$api->setUuid($uuid);
142+
return $api;
143+
}
144+
132145
public function order(int $orderId): OrdersApi
133146
{
134147
return new OrdersApi($this, $orderId);
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace BigCommerce\ApiV3\ResourceModels\Cart;
4+
5+
use BigCommerce\ApiV3\ResourceModels\ResourceModel;
6+
7+
class Cart extends ResourceModel
8+
{
9+
public string $id;
10+
public string $parent_id;
11+
public int $customer_id;
12+
public string $email;
13+
public $currency;
14+
public bool $tax_included;
15+
public float $base_amount;
16+
public float $discount_amount;
17+
public float $cart_amount;
18+
public array $coupons;
19+
public array $discounts;
20+
public $line_items;
21+
public string $created_time;
22+
public string $updated_time;
23+
public int $channel_id;
24+
public string $locale;
25+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace BigCommerce\ApiV3\ResourceModels\Cart;
4+
5+
use BigCommerce\ApiV3\ResourceModels\ResourceModel;
6+
7+
class CartItem extends ResourceModel
8+
{
9+
public array $line_items;
10+
public array $gift_certificates;
11+
public array $custom_items;
12+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace BigCommerce\ApiV3\ResourceModels\Cart;
4+
5+
use BigCommerce\ApiV3\ResourceModels\ResourceModel;
6+
7+
class CartRedirectUrls extends ResourceModel
8+
{
9+
public string $cart_url;
10+
public string $checkout_url;
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace BigCommerce\ApiV3\ResponseModels\Cart;
4+
5+
use BigCommerce\ApiV3\ResourceModels\Cart\CartRedirectUrls;
6+
use BigCommerce\ApiV3\ResponseModels\SingleResourceResponse;
7+
use stdClass;
8+
9+
class CartRedirectUrlsResponse extends SingleResourceResponse
10+
{
11+
private CartRedirectUrls $cartRedirectUrls;
12+
13+
public function getCartRedirectUrls(): CartRedirectUrls
14+
{
15+
return $this->cartRedirectUrls;
16+
}
17+
18+
protected function addData(stdClass $rawData): void
19+
{
20+
$this->cartRedirectUrls = new CartRedirectUrls($rawData);
21+
}
22+
}

0 commit comments

Comments
 (0)