Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WooCommerce how to update product category description and thumbnail ID

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

like image 554
Ruvee Avatar asked Oct 16 '25 23:10

Ruvee


1 Answers

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'] );
}
like image 177
LoicTheAztec Avatar answered Oct 19 '25 13:10

LoicTheAztec



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!