Get the Product description from WooCommerce? - php

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);

Related

How to get woocommerce product attribute description?

I want to get value from woocommerce product attribute description input.
I tried $discription = $attributes->description; and also $product_attribute['description'], but it doesn't work.
If you have the product object, you can get the description like so
$productObject->get_short_description();
Otherwise initialize the product with the ID first and then get the description
$productObject = wc_get_product( $productId );
$productObject->get_short_description();

Display product stock status on single product pages in WooCommerce when price field is empty

I'm adding a new stock status in WooCommerce products. It has been done by the help of How to add custom stock status to products in WooCommerce 4+ answer code.
But the problem is that when the price field of products is empty it doesn't show the status (front-end). but otherwise when the product has the price, it shows the status.
What can I do, to make the status be shown even if the price field is empty?
You will see that if the price is empty, besides not showing the product stock status, the price (the HTML code) for this will also not be displayed. Because the product is considered as not purchasable if the price is empty.
So in order to display the product stock status, you should make the product purchasable, if the price is empty. However, I do not believe this is your intention?
So instead of focusing on displaying the product stock status, it will be much simpler to display something else (HTML/text) where the price is normally displayed,
and that can be done by using the woocommerce_empty_price_html filter hook
So either you drop the whole idea of ​​the product stock status and simply use:
function filter_woocommerce_empty_price_html( $html, $product ) {
// NOT true on a single product page, RETURN
if ( ! is_product() ) return $html;
// Add HTML
$html = '<p>My text</p>';
return $html;
}
add_filter( 'woocommerce_empty_price_html', 'filter_woocommerce_empty_price_html', 10, 2 );
Or you build further on the product stock status and on that basis you get:
function filter_woocommerce_empty_price_html( $html, $product ) {
// NOT true on a single product page, RETURN
if ( ! is_product() ) return $html;
// Get stock status
$product_stock_status = $product->get_stock_status();
// Compare
if ( $product_stock_status == 'MY CUSTOM STATUS' ) {
// Add HTML
$html = '<p>My text based on product stock status</p>';
}
return $html;
}
add_filter( 'woocommerce_empty_price_html', 'filter_woocommerce_empty_price_html', 10, 2 );
The only downside to this solution(s) is that your new HTML/text will not be displayed where you normally see the product stock status, but where the price is normally displayed.

How to programmatically grab the product description in WooCommerce?

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();

Woocommerce add free products with ACF Post Object field

I am trying to build a custom functionality in Woocommerce with Advanced Custom Fields (ACF) plugin.
I created some code already but it is not working properly.
I want to select (simple) products at a Woocommerce product with ACF post object field. That products must be added free into the Woocommerce cart when that product will be added to the cart. Also when I add two or more items the free products must be added together.
I got also some images how the situation should work.
This is the ACF field setup.
For example: this is the product we give away.
This is the product we give away selected at a Woocommerce single product.
This is my actual code:
add_action('woocommerce_check_cart_items', 'free_products' );
function free_products() {
if( ( is_cart() || is_checkout () ) {
$free_product = get_field('gratis_producten'); // Incentive product we are giving away
$cart_id = WC()->cart->generate_cart_id( $free_product );
$free_products_in_cart = WC()->cart->find_product_in_cart( $cart_id );
if( $free_product ) {
// Removing existing "free products" from the cart.
WC()->cart->remove_cart_item( $free_products_in_cart );
// Adding to cart 40 free products
WC()->cart->add_to_cart( $free_product, 40 );
}
}
}
Any help will be highly appreciated.

Get the Product ID from email templates in WooCommerce 3

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

Categories