1

How to change / delete product short description in Woocommerce

I use

$short_desc = $post->post_excerpt;

to read the property but I cant change it with

 wp_set_object_terms($post->ID, $post->post_excerpt, 'new excerpt');
  • I guess you need to expand your code. Otherwise we can't tell you what might be wrong. Just wp_set_object_terms doesn't tell us anything, when we don't know how exactly you are using it. In what function, with what parameters, retrieved from where? Please update your question accordingly. Many thanks – leymannx May 11 at 7:55
  • Looking at codex.wordpress.org/Function_Reference/wp_set_object_terms I'm wondering what exactly are you trying to do with this function? – leymannx May 11 at 7:59
  • updated my question – Cameron A May 11 at 8:36
2

Th function wp_set_object_terms() will not work to set post object properties.

To add/update/delete product short description there is mainly 2 ways:

1) the WordPress way from a post ID using wp_update_post() function this way:

$product_id = $post->ID; // or get_the_id() or $product->get_id() (when $product is available);

// The product short description (for testing)
$new_short_description = "<strong>Here</strong> is is the product short description content."

// Add/Update/delete the product short description
wp_update_post( array('ID' => $product_id, 'post_excerpt' => $new_short_description ) );

2) The WooCommerce way from the WC_product Object using set_short_description():

// Optional: get the WC_product Object instance from the post ID
$product_id = $post->ID; // or get_the_id()
$product    = wc_get_product( $product_id );

// The product short description (for testing)
$new_short_description = "<strong>Here</strong> is is the product short description content."

$product->set_short_description( $new_short_description ); // Add/Set the new short description
$product->save(); // Store changes in database

To delete the product description you will set an empty string.


Related: How to programmatically grab the product description in WooCommerce?

1

You should have the $product global available to you, which lets you call set_short_description and then save to store the changes:

$product->set_short_description('some description');
$product->save();
  • can I use $post instead of $product ? – Cameron A May 11 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.