|
1 | 1 | # BigCommerce V3 Api Library |
2 | 2 |
|
3 | | - |
4 | | - |
| 3 | +[](https://github.com/aligent/bigcommerce-v3-api-php-client/releases) |
| 4 | +[](https://packagist.org/packages/aligent/bigcommerce-api-client) |
5 | 5 |  |
6 | | - |
7 | | - |
8 | | - |
| 6 | +[](https://github.com/aligent/bigcommerce-v3-api-php-client/blob/main/LICENSE.md) |
| 7 | +[](https://github.com/aligent/bigcommerce-v3-api-php-client/actions/workflows/php.yml) |
9 | 8 |
|
10 | 9 | ## Introduction |
11 | 10 |
|
12 | | -This is an easy-to-use API client for BigCommerce. |
| 11 | +This is an easy-to-use API client for [BigCommerce](https://developer.bigcommerce.com/api-reference). |
13 | 12 |
|
14 | 13 | ## Installation |
15 | 14 |
|
16 | | -Install via _composer_: `composer require aligent/bigcommerce-api-client`. |
| 15 | +Install [aligent/bigcommerce-api-client ](https://packagist.org/packages/aligent/bigcommerce-api-client) |
| 16 | +from packagist using [composer](https://getcomposer.org/): `composer require aligent/bigcommerce-api-client`. |
17 | 17 |
|
18 | | -## Usage |
| 18 | +## Usage Examples |
19 | 19 |
|
20 | 20 | Trivial example of updating a product name: |
21 | 21 |
|
@@ -75,68 +75,105 @@ try { |
75 | 75 | } |
76 | 76 | ``` |
77 | 77 |
|
78 | | -## Development |
| 78 | +### API Design |
| 79 | + |
| 80 | +There are three components to the library: |
| 81 | + |
| 82 | +- [BigCommerce/Api](./src/BigCommerce/Api) - which represent the API endpoints and tries to mimic the layout of the |
| 83 | + documentation. |
| 84 | + |
| 85 | +- [BigCommerce/ResourceModels](./src/BigCommerce/ResourceModels) - which represent the resources that are sent to and |
| 86 | + received from the API, for example a `Product` or an `Order`. |
| 87 | + |
| 88 | +- [BigCommerce/ResponseModels](./src/BigCommerce/ResponseModels) - which represent the responses from the BigCommerce |
| 89 | + API. |
| 90 | + |
| 91 | +For additional documentation, see the [code documentation](https://aligent.github.io/bigcommerce-v3-api-php-client/). |
| 92 | + |
| 93 | +#### API Classes |
| 94 | + |
| 95 | +To interact with the API, always start with the [BigCommerce\ApiV3\Client](./src/BigCommerce/Client.php) class. All APIs |
| 96 | +can be accessed in two ways: with and without an ID. |
| 97 | + |
| 98 | +If you are querying about a specific resource instance (e.g. Product 5), then you would use singular endpoint ( |
| 99 | +`->catalog()->product(5)`), otherwise you would use the plural endpoint (i.e. `->catalog()->products()`). |
| 100 | + |
| 101 | +For example, suppose we want to find all the metafields for a brand with and id of `123`. Our query is for a _specific_ |
| 102 | +brand, but any metafield, so the call would look like: |
| 103 | + |
| 104 | +```php |
| 105 | +$api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']); |
| 106 | + |
| 107 | +$metafieldsResponse = $api->catalog()->brand(123)->metafields()->getAll(); |
| 108 | +$metafields = $metafieldsResponse->getMetafields(); |
| 109 | +``` |
79 | 110 |
|
80 | | -Running tests: `composer run-script test` |
| 111 | +Suppose we now want to delete metafield `456` on brand `123`. Now our query is for a _specific_ brand and a _specific_ |
| 112 | +metafield. |
81 | 113 |
|
| 114 | +```php |
| 115 | +$api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']); |
82 | 116 |
|
83 | | -## Coverage |
| 117 | +$api->catalog()->brand(123)->metafield(456)->delete(); |
| 118 | +``` |
84 | 119 |
|
85 | | -### Store Management |
| 120 | +#### Resource Model Classes |
86 | 121 |
|
87 | | -#### Customers |
| 122 | +The resource models represent the resources we provided to the API and the responses we receive. |
88 | 123 |
|
89 | | -- ☑️ Customers |
90 | | -- ☑️ Addresses |
91 | | -- ☑️ Attributes |
92 | | -- ☑️ Attribute Values |
93 | | -- ☑️ Form Field Values |
94 | | -- ☑️ Consent |
| 124 | +To create a new resource, simply instantiate a new object of the correct resource model and then send it to the create |
| 125 | +endpoint. For example, if we want to create a new brand: |
95 | 126 |
|
96 | | -#### Orders (V3) |
| 127 | +```php |
| 128 | +$api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']); |
97 | 129 |
|
98 | | -- ☑️ Order Metafields |
99 | | -- ☑️ Transactions |
100 | | -- ☑️ Order Refunds |
| 130 | +$brand = new BigCommerce\ApiV3\ResourceModels\Catalog\Brand\Brand(); |
| 131 | +$brand->name = "My Brand"; |
| 132 | +$brand->meta_description = "My wonderful brand"; |
101 | 133 |
|
102 | | -#### Payment Methods |
| 134 | +$api->catalog()->brands()->create($brand); |
| 135 | +``` |
103 | 136 |
|
104 | | -- ☑️ Payment Access Token |
105 | | -- ☑️ Payment Methods |
| 137 | +#### Response Model Classes |
106 | 138 |
|
107 | | -#### Scripts |
| 139 | +Responses from the API all use similar response classes for consistency. There are two types generally: singular responses, |
| 140 | +and plural responses. Singular responses will have a single method in the format `get<resource>()`, |
| 141 | +for example (`ProductResponse::getProduct()`). Plural responses will have two methods, a `getPagination()` |
| 142 | + and `get<resources>()` (e.g. `ProductsResponse::getProducts()`). |
108 | 143 |
|
109 | | -- ☑️ Scripts |
| 144 | +Note that the API request is sent when the action is called and the response |
| 145 | +is returned. |
110 | 146 |
|
111 | | -#### Subscribers |
| 147 | +```php |
| 148 | +$api = new BigCommerce\ApiV3\Client($_ENV['hash'], $_ENV['CLIENT_ID'], $_ENV['ACCESS_TOKEN']); |
112 | 149 |
|
113 | | -- ☑️ Subscribers |
| 150 | +// Singular Responses |
| 151 | +$category = $api->catalog()->category(456)->get()->getCategory(); |
| 152 | +$brand = $api->catalog()->brand(123)->get()->getBrand(); |
114 | 153 |
|
115 | | -#### Themes |
| 154 | +// Plural Responses |
| 155 | +$categoryResponse = $api->catalog()->categories()->getAll(limit: 10); |
| 156 | +$totalCategories = $categoryResponse->getPagination()->total; |
| 157 | +$categories = $categoryResponse->getCategories(); |
116 | 158 |
|
117 | | -- ☑️ Themes |
118 | | -- ☑️ Theme Actions |
119 | | -- ☑️ Theme Jobs |
| 159 | +$brands = $api->catalog()->brands()->getAll()->getBrands(); |
| 160 | +``` |
120 | 161 |
|
121 | | -#### Widgets |
| 162 | +## Development |
122 | 163 |
|
123 | | -- ☑️ Regions |
124 | | -- ☑️ Widget Template |
125 | | -- ☑️ Widget |
126 | | -- ☑️ Placement |
| 164 | +- Running tests: `composer run-script test` |
| 165 | +- Checking PHP style rules: `composer run-scruot check-style` |
| 166 | +- Auto fix code style rules: `composer run-script fix-style` |
127 | 167 |
|
128 | | -### Catalog |
| 168 | +### Writing Tests |
129 | 169 |
|
130 | | -#### Catalog API |
| 170 | +All tests are located in the _tests_ folder in the namespace `BigCommerce\Tests`. The namespace should match the class |
| 171 | +being tested after this, e.g. `BigCommerce\Tests\Api\Carts` for testing `BigCommerce\ApiV3\Api\Carts\CartsApi`. |
131 | 172 |
|
132 | | -- ☑️ Brands |
133 | | -- ☑️ Category |
134 | | -- ☑️ Product |
135 | | -- ☑️ Summary |
136 | | -- ☑️ Variants |
| 173 | +Responses can be mocked using the `BigCommerceApiTest::setReturnData()` function then you can inspect the request that |
| 174 | +was made with `BigCommerceApiTest::getLastRequest()`. Response JSON files are stored in _tests/BigCommerce/responses_. |
137 | 175 |
|
138 | | -#### Price Lists |
| 176 | +## Full Documentation |
139 | 177 |
|
140 | | -- ☑️ Price Lists |
141 | | -- ☑️ Assignments |
142 | | -- ☑️ Records |
| 178 | +If you would like to have full class documentation, run |
| 179 | +`docker run --rm -v /path/to/vendor/aligent/bigcommerce-api:/data phpdoc/phpdoc:3 run -d /data/src -t /data/docs --defaultpackagename BigCommerce --visibility public` |
0 commit comments