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();
}
Related
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();
}
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.
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);
I am trying to build an application which texts me my woo commerce order, order items and Quantity,
I am 90% there,
function custom_woocommerce_complete_order_sms( $order_id ) {
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$product_list = '';
$order_item = $order->get_items();
foreach( $order_item as $product ) {
$prodct_name[] = $product['name'];
}
$product_list = implode( ',\n', $prodct_name );
require "twilio-php-master/Services/Twilio.php";
$AccountSid = "xxxxxxxxx";
$AuthToken = "xxxxxxxxx";
$client = new Services_Twilio($AccountSid, $AuthToken);
$people = array(
"xxxxxxxxxx" => "Me",
);
foreach ($people as $number => $name) {
$sms = $client->account->messages->sendMessage(
"+44xxxxxxxxxx",
// the number we are sending to - Any phone number
$number,
// the sms body
"Hey $name, there is a new Order, the order is, $product_list"
);
}
}
My problem is I do not know how to get the item Quantity , for example my text looks like list, item 1, item 2, item 3, I want it to say item 1 x1, item 2 x2, item3 x3
I did try and dig into the email php file in abstract woo commerce folder to see how they do as they send Quantities in emails but got a little lost
also in the class WC_Abstract_Order the only other thing I could find is get_item_total which returns to the total of all items
From research you can also grab the qty from the order item
$product['qty'];
Therefore , it was simple to loop over and add the quantity to the item name (below)
$product_details = array();
$order_items = $order->get_items();
foreach( $order_items as $product ) {
$product_details[] = $product['name']."x".$product['qty'];
}
$product_list = implode( ',', $product_details );
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!