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.
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();
Related
How can I update a product by product_id in my functions file?
I tried using the below code but to no success:
$_pf = new WC_Product_Factory();
$product_id = wc_get_product_id_by_sku( $sku );
$_product = $_pf->get_product($product_id);
$_product->set_price('225');
Since WooCommerce 3, new WC_Product_Factory() with get_product() method is deprecated and replaced simply by the function wc_get_product().
To update price, you have to update the price and the regular price (or the price and the sale price)...
Also the save() method is necessary to grab the data at the end.
So to get the WC_Product Object from an existing product SKU and to use on it any available method, do the following:
$new_price = '225'; // New price
$_product_id = wc_get_product_id_by_sku( $sku );
if ( $_product_id > 0 ) {
// Get an instance of the WC_Product Object
$_product = wc_get_product( $_product_id );
$_product->set_regular_price($new_price); // Set the regular price
$_product->set_price($new_price); // Set the price
$_product->save(); // Save to database and sync
} else {
// Display an error (invalid Sku
printf('Invalid sku "%s"… Can not update price.', $sku);
}
Tested and works.
I am trying to show the price of a product in a post by code using HTML.
I am trying to utilize $product->get_price(). however, I need to be able to call the product by identifying it, using its code or something like it.
So Basically, all I want to do is to show the price of a particular product in the post by using a product ID as reference for example.
Any idea?
If you have the product's ID you can use that to create a product object:
$_product = wc_get_product( $product_id );
Then from the object you can run any of WooCommerce's product methods.
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
Hope this one helps.
you use wc_get_product function :
$_product = wc_get_product( $product_id );
$_product->get_regular_price();
$_product->get_sale_price();
$_product->get_price();
In Woocommerce I am trying to find out how to get the product id for a completed order inside the Customer completed order email template to store it as a PHP variable. This way I will be able to insert it into an external database.
I already tried $product->get_id(); but it does not work.
How can get the Product ID in WooCommerce 3+ from email templates?
You need to loop through Order items… Normally the WC_Order object is defined through the variable $order mostly defined everywhere in email templates.
So the code to get the product ID will be:
// Loop through order items
foreach( $order->get_items() as $item_id => $item ){
// Get the product ID
$product_id = $item->get_product_id();
// Get an instance of the WC_Product object
$product = $item->get_product();
// Get product title (name)
$product_name = $product->get_title(); // or $product->get_name();
// Get product price
$product_price = $product->get_price();
}
See this related thread: Get Order items and WC_Order_Item_Product in Woocommerce 3
I built a custom payment gateway extension for woocommerce and I'm posting all the data (item name, cost, quantity etc) in JSON format back to the payment provider.
However, I can't seem to get the item description to go through.
Any ideas?
You can have that in cart object items, when getting an instance of the WP_post object, this way:
foreach(WC()->cart->get_cart() as $cart_item){
// Get an instance of Product WP_Post object
$post_obj = get_post( $cart_item['product_id'] );
// HERE the product description
$product_desciption = $post_obj->post_content;
// The product short description
$product_short_desciption = $post_obj->post_excerpt;
}
WooCommerce: Adding the Product Short Description to Archive Pages or Show Products Short Description on Shop Page
function vitahc_excerpt_in_product_archives() {
the_excerpt();
}
add_action('woocommerce_after_shop_loop_item_title','vitahc_excerpt_in_product_archives', 10);
Trying to get Product Data on custom Template By product ID, right now i have this code to get Product Title.
$productId = 164;
echo $p_title = get_the_title( $productId );
looking for Short Description, Price, Product Image, Product Url, Product Brand. Or might be loop will be better but that loop should work with product static ID.
Thanks in advance.
You would probably be better served by creating a new product object.
$productId = 164;
$product = wc_get_product( $productId );
echo $product->get_title();
echo $product->get_price_html();
Note, that the short description is merely the post's post_excerpt. If using outside of the loop (where $post is automatically defined) then you would need to get the post directly.
$post = get_post( $productId );
echo apply_filters( 'woocommerce_short_description', $post->post_excerpt );
or alternatively, if you've already defined the product object you could do
echo apply_filters( 'woocommerce_short_description', $product->post->post_excerpt );
since the WooCommerce product class will automatically define the $product->post property with get_post().
apply_filters() means that functions can be attached at this point to modify the content of $product->post->post_content. At a minimum, I know that wpautop() is attached to the woocommerce_short_description filter to create paragraph breaks.