6

I'm using this API to create orders in WooCommerce: https://github.com/kloon/WooCommerce-REST-API-Client-Library

When I'm adding an order:

$orderData = array(
    "order" => array(
        "line_items" => array( 
            array(
                "product_id" => 1, 
                "quantity" => 1
            ) 
        )
    )
);

$client->orders->create($orderData);

everything works fine, the order is created in WooCommerce.

But When I want to add a product variation with meta data about the variation, how should I do that?

I tried several things, including:

$orderData = array(
    "order" => array(
        "line_items" => array( 
            array(
                "product_id" => 1, 
                "quantity" => 1,
                "variation_id" => 2,
                "variations" => array(
                    "color" => "black"
                )
            ) 
        )
    )
);

$client->orders->create($orderData);

What I want to achieve is, when getting the order with:

$client->orders->get( $order_id );

The color info is already added to the meta data of the line item (so the color description is visible in the order details when sending an email):

line_items: [
    {
        id: ...,
        subtotal: "...",
        subtotal_tax: "...",
        total: "...",
        total_tax: "...",
        price: "...",
        quantity: 1,
        tax_class: null,
        name: "Product name",
        product_id: 1,
        sku: "",
        meta: [
            {
                key: "color",
                label: "Color",
                value: "black"
            }
        ]
    }
]

Hope the question is clear enough and someone can point me to the right solution :)

Thanks for your patience to read this.

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
Dirk Pennings
  • 1,074
  • 9
  • 18

1 Answers1

0

You can't specify the product variation data when placing a order, the product variation should already exist and shall be referred to using the variation ID.

For example if you would place a order of the "black" variation (say it has variation ID 12):

"line_items": [
  {
    "product_id": 1,
    "variation_id": 12,
    "quantity": 1
  }
]

Adding metadata to a product variation can not be done using the orders endpoint, use the products endpoint for updating products.

Nils Wasell
  • 947
  • 1
  • 8
  • 10