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

Tuesday, May 10, 2022

[FIXED] How to programmatically grab the product description in WooCommerce?

 May 10, 2022     php, product, woocommerce, wordpress     No comments   

Issue

I saw this, WooCommerce - Get the product description by product_id

But I was hoping there was a shorter way to get the product by ID and get the description? Any idea?

I'm using WooCommerce 3.0 and above.


Solution

In your question, we don't know what do you mean by "GRAB"… It can be how to SET (or how to GET) the product description.


There is multiple ways to SET the product description.

1) From the product ID using Wordpress wp_update_post();

  wp_update_post( array('ID' => $product_id, 'post_content' => 'This is the <strong>product description</strong> content.') );

2) From the WC_Product Object using set_description() method

// Get the WC_Product Object instance (optional if needed)
$product = wc_get_product( $product_id );

// Set the description
$product->set_description('This is the <strong>product description</strong> content.');

$product->save(); // Save product data

There is multiple ways to GET the product description (as explained in this thread).

1) From the product ID, using Wordpress get_post() function:

 $product_description = get_post( $product_id )->post_content;

2) From the WC_Product Object using get_description() method

// Get the WC_Product Object instance (optional if needed)
$product = wc_get_product( $product_id );

// Get the product description
$description = $product->get_description();


Answered By - LoicTheAztec
Answer Checked By - Senaida (PHPFixing Volunteer)
  • 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