WooCommerce - Get the product description by product_id - php

How can I get the product description or the product object using product ID.
$order = new WC_Order($order_id);
foreach ($order->get_items() as $item) {
$product_description = get_the_product_description($item['product_id']); // is there any method can I use to fetch the product description?
}
The code above extends class WC_Payment_Gateway
Any help would be greatly appreciated.

$order = new WC_Order($order_id);
foreach ($order->get_items() as $item)
{
$product_description = get_post($item['product_id'])->post_content; // I used wordpress built-in functions to get the product object
}

If you are using WooCommerce 3.0 or above, then you can get description with the below code.
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item) {
$product_id = $item['product_id'];
$product_details = $product->get_data();
$product_full_description = $product_details['description'];
$product_short_description = $product_details['short_description'];
}
Alternate way (recommended)
$order = wc_get_order($order_id);
foreach ($order->get_items() as $item) {
$product_id = $item['product_id'];
$product_instance = wc_get_product($product_id);
$product_full_description = $product_instance->get_description();
$product_short_description = $product_instance->get_short_description();
}
Hope this helps!

Related

Get category ID from order item inside woocommerce hook

In functions.php we have:
add_action( 'woocommerce_order_status_processing', 'mysite_processing', 10, 1);
then inside this function we get order items:
function mysite_processing($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
}
}
After getting the product id, we want to get the category id that product belongs. We already try with get_the_terms and has_term functions, both didn't work.
Solved, added to items for: $product = wc_get_product( $product_id );
Try below code
function mysite_processing($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$product_id = $item->get_product_id();
$term_list = wp_get_post_terms($product_id,'product_cat',array('fields'=>'ids'));
print_r($term_list);
$cat_id = (int) $term_list[0];
echo $cat_id;
die();
}
}

how to get woocommerce shipping class price/cost using shipping class id

I am trying to get shipping class cost assigned to products.
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ($items as $item) {
$product = new WC_Product($item['product_id']);
$shipping_class_id = $product->get_shipping_class_id();
}
i don't know how to get the cost of the shipping class based on its id can anyone help me?
you can try this
foreach ($order->get_items('shipping') as $item_id => $shipping)
{
$method_id = $shipping->get_method_id();
$name = $shipping->get_name();
$total = $shipping->get_total();
}

Get SKU of product in cart Wordpress

I'm trying to get the SKU of all the products in my cart.
In the past i tried something similar with the product names. I did it like this:
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$product_names=array();
foreach($items as $item => $values) {
$_product = $values['data']->post;
$product_names[]=$_product->post_title;
}
Is there a way like this but instead of the title get the SKU of a product?
EDIT:
I tried Thoby's way like this :
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$product_names=array();
$productsku=array();
foreach($items as $item => $values) {
$_product = $values['data']->post;
$product_names[]=$_product->post_title;
$productsku[]= $_woo_product->get_sku();
}
But i get an error :
Fatal error: Call to undefined method WP_Post::get_sku()
Try :
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$product_names = array();
foreach($items as $item => $values) {
// Retrieve WC_Product object from the product-id:
$_woo_product = wc_get_product( $values['product_id'] );
// Get SKU from the WC_Product object:
$product_names[] = $_woo_product->get_sku();
}
The official WooCommerce API docs: wc_get_product()
I believe this might help you-
$_product->get_sku();
It works with product loops and you can use it inside your cart/checkout/etc..
I assume you already have access to $cart
$cart_items = $cart->get_cart_contents();
$skus = array();
foreach($cart_items as $cart_item) {
// Get WC_Product by Variation ID, if not avaiable use Product ID
$product = wc_get_product($cart_item["variation_id"] ? $cart_item["variation_id"] :
$cart_item["product_id"]);
// Add new SKU to array
array_push($skus, $product->get_sku());
}
The easiest way to get the SKU of a product when you have access to the cart items:
$cart = WC()->cart->get_cart();
foreach( $cart as $cart_item_key => $cart_item ){
$product = $cart_item['data'];
$product->get_sku();
}

How to get categories from an order at checkout in WooCommerce?

I am want to get the category of the items in the cart at the checkout in WooCommerce. I want to extract it and then place it in a field in my custom checkout.
I'm using WooCommerce MultiStep Checkout Wizard premium plugin and a specific hook:
add_action('woocommerce_multistep_checkout_before_order_info', 'destinationStep');
I'm a little lost and can't find much documentation for what I need to use to get it.
I'm trying to just get items to appear but I just get an empty array.
$order = new WC_Order( $order_id );
$items = $order->get_items();
var_dump($items);
You could try first with your approach "new WC_Order( $order_id );", this way:
function destinationStep( $order_id )
global $woocommerce;
$order = new WC_Order( $order_id );
$items = $order->get_items();
// echo var_dump($items);
//----
foreach ($items as $key => $item) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$terms = get_the_terms( $product_id, 'product_cat' );
// echo var_dump($terms);
foreach ( $terms as $term ) {
// Categories by slug
$product_cat_slug= $term->slug;
}
}
add_action('woocommerce_multistep_checkout_before_order_info', 'destinationStep', 10, 1);
If it still doesn't work try with "new WC_Order($post->ID)" approach:
function destinationStep()
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$items = $order->get_items();
// echo var_dump($items);
//----
foreach ($items as $key => $item) {
$product_name = $item['name'];
$product_id = $item['product_id'];
$terms = get_the_terms( $product_id, 'product_cat' );
// echo var_dump($terms);
foreach ( $terms as $term ) {
// Categories by slug
$product_cat_slug= $term->slug;
}
}
add_action('woocommerce_multistep_checkout_before_order_info', 'destinationStep');
Update - After some thought:
You can't get the order Id for `'post_type' => 'shop_order', because it doesn't exist yet. This order ID is generated when customer submit the order, but not before on checkout page.
So in this case, it's normal to get an empty array.

Woocommerce get order key

I have orders in the format [domain]/checkout/order-received/[order_number]/key=[wc-order-key] - how do I get [wc-order-key]?
So far I've done:
add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id)
{
$order = new WC_Order( $order_id );
$myuser_id = (int)$order->user_id;
$user_info = get_userdata($myuser_id);
$items = $order->get_items();
foreach ($items as $item)
{
$product_name = $item['name'];
$product_id = $item['product_id'];
$product_variation_id = $item['variation_id'];
$product_description = get_post_meta($item['product_id'])->post_content
}
return $order_id;
}
If i understand correctly, you need get order_key by order_id, is it correct?
If so, you can just use WC_Order property:
$test_order = new WC_Order($order_id);
$test_order_key = $test_order->order_key;
Edited
As mentioned indextwo, since Woo 3.0 there new syntax:
$test_order = wc_get_product($order_id);
$test_order_key = $test_order->get_order_key();
2018 updated answer
As WooCommerce 3 changed how property calls were made, the appropriate way to get the same information is:
$order = wc_get_order($order_id);
// Added a check to make sure it's a real order
if ($order && !is_wp_error($order)) {
$order_key = $order->get_order_key();
}
Note that you can easily do the same in reverse: get an order ID from an order key:
$order_id = wc_get_order_id_by_order_key($order_key);

Categories