I have created a function that is supposed to update certain meta fields once an order has been placed with Woocommerce.
Although I can confirm that the function is fired, I cannot seem to be able to load the order by the ID. Here is my code so far:
add_action('woocommerce_new_order', 'wc_update_bib_options');
function wc_update_bib_options($order_id) {
$order = wc_get_order( $order_id );
$items = $order->get_items();
foreach ($items as $item) {
// Some stuff...
}
}
I can confirm that $order_id is passed and is an integer. Also, if I enter an ID of another (previous) order, the code works just fine...
I also tried using
$order = new WC_Order( $order_id );
But I get the same results...
Can anyone help me out?
Related
I need to add a custom text that is displayed on the thank you page, if the order has been processed and has a completed status.
I am using the following code in the "woocommerce_order_details_before_order_table" hook and it works perfectly, but when I use it in the "woocommerce_thankyou" hook it generates errors.
add_action( 'woocommerce_thankyou', 'complete_custom_text' );
function complete_custom_text( $order ) {
$status = $order->get_status();
if(($status === 'completed')){
echo '<p><b>Text (Custom) 5:</b> Perfect.</p>';
}
}
If someone could help me I would appreciate it.
You are receiving order_id in that hook, use $order = wc_get_order( $order_id ); to get the order before reading the status.
Using get_post_meta works elsewhere in my plugin, but when I want to use it with woocommerce_thankyou nothing is returned.
function my_function($order_id)
{
$order = wc_get_order( $order_id );
foreach ($order->get_items() as $item_id => $item) {
$customer_meta = get_post_meta($item->get_id(), '_my_custom_meta', true);
error_log("Meta value - " . $customer_meta);
}
}
add_action('woocommerce_thankyou', 'my_function');
Checking $item->get_id does confirm the product ID is being read, but get_post_meta isn't working here.
This seems so simple, but it was late and I might have been over-complicating things!
I'm currently using the woocommerce_thankyou hook in the WP functions file to compile some data and send it to a third party API. So far, so easy, using standard $order and $order_meta values. But I need to get the total number of items in an order, and I can't see where to get it.
So if someone orders 2 green widgets and 3 blue widgets, I need to get 5 from somewhere.
Am I missing something obvious? :-)
Counting order items can be 2 different things:
Total items count:
// Get an instance of the WC_Order Object
$order = wc_get_order( $order_id );
$items_count = count( $order->get_items() );
// Testing output
echo $items_count;
The total items quantity count:
// Get an instance of the WC_Order Object
$order = wc_get_order( $order_id );
$total_quantity = 0; // Initializing
// Loop through order items
foreach ( $order->get_items() as $item ) {
$total_quantity += $item->get_quantity();
}
// Testing output
echo $total_quantity;
Or you can use the WC_Order get_item_count() method that do the same (see its source code):
// Get an instance of the WC_Order Object
$order = wc_get_order( $order_id );
$total_quantity = $order->get_item_count();
Use this to get total items in a order -
$order = wc_get_order( $order_id );
echo $order->get_item_count(); // Will display the total numbers
I'm trying to get Item or Product Attribute in WooCoomerce Order.
How can I get it?
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ( $items as $item ) {
$pid = $item['product_id'];
$patt = $pid->get_attribute( 'pa_myattrname' );
echo $patt;
}
Later, I want to insert autoresponder link on attribute, so that after user complete payment, they will automatically subscribed into my autoresponder.
Thank you
I know that it is old question, but that answer may help someone who is looking for nicer option.
There is much simpler way to get product attributes from order. You just need to go into products (items) and then load meta data
// at first get order object
$order = wc_get_order($orderId);
// iterate through order items/products
foreach ($order->get_items() as $item) {
// load meta data - product attributes
foreach ($item->get_meta_data() as $metaData) {
$attribute = $metaData->get_data();
// attribute value
$value = $attribute['value'];
// attribute slug
$slug = $attribute['key'];
}
}
$item['product_id']; will return the integer product_id, you cannot call get_attribute method on it. Using the integer product_id you need to create a Product object and then call the method
$pid = $item['product_id']; // returns the product id
$p = new WC_Product( $pid ); // create an object of WC_Product class
$patt = $p->get_attribute( 'pa_myattrname' ); // call get_attribute method
echo $patt;
For a specific woocommerce project, I need to set every new order on hold.
It needs to go through that before processing payment.
Do you guys know a hook to do that? I tried many different things, that didn't work.
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order' );
function custom_woocommerce_auto_complete_order( $order_id ) {
global $woocommerce;
if ( !$order_id )
return;
$order = new WC_Order( $order_id );
$order->update_status( 'on-hold' );
}
This is the standard way of doing it. Not sure if it still works with 2.2, but you didn't specify your WooCommerce version.