PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 28, 2022

[FIXED] WooCommerce how to update product category description and thumbnail ID

 January 28, 2022     php, taxonomy-terms, woocommerce, woocommerce-theming, wordpress     No comments   

Issue

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


Solution

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'] );
}


Answered By - LoicTheAztec
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing