3

I added a new field using a small plugin script... it works fine in Woocommerce interface and also shows up using the REST API to get a product

Here an excerpt of results using $woocommerce->get()

'meta_data' => 
    array (size=1)
      0 => 
        array (size=3)
          'id' => int 3293
          'key' => string 'kambusa' (length=7)
          'value' => string '123' (length=3)

using:

$data = [
    'regular_price' => '21.00',
    'meta_data' => [
        ['kambusa' => '456']
    ]
];

print_r($woocommerce->put('products/530', $data));

Updates only the price but ignores (without any error) the meta_data.

I searched the web all the morning but didn't find any clear solution (some suggested I have to register_meta(), made some tests but nothing changed (the meta data show also before registering))

Following the code used to create the field in the "admin" part of my plugin:

$args = array(
    'id'            => $this->textfield_id,
    'label'         => sanitize_text_field( 'Kambusa ID' ),
    'placeholder'   => $placeholder,
    'desc_tip'      => true,
   'description'    => $description,
);
woocommerce_wp_text_input( $args );

4 Answers 4

2

I tried this to update the meta data for woocommerce REST api wc/v3. You can send the data as an array instead of a JSON string.

$data = [
    'meta_data' => [
        [
            'key' => '-- the meta key you need --',
            'value' => '-- the meta value you want --'
        ],
    ],
];
Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried this?

$data = [
    'regular_price' => '21.00',
    'meta_data' => [
        ['key' => 'kambusa'],
        ['value' => '456']
    ]
];

Comments

1
{
  "name": "Test Product",
  "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
    },
    {
      "id": 14
    }
  ],
  "images": [
    {
      "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_front.jpg"
    },
    {
      "src": "http://demo.woothemes.com/woocommerce/wp-content/uploads/sites/56/2013/06/T_2_back.jpg"
    }
  ],
  "meta_data": [
    {
        "key": "test_meta1", 
        "value": "test_meta_val1"
    }
  ]
}

This how I managed to pass meta_data on POST wp-json/wc/v3/products (Create Product)

Comments

0

TRY THIS CODE:

$product_id = 123;

update_products_meta($product_id);    

function update_products_meta($product_id)
{

$connection = woo_connection();

$data = [
    'meta_data' => [
        [
            'key' => 'KNK',
            'value' => 'KIDZ N KIDZ'
        ],
    ],
];

$connection = woo_connection();

print_r($connection->put('products/' . $product_id, $data));
}

1 Comment

This code could use some clarification on what woo_connection() exactly is

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.