openy / commerce_cart_api
提供了一种RESTful接口,通过轻量级公开API与Drupal Commerce中的购物车进行交互。
dev-8.x-1.x
2023-08-11 09:41 UTC
Requires
- drupal/commerce: ~2.17
This package is auto-updated.
Last update: 2024-09-11 12:14:25 UTC
README
沙箱提供通过轻量级公开API与Drupal Commerce中的购物车进行交互的RESTful接口。
如何测试
- 将此存储库克隆到您的Drupal Commerce测试项目
- 将
$settings['extension_discovery_scan_tests'] = TRUE;
添加到您的settings.php - 启用
commerce_cart_api
- 启用
commerce_cart_reactjs
- 使用
commerce_cart_reactjs
删除默认购物车块 - 购物车表单自动交换。
GET (购物车)
curl 'http://localhost:32775/cart?_format=json'
上述示例的JSON响应。
[ { "order_id": 2, "uuid": "be9cb309-2c77-4f85-9444-013c91ab9318", "order_number": null, "store_id": 1, "total_price": { "number": "403.97", "currency_code": "USD" }, "order_items": [ { "order_item_id": 9, "uuid": "dcaa6901-f799-4505-af4c-9d1db9a85bbb", "type": "physical_product_variation", "purchased_entity": 4, "title": "Drupal Commerce Hoodie - Green, Small", "quantity": "2.00", "unit_price": { "number": "52.00", "currency_code": "USD" }, "total_price": { "number": "104.00", "currency_code": "USD" } }, { "order_item_id": 21, "uuid": "3bb80d13-ec7d-4e69-af33-a2f014c5e284", "type": "physical_product_variation", "purchased_entity": 15, "title": "Pronto600 Instant Camera", "quantity": "3.00", "unit_price": { "number": "99.99", "currency_code": "USD" }, "total_price": { "number": "299.97", "currency_code": "USD" } } ] } ]
POST
添加项目到购物车的示例。创建或添加到现有购物车。可选:将“combine”属性添加到负载(默认为TRUE)。FALSE将创建另一个订单项目,而TRUE将增加现有项目。
curl -X POST \ 'http://localhost:32775/cart/add?_format=json' \ -H 'Content-Type: application/json' \ -d '[{ "purchased_entity_type": "commerce_product_variation", "purchased_entity_id": "6", "quantity": "1", "combine": true }]'
响应
[ { "order_id": 7, "uuid": "d0e68a71-780d-422e-b951-5ffc980bd296", "order_number": null, "store_id": 1, "total_price": { "number": "174.00", "currency_code": "USD" }, "order_items": [ { "order_item_id": 26, "uuid": "ec920271-51f7-4869-8e8f-46bdd733ba58", "type": "physical_product_variation", "purchased_entity": 21, "title": "24\" x 36\" Hot Air Balloons", "quantity": "2.00", "unit_price": { "number": "35.00", "currency_code": "USD" }, "total_price": { "number": "70.00", "currency_code": "USD" } }, { "order_item_id": 27, "uuid": "ac0a2890-38ab-4332-8425-8df7f1458552", "type": "physical_product_variation", "purchased_entity": 6, "title": "Drupal Commerce Hoodie - Green, Large", "quantity": "2.00", "unit_price": { "number": "52.00", "currency_code": "USD" }, "total_price": { "number": "104.00", "currency_code": "USD" } } ] } ]
PATCH
一个JSON对象,其中包含订单项目ID,其值是定义项目新数量的对象。
{
3: {"quantity": 1},
9: {"quantity": 2},
}
curl 'http://localhost:32775/cart/1/items?_format=json' -X PATCH \ -H 'Content-Type: application/json' \ --data-binary '{"2":{"quantity":"1"},"3":{"quantity":"1.00"}}'
响应
{ "order_id": 2, "uuid": "be9cb309-2c77-4f85-9444-013c91ab9318", "order_number": null, "store_id": 1, "total_price": { "number": "303.98", "currency_code": "USD" }, "order_items": [ { "order_item_id": 9, "uuid": "dcaa6901-f799-4505-af4c-9d1db9a85bbb", "type": "physical_product_variation", "purchased_entity": 4, "title": "Drupal Commerce Hoodie - Green, Small", "quantity": "2.00", "unit_price": { "number": "52.00", "currency_code": "USD" }, "total_price": { "number": "104.00", "currency_code": "USD" } }, { "order_item_id": 21, "uuid": "3bb80d13-ec7d-4e69-af33-a2f014c5e284", "type": "physical_product_variation", "purchased_entity": 15, "title": "Pronto600 Instant Camera", "quantity": "2.00", "unit_price": { "number": "99.99", "currency_code": "USD" }, "total_price": { "number": "199.98", "currency_code": "USD" } } ] }
DELETE
从购物车中删除订单项目。
curl 'http://localhost:32775/cart/2/items/9?_format=json' -X DELETE
响应
没有正文,只有HTTP代码。