1

I am adding products via Woocommerce Rest Api v2. But I am unable to add custom fields via the Api.

please suggest me the best way to do that.

The json response:

$data = array(
   // 'product' => array(
        'id' => 8555,
        'title' => 'Notebook',
        'type' => 'simple',
        'product_id' => 8555,
        'regular_price' => '21.99',
        'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
        'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
        'categories' => array("General"),
        'product_meta' => array(
            'custom_products_key' => 'custom_product_value',

            )
);

product_meta is not working…

1 Answer 1

7

You should normally replace 'product_meta' by 'meta_data' and separate the key from the value.

For example this will add 2 custom fields to your product:

    'meta_data' => [
        [
            'key' => 'custom_key'
            'value' => 'custom_value'
        ]
        [
            'key' => 'custom_key2'
            'value' => 'custom_value2'
        ]
    ]

To create a product using latest Woocommerce Rest API v2 (with PHP), setting the good category ID in the following, will be:

$data = [
    'name' => 'Notebook',
    'type' => 'simple',
    'regular_price' => '21.99',
    'description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.',
    'short_description' => 'Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.',
    'categories' => [
        [
            'id' => 9
        ]
    ],
    'meta_data' => [
        [
            'key' => 'custom_key'
            'value' => 'custom_value'
        ]
        [
            'key' => 'custom_key2'
            'value' => 'custom_value2'
        ]
    ]
];

print_r($woocommerce->post('products', $data));

This should work.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.