2

I've got an array of data with which I'd like to update my products categories (taxanomy) metadata. Specifically, I'm trying to update description as well as thumbnail url values. I tried to use multiple wordpress functions but none of them worked! I didn't get any error but those values didn't get updated either.

$row_data = array(
      'Term ID' => 150,
      'Name' => "my 1st category",
      'Slug' => "my-1st-category",
      'Term URI' => "",
      'Parent Term ID' => "",
      'Description' => "My best description on this category that would change your life forever!",
      'Display Type' => "",
      'Image' => "https://myexample.site/wp-content/"
    );

// This did not work!
wp_update_term($row_data['Term ID'], 'product_cat', $row_data);  

// This did not work either!
update_term_meta($row_data['Term ID'], 'description', $row_data['Description']);  

// This did not work either!
update_woocommerce_term_meta($row_data['Term ID'],  'thumbnail_id',  $row_data['Image']);

Is there something that i'm missing?

Is thumbnail_id the right field name that i'm using here?

Is update_woocommerce_term_meta the right function for updating the thumbnail url?

Thank you.

enter image description here

Ruvee
  • 8,611
  • 4
  • 18
  • 44
  • `update_term_meta($row_data['Term ID'], 'description', $row_data['Description']);` working fine for me. – Bhautik Apr 07 '21 at 07:04
  • @Bhautik Thank you for taking a look at this. Is that right? hmmmmmm interesting! Can't get it to work on my end! What about the other one? Do you happen to know anything about the ```thumbnail url``` field? Can't find its field name in the database either, neither can i find the "right" function for updating that. – Ruvee Apr 07 '21 at 12:03
  • Where you did this code? – Bhautik Apr 07 '21 at 13:28
  • @Bhautik I ran it on a page of my website (in development). i didn't hook it to any action/filter hook. – Ruvee Apr 07 '21 at 13:32

1 Answers1

3

The main problem come from your array keys that are wrong to insert or update a term.

From your custom array (updated):

$row_data = array(
    'Term ID'        => 150,
    'Name'           => 'my 1st category',
    'Slug'           => 'my-1st-category',
    'Taxonomy'       => 'product_cat'
    'Parent Term ID' => 0,
    'Description'    => __("My best description on this category that would change your life forever!", "woocommerce"),
    'Display Type'   => "",
    'Image ID'       => "356",
);
  1. To update term description, use WordPress wp_update_term() function as follows:
wp_update_term( $row_data['Term ID'], $row_data['Taxonomy'], array('description' => $row_data['Description']) );
  1. To update the thumbnail ID (and not an Image URL), use update_term_meta() function as follows:
update_term_meta( $row_data['Term ID'], 'thumbnail_id', $row_data['Image ID'] ); 

Both are tested an works.


To insert a new term you will use WordPress wp_insert_term() function like:

$term_data = wp_insert_term( $row_data['Name'], $row_data['Taxonomy'], array(
    'description' => $row_data['Description'],
    'slug'        => $row_data['Slug'],
    'parent'      => $row_data['Parent Term ID'],
) );

if ( ! empty($row_data['Display Type']) ) {
    update_term_meta( $row_data['Term ID'], 'display_type', $row_data['Display Type'] );

if ( ! empty($row_data['Image ID']) ) {
    update_term_meta( $row_data['Term ID'], 'thumbnail_id', $row_data['Image ID'] );
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Thanks so much for debugging this, I really appreciate it. For "description" it worked. I'm sure for the "'thumbnail_id'" would work too. But the problem is, I have the actual "url" to the thumbnail, I don't have the "id" of that thumbnail. I guess I have to find a way to find the ```thumbnail id``` using the ```thumbnail url```. – Ruvee Apr 07 '21 at 16:37