I'm trying to create a function that will retrieve an order by its ID. For some reason I can't get the WooCommerce global function get_order to work. I'm passing a valid order id to the function and trying to print it out to verify that it's working. The function has been placed in my functions.php file.
function getWC_order_details($id){
global $woocommerce;
$order = get_order( $id );
print "<pre>";
print_r($order);
print "</pre>";
}
I have tested echoing other data out of the function without a problem.
First of all make function like this :
function getWC_order_details($order_id) {
$order = new WC_Order( $order_id );
var_dump($order);
}
After that, use it with some woo_commerce action or filter.
function use_after_cart_table(){
getWC_order_details(40);
}
add_action( 'woocommerce_after_cart_table', 'use_after_cart_table' );
So after adding any product to the cart, you will see after cart table that there is one array containing all the details.
NOTE : You can use any other action or filter and you can find them here.
EDITED:
function getWC_order_details($order_id) {
$order = new WC_Order( $order_id );
//var_dump($order);
$order_shipping_total = $order->get_shipping();
$order_shipping_method = $order->get_shipping_methods();
var_dump($order_shipping_total);//Use it for debugging purpose or to see details in that array
var_dump($order_shipping_method);//Use it for debugging purpose or to see details in that array
$_order = $order->get_items(); //to get info about product
foreach($_order as $order_product_detail){
//var_dump($order_product_detail);
echo "<b>Product ID:</b> ".$order_product_detail['product_id']."<br>";
echo "<b>Product Name:</b> ".$order_product_detail['name']."<br><br>";
}
//var_dump($_order);
}
Try this. It might be useful to you.
function getWC_order_details($id)
{
$array = WC_API_Orders::get_order( $id, $fields );
print "<pre>";
print_r($order);
print "</pre>";
}
Source:
File name: woocommerce/includes/api/class-wc-api-orders.php
Related
I tried about 5 hooks to get the order hook for completed and the function doesn't run at all but woocommerce_add_to_cart for example is working!
1. woocommerce_order_status_changed
2. woocommerce_new_order
I just make the alert to know if the function runs but the fucntion is quite large and I make order manullay as a test before deploying the plugin in this function in the plugin in index.php
function SP_order_token($order_id)
{
?>
<script>
alert("hello");
alert('<?php echo $order_id; ?>');
</script>
<?php
echo "hello";
global $woocommerce, $post;
echo $order_id;
$order = wc_get_order($order_id);
$order_data = $order->get_data(); // The Order data
var_dump($order_data);
}
// the final hook is when an order successfully paid
add_action('woocommerce_order_status_changed', 'SP_order_token',10,1);
Your approach for debugging php is wrong. You can't alert or do JS things on the server side. Also, var_dump and echo will work but you don't know where they gonna echo or dump the output.
The correct way for php debugging will be to write your output in external files or in error logs. but I prefer writing in files on the root of wordpress.
Here is the code snippet that you can use:
function sp_order_token( $order_id ) {
$order = wc_get_order( $order_id );
$order_data = $order->get_data();
/**
* We're using file_put_contents to create a debug.txt file in the root of wordpress where your wp-config.php exists.
*
* we're also passing true in print_r so that print_r returns the output instead of printing it during code execution.
*
* ABSPATH is a constant of wordpress root defined by core WordPress.
*/
file_put_contents( ABSPATH . 'debug.txt', print_r($order_data, true ) );
}
add_action( 'woocommerce_order_status_changed', 'sp_order_token', 10, 1 );
Once your action will run in wordpress root you'll find debug.txt with your expected output.
Please replace with this and check again.
function woo_order_status_change_custom_check($order_id) {
?>
<script>
alert("hello");
alert('<?php echo $order_id; ?>');
</script>
<?php
// echo "hello";
$order = new WC_Order( $order_id );
$orderstatus = $order->status;
var_dump($orderstatus);
}
add_action('woocommerce_order_status_changed', 'woo_order_status_change_custom_check', 10, 1);
I'm trying to introduce a shortcode on the thankyou.php page to show the details of the order just placed by a customer.
If I write the code in php like this it works and shows the total:
<?php echo $order->get_total(); ?>
Now I'm trying to get the same result through a shortcode, but I don't know some parameters and therefore can't get it to work.
<?php
add_shortcode( 'custom-woocommerce-total' , 'custom_total' );
function custom_total(){
$customer_id = get_current_user_id();
$order = new WC_Order($order_id); // I suppose that's not right.
return $order->get_total();
} ?>
can anyone help me understand what I'm doing wrong?
You would need to get the order id first by using global $wp variable. Try this:
add_shortcode('custom-woocommerce-total', 'custom_total');
function custom_total($attr)
{
if (is_wc_endpoint_url('order-received'))
{
global $wp;
$order_id = absint($wp->query_vars['order-received']);
if ($order_id)
{
$order = new WC_Order($order_id);
if($order)
{
return $order->get_total();
}
}
}
}
And in the thankyou page template use it like this:
echo do_shortcode('[custom-woocommerce-total]');
Don't forget to override the template.
I have attached a function to the woocommerce_checkout_order_processed hook:
//check if woocommerce is acive
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
add_action('woocommerce_checkout_order_processed', 'wc_on_place_order');
}
The wc_on_place_order function is to be executed after the user clicks on the PLACE ORDER button. However, it's so odd that the function is executed twice.
My wc_on_place_order function calls an external api written in C#:
function wc_on_place_order( $order_id ) {
global $wpdb;
// get order object and order details
$order = new WC_Order( $order_id );
// get product details
$items = $order->get_items();
//return $items;
$products = array();
foreach ($items as $item) {
array_push($products,
array('userid' => $order->user_id, 'descr' => $item['name'], 'amt' => (float)$item['line_total'])
);
}
//passing $products to external api using `curl_exec`
. . . .
//on successful call, the page should be showing an `alert`, however, it does not
// the handle response
if (strpos($response,'ERROR') !== false) {
print_r($response);
} else {
echo "<script type='text/javascript'>alert($response)</script>";
}
}
After debugging on C# API, I noticed that it the service is being called twice, thus, the checkout is being saved twice to the API database.
Is there something wrong with the wc_on_place_order function or is woocommerce_checkout_order_processed called twice when clicking the PLACE ORDER?
Interestingly, adding return $items after $items = $order->get_items() somehow, the C# api was only called once:
// get product details
$items = $order->get_items();
return $items; //this line
Why is that so?
One more question I would like to ask, is woocommerce_checkout_order_processed the right hook I should use? I have been searching the web for the correct hook to use and it seems that woocommerce_checkout_order_processed is used in the most post. I can't use the woocommerce_thankyou hook as it is also calling the API if I refresh the page.
Any idea will be really appreciated.
EDIT:
I used woocommerce_after_checkout_validation hook which fires after pre-validations on checkout. I can't remember though why woocommerce_checkout_order_processed is being fired twice but I just changed some kind of settings in WooCommerce options page. I can't remember which.
Useful Links from the Comments:
Visual Representation of the WooCommerce hooks
WordPress Action References
I always use the hook woocommerce_payment_complete This will fire as the name suggests after the order has been paid.
function order_payment_complete( $order_id ){
$order = wc_get_order( $order_id );
/* Insert your code */
}
add_action( 'woocommerce_payment_complete', 'order_payment_complete' );
I want to get the "mycred" balance of a customer through the order while using WP ALL Export to export the customer balance based on orders to a spreadsheet. It's actually probably quite simple. I'm able to get the Order ID, but not the Customer ID
Here is what I'm doing to test if I can get the customer ID:
function get_customeruserid($value)
{
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$order_id = $order->get_order_number();
$customer = new WC_Customer($post->ID);
$user_id = $customer->get_ID();
$value = $user_id;
return $value;
}
This returns a 0.
However, I can get the order number easily enough by doing this:
function get_customerorderid($value)
{
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$order_id = $order->get_order_number();
$value = $order_id;
return $value;
}
This returns the customer's order number which is great, but only half the battle. I now want the Customer ID so I call call the mycred balance function to show their balance.
Any ideas? I'm a newbie at php and probably very bad.
To get the User ID from the Order ID, you can use many ways, Here are 2 of them:
In WooCommerce 3.0+ you can use WC_Order Class methods this way:
function get_customerorderid(){
global $order, $post;
if( ! is_a($order, 'WC_Order') ) {
$order_id = $post->ID;
// Get an instance of the WC_Order object
$order = wc_get_order($order_id);
} else {
$order_id = $order->id;
}
// Get the user ID from WC_Order methods
$user_id = $order->get_user_id(); // or $order->get_customer_id();
return $user_id;
}
Before WooCommerce 3.0 version, you can use get_post_meta() function this way:
function get_customerorderid(){
global $order, $post;
if( ! is_a($order, 'WC_Order') ) {
$order_id = $post->ID;
} else {
$order_id = $order->id;
}
// Get the user ID
$user_id = get_post_meta($order_id, '_customer_user', true);
return $user_id;
}
For those who want to specifically add the customer mycred balance from an ORDER into the CSV sheet within WP All Export here is the bit of code I used.
Thank you for your help getting it solved.
While editing an ORDER export in WP ALL EXPORT, add a new data object and click on it and "Export the value returned by a PHP function" then add the following function in the code editor:
function all_export_mycred($balance)
{
global $woocommerce, $post;
$order = new WC_Order($post->ID);
$user_id = $order->get_user_id( );
$balance = mycred_get_users_balance( $user_id );
return $balance;
}
Then make sure to add the "all_export_mycred" to the php return field.
I'm trying to get price without currency in a function I made.
function add_price_widget()
{
global $woocommerce;
$product = new WC_Product(get_the_ID());
$thePrice = $product->get_price_html();
echo thePrice;
}
Displays: 100kr
How do I get it to just give me the price 100
What #Syntax_Error had said is correct you have to use
get_price(), WooCOmmerce also provide a wrapper function
wc_get_product() for WC_Product class.
So your function would look something like this:
function add_price_widget()
{
$product = wc_get_product(get_the_ID());
$thePrice = $product->get_price(); //will give raw price
echo $thePrice;
}
Hope this helps!
You can just use the function get_price that returns only the number (without dots or symbol)
function add_price_widget() {
global $woocommerce;
$product = new WC_Product(get_the_ID());
$thePrice = $product->get_price();
echo thePrice;
}
I just tested it in my site and it work. So it should work for you too.