Skip to content

AI Agents & MCPs & AI Workflow Automation • (~400 MCP servers for AI agents) • AI Automation / AI Agent with MCPs • AI Workflows & AI Agents • MCPs for AI Agents

4.5 KB
Raw Blame
import { createAction, Property } from '@activepieces/pieces-framework';
import {
  AuthenticationType,
  HttpMethod,
  HttpRequest,
  httpClient,
} from '@activepieces/pieces-common';

import { wooAuth } from '../auth';
import { createOrderOutputSchema } from '../output-schemas';

export const wooCreateOrder = createAction({
  name: 'Create Order',
  classification: 'WRITE',
  displayName: 'Create Order',
  description: 'Create an order with one or more line items',
  audience: 'human',
  aiMetadata: {
    description:
      'Creates a new order in a WooCommerce store from a list of product IDs and quantities, with an optional customer, status, billing email, and payment gateway (payment method id, with an optional display title). Use when an agent needs to place an order on a customer behalf. Not idempotent: each call creates a separate order.',
    idempotent: false,
  },
  auth: wooAuth,
  outputSchema: createOrderOutputSchema,
  propertyGroups: [
    {
      key: 'items',
      display: 'section',
      label: 'Products',
      icon: 'inbox',
      props: ['line_items'],
    },
    {
      key: 'order',
      display: 'section',
      label: 'Order',
      icon: 'user',
      props: ['customer_id', 'status'],
    },
    {
      key: 'payment',
      display: 'section',
      label: 'Payment',
      icon: 'send',
      props: ['billing_email', 'payment_method', 'payment_method_title'],
    },
  ],
  props: {
    line_items: Property.Array({
      displayName: 'Line Items',
      description: 'Products to include in the order',
      required: true,
      properties: {
        product_id: Property.ShortText({
          displayName: 'Product ID',
          description: 'Find it in WooCommerce under Products.',
          placeholder: '123',
          required: true,
        }),
        quantity: Property.Number({
          displayName: 'Quantity',
          required: true,
          defaultValue: 1,
        }),
      },
    }),
    customer_id: Property.ShortText({
      displayName: 'Customer ID',
      description: 'Leave empty for a guest order',
      placeholder: '123',
      width: 'half',
      required: false,
    }),
    status: Property.StaticDropdown({
      displayName: 'Status',
      description: 'Status the order starts with.',
      width: 'half',
      required: false,
      options: {
        options: [
          { label: 'Pending payment', value: 'pending' },
          { label: 'Processing', value: 'processing' },
          { label: 'On hold', value: 'on-hold' },
          { label: 'Completed', value: 'completed' },
        ],
      },
    }),
    billing_email: Property.ShortText({
      displayName: 'Billing Email',
      description: 'Where WooCommerce sends the order emails.',
      placeholder: '[email protected]',
      required: false,
    }),
    payment_method: Property.ShortText({
      displayName: 'Payment Method ID',
      description: 'Gateway ID, e.g. bacs, cheque or cod.',
      placeholder: 'bacs',
      width: 'half',
      required: false,
    }),
    payment_method_title: Property.ShortText({
      displayName: 'Payment Method Title',
      description: 'Name the customer sees, e.g. Direct bank transfer.',
      placeholder: 'Direct bank transfer',
      width: 'half',
      required: false,
    }),
  },
  async run(configValue) {
    const trimmedBaseUrl = configValue.auth.props.baseUrl.replace(/\/$/, '');
    const {
      line_items,
      customer_id,
      status,
      billing_email,
      payment_method,
      payment_method_title,
    } = configValue.propsValue;

    const body: Record<string, unknown> = {
      line_items: line_items.map((item) => {
        const entry = Object(item);
        return {
          product_id: Number(entry['product_id']),
          quantity: Number(entry['quantity']),
        };
      }),
    };
    if (customer_id) {
      body['customer_id'] = Number(customer_id);
    }
    if (status) {
      body['status'] = status;
    }
    if (billing_email) {
      body['billing'] = { email: billing_email };
    }
    if (payment_method) {
      body['payment_method'] = payment_method;
    }
    if (payment_method_title) {
      body['payment_method_title'] = payment_method_title;
    }

    const request: HttpRequest = {
      method: HttpMethod.POST,
      url: `${trimmedBaseUrl}/wp-json/wc/v3/orders`,
      authentication: {
        type: AuthenticationType.BASIC,
        username: configValue.auth.props.consumerKey,
        password: configValue.auth.props.consumerSecret,
      },
      body,
    };

    const res = await httpClient.sendRequest(request);

    return res.body;
  },
});