@jsondrop /商务
Drupal Commerce类型和客户端 commerce_api 模块:购物车, 优惠券,结帐,航运和付款。
npm install @jsondrop/core @jsondrop/drupal @jsondrop/commerce
需要 commerce_api (8.x-1.x)在Drupal端。
目录
commerce_api重命名Commerce的资源类型: commerce_ 前缀
go和下划线变成了连字符,路径被复数化。
| Drupal | 资源类型 | 路径 |
|---|---|---|
commerce_product (默认) | product--default | /jsonapi/products/default |
commerce_product_variation (默认) | product-variation--default | /jsonapi/product-variations/default |
commerce_order (默认) | order--default | /jsonapi/orders/default |
commerce_order_item (默认) | order-item--default | /jsonapi/order-items/default |
commerce_store (在线) | store--online | /jsonapi/stores/online |
commerce_promotion_coupon | promotion-coupon | /jsonapi/promotion-coupons |
JSON:API Extras覆盖重命名。 在带有Extras的网站上,类型
保留Drupal的名称(commerce_product_variation--default),除非
资源配置则相反,站点将两者混合在一起。这两种类型都接受
命名;对于路径,使用 client.withDiscoveredPaths();对于类型名称
CommerceApi 发送, naming: 'drupal' (并下达命令,而不是
id,所以它们自己的类型被发送)。
withCommerceApi(client) 给出解析这些路径的客户端,以及
类型是按实体类型注册的,所以每个产品包都是
Product,每一种变化 ProductVariation等等。
import { JsonApiClient } from '@jsondrop/core';
import { withCommerceApi } from '@jsondrop/commerce';
const drupal = withCommerceApi(new JsonApiClient({ baseUrl: 'https://shop.example.com' }));
const products = await drupal.getCollection('product--default', { query: { include: ['variations'] } });
products[0].variations; // ProductVariation[], with resolved_price
购物车和结帐
import { CommerceApi, checkoutStatus, createCartToken } from '@jsondrop/commerce';
const commerce = new CommerceApi(drupal).withCartToken(token ?? createCartToken());
await commerce.addToCart({ type: 'product-variation--default', id: variationId, quantity: 2 });
const [cart] = await commerce.getCarts();
await commerce.applyCoupons(cart.id, 'SAVE10');
await commerce.updateCheckout(cart.id, {
email: '[email protected]',
billing_information: { address: { country_code: 'HR', locality: 'Zagreb', postal_code: '10000', address_line1: 'Ilica 1' } },
});
const { constraints, shippingRates, paymentOptions, ready } = checkoutStatus(await commerce.getCheckout(cart.id));
| 方法 | 端点 |
|---|---|
getCurrentStore() | GET /current-store |
getCarts(), getCart(id) | GET /carts, GET /carts/{order} |
addToCart(items) | POST /cart/add |
updateCartItem(order, item, { quantity }) | PATCH /carts/{order}/items/{item} |
removeCartItems(order, items) | DELETE /carts/{order}/items |
clearCart(order) | DELETE /carts/{order} |
applyCoupons(order, codes), removeCoupons(order) | PATCH / DELETE /carts/{order}/coupons |
getCheckout(order), updateCheckout(order, changes) | GET / PATCH /checkout/{order} |
getShippingMethods(order) | GET /checkout/{order}/shipping-methods |
createPayment(order, { gatewayId }) | POST /checkout/{order}/payment |
approvePayment(order, params) | GET /checkout/{order}/payment/approve (离线返回) |
订单 可以作为ID或订单本身传递(getCarts()
将它们返回);对于订单,发送其类型的请求使用真实类型
(order--regular,而不是配置的默认值)。
你自己的模型。 方法是泛型的,并使用包的类型进行类型化
默认情况下。如果您将响应映射到您自己的模型,请使用未类型化的响应:
commerce.getCarts<Resource>().
车令牌。 Commerce_api从不发出一个:客户端创建一个
(createCartToken())并将其发送为 Commerce-Cart-Token;车创建
下面的权利属于所有持有它的人。将它保存在httpOnly cookie中并调用
从服务器获取Drupal。看到 例子6.
支付。 createPayment 用付款支付订单的余额
仪表(现场和手动网关)和下订单。它返回
整个响应,所以一个网关模块用一个额外的步骤来响应
可以由调用方处理。场外网关重定向客户,并且
approvePayment 在他们返回时完成付款。
产品包
为 commerce_variation_bundle:
带有“变体包”特征的变体类型保存着包项
(bundle-item--{type}),每一个都是另一种变化的数量。
import { bundleItems, type VariationBundle } from '@jsondrop/commerce';
declare module '@jsondrop/core' {
interface ResourceTypes {
'product-variation--bundle': VariationBundle<'bundle'>;
}
}
const set = await drupal.getResource('product-variation--bundle', id, {
query: { include: ['bundle_items.variation'] },
});
set.bundle_discount; // percentage off the items
for (const { variation, quantity, unitPrice } of bundleItems(set)) {
// enabled items, with their variation and its resolved unit price
}
isVariationBundle(variation) 从普通的变化中区分包。添加一个
像任何变化一样捆在车上;模块从它的物品中定价。
其他商务模块
Commerce_api重命名每一个 commerce_* 实体类型,也有贡献类型。
COMMERCE_ENTITY_TYPES 列出了解决问题的途径(商业,
commerce_shipping commerce_variation_bundle);在commerce_api中添加其他的
命名:
const drupal = withCommerceApi(client, ['license', 'wishlist-item']);
// or
const commerce = new CommerceApi(client, { entityTypes: ['license'] });