Browse Source

docs: Add missing part to digital products guide

Michael Bromley 2 years ago
parent
commit
8f5f189607
1 changed files with 38 additions and 0 deletions
  1. 38 0
      docs/docs/guides/how-to/digital-products/index.mdx

+ 38 - 0
docs/docs/guides/how-to/digital-products/index.mdx

@@ -215,6 +215,44 @@ export class DigitalShippingLineAssignmentStrategy implements ShippingLineAssign
 }
 ```
 
+### Define a custom OrderProcess
+
+In order to automatically fulfill any digital products as soon as the order completes, we can define a custom OrderProcess:
+
+```ts title="src/plugins/digital-products/config/digital-order-process.ts"
+import { OrderProcess, OrderService } from '@vendure/core';
+
+import { digitalFulfillmentHandler } from './digital-fulfillment-handler';
+
+let orderService: OrderService;
+
+/**
+ * @description
+ * This OrderProcess ensures that when an Order transitions from ArrangingPayment to
+ * PaymentAuthorized or PaymentSettled, then any digital products are automatically
+ * fulfilled.
+ */
+export const digitalOrderProcess: OrderProcess<string> = {
+    init(injector) {
+        orderService = injector.get(OrderService);
+    },
+    async onTransitionEnd(fromState, toState, data) {
+        if (
+            fromState === 'ArrangingPayment' &&
+            (toState === 'PaymentAuthorized' || toState === 'PaymentSettled')
+        ) {
+            const digitalOrderLines = data.order.lines.filter(l => l.productVariant.customFields.isDigital);
+            if (digitalOrderLines.length) {
+                await orderService.createFulfillment(data.ctx, {
+                    lines: digitalOrderLines.map(l => ({ orderLineId: l.id, quantity: l.quantity })),
+                    handler: { code: digitalFulfillmentHandler.code, arguments: [] },
+                });
+            }
+        }
+    },
+};
+```
+
 ### Complete plugin & add to config
 
 The complete plugin can be found here: [example-plugins/digital-products](https://github.com/vendure-ecommerce/vendure/tree/master/packages/dev-server/example-plugins/digital-products)