I am using this to create a new function in my functions.php file
add_action( 'woocommerce_new_order', 'create_invoice_for_wc_order', 1, 1 );
function create_invoice_for_wc_order() {
}
it is to execute some custom code when a new order is placed, how can i get the order information (ordered products etc) inside my function
You can use $order_id as a parameter for your woocommerce_new_order callback and get the order details from the $order_id.
Example:
// define the woocommerce_new_order callback
function create_invoice_for_wc_order( $order_id ) {
// get order details data...
$order = new WC_Order( $order_id );
var_dump($order);
};
References:
http://hookr.io/actions/woocommerce_new_order/
https://docs.woothemes.com/wc-apidocs/class-WC_Order.html
All answers here are correct but if you need the line items the above wont work since line items can't be fetched with new WC_Order at that time.
The hook now has a second $order parameter that will work.
add_action( 'woocommerce_new_order', 'create_invoice_for_wc_order', 1, 2 );
function create_invoice_for_wc_order( $order_id, $order ) {
$items = $order->get_items();
// etc...
}
woocommerce_new_order includes an $order_id parameter. You can use it in your callback:
add_action( 'woocommerce_new_order', 'create_invoice_for_wc_order', 1, 1 );
function create_invoice_for_wc_order( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
// etc...
}
If you want to get only the order details, You can use 'woocommerce_new_order' hook and $order_id as parameter
add_action( 'woocommerce_new_order', 'create_invoice_for_wc_order', 1, 1 );
function create_invoice_for_wc_order($order_id) {
$order = new WC_Order( $order_id );
print_r($order);
//your code
}
But if You want to get order items You should use 'woocommerce_checkout_order_processed'
add_action( 'woocommerce_checkout_order_processed', 'create_invoice_for_wc_order', 1, 1 );
function create_invoice_for_wc_order($order_id) {
$order = new WC_Order( $order_id );
//order items
$items = $order->get_items();
print_r($items);
}
Related
I am using this to create a new function in my functions.php file
add_action( 'woocommerce_new_order', 'create_invoice_for_wc_order', 1, 1 );
function create_invoice_for_wc_order() {
}
it is to execute some custom code when a new order is placed, how can i get the order information (ordered products etc) inside my function
You can use $order_id as a parameter for your woocommerce_new_order callback and get the order details from the $order_id.
Example:
// define the woocommerce_new_order callback
function create_invoice_for_wc_order( $order_id ) {
// get order details data...
$order = new WC_Order( $order_id );
var_dump($order);
};
References:
http://hookr.io/actions/woocommerce_new_order/
https://docs.woothemes.com/wc-apidocs/class-WC_Order.html
All answers here are correct but if you need the line items the above wont work since line items can't be fetched with new WC_Order at that time.
The hook now has a second $order parameter that will work.
add_action( 'woocommerce_new_order', 'create_invoice_for_wc_order', 1, 2 );
function create_invoice_for_wc_order( $order_id, $order ) {
$items = $order->get_items();
// etc...
}
woocommerce_new_order includes an $order_id parameter. You can use it in your callback:
add_action( 'woocommerce_new_order', 'create_invoice_for_wc_order', 1, 1 );
function create_invoice_for_wc_order( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
// etc...
}
If you want to get only the order details, You can use 'woocommerce_new_order' hook and $order_id as parameter
add_action( 'woocommerce_new_order', 'create_invoice_for_wc_order', 1, 1 );
function create_invoice_for_wc_order($order_id) {
$order = new WC_Order( $order_id );
print_r($order);
//your code
}
But if You want to get order items You should use 'woocommerce_checkout_order_processed'
add_action( 'woocommerce_checkout_order_processed', 'create_invoice_for_wc_order', 1, 1 );
function create_invoice_for_wc_order($order_id) {
$order = new WC_Order( $order_id );
//order items
$items = $order->get_items();
print_r($items);
}
Im trying to trigger function with some information from product (like title, attributes ect.) on order status change, but Im getting this error:
FastCGI sent in stderr: "PHP message: PHP Fatal error: Uncaught Error: Call to a member function get_title() on null
Im using this code as my function:
function trigerinam_i_duombaze($order_id) {
global $product;
$pavadinimas = $product->get_title();
$sql = "INSERT INTO garantiniai (kompiuterio_bukle, preke, uzsakymo_bukle)
VALUES ('bukle', $pavadinimas, 'processing')";
}
add_action( 'woocommerce_order_status_processing', 'trigerinam_i_duombaze', 10, 1);
I have same global $product; and $pavadinimas = $product->get_title(); used in my other functions without any problems, any Ideas why this not working specifically with this one?
I also tried a bit different function with
add_action( 'woocommerce_order_status_changed', 'trigerinam_i_duombaze', 99, 3 );
But I'm stuck at exactly same place.. seems like global $product is not getting defined.
You can't access global $product; inside woocommerce_order_status_changed or woocommerce_order_status_processing.
using woocommerce_order_status_changed action hook parameter
$order_id, $status_from, $status_to, $order
function trigerinam_i_duombaze_changed( $order_id, $status_from, $status_to, $order) {
// get order from order id
$order = wc_get_order( $order_id );
foreach ($order->get_items() as $item_id => $item ) {
$pavadinimas = $item->get_name();
$sql = "INSERT INTO garantiniai (kompiuterio_bukle, preke, uzsakymo_bukle)
VALUES ('bukle', $pavadinimas, 'processing')";
}
}
add_action( 'woocommerce_order_status_changed', 'trigerinam_i_duombaze_changed', 99, 4 );
using woocommerce_order_status_changed action hook parameter
$order_id, $order
function trigerinam_i_duombaze_processing( $order_id, $order) {
// get order from order id
$order = wc_get_order( $order_id );
foreach ($order->get_items() as $item_id => $item ) {
$pavadinimas = $item->get_name();
$sql = "INSERT INTO garantiniai (kompiuterio_bukle, preke, uzsakymo_bukle)
VALUES ('bukle', $pavadinimas, 'processing')";
}
}
add_action( 'woocommerce_order_status_processing', 'trigerinam_i_duombaze_processing', 10, 2);
In WooCommerce, I am making a custom merchant plugin and I have added this hook:
add_action( 'woocommerce_order_details_after_order_table', 'track_me' );
Inside the track_me() function, I want to retrieve the order to do some things.
The function does what I want when I want, but I'm currently getting the order_id from the URL which seems janky.
I cannot figure out how to properly retrieve the $order object or the $order_id which would be enough.
It's almost certainly something obvious as hours of searching the Internet has been fruitless. I just don't know what the obvious something is...
UPDATE 1: Following the advice of #LoicTheAztec, I did the following:
do_action( 'woocommerce_order_details_after_order_table_items', $order );
class Order_MY extends WC_Order
{
function __construct()
{
add_action( 'woocommerce_order_details_after_order_table', array( $this, 'track_me' ) );
}
function track_me( $order )
{
// My code here
}
}
The first place the script fails is on the do_action line where PHP complains $order is an undefined variable.
The script also fails at extending WC_Order: Class 'WC_Order' not found
New question... is there something I need to do to make sure I have access to the woocommerce classes in my plugin?
The WC_Order Object instance $order variable is included in the hook. You see that on order/order-details.php template file (line 76):
do_action( 'woocommerce_order_details_after_order_table_items', $order );
So you can use it in your hooked function this way:
add_action( 'woocommerce_order_details_after_order_table', 'track_me' );
function track_me( $order ) {
// The Order ID
$order_id = $order->get_id();
// your code goes below
}
Now in a plugin, inside a class, you will replace:
add_action( 'woocommerce_order_details_after_order_table', 'track_me' );
by:
add_action( 'woocommerce_order_details_after_order_table', array( $this, 'track_me' ) );
Addition:
As you can't get the $order object from your plugin you should also try:
add_action( 'woocommerce_order_details_after_order_table', 'track_me' );
function track_me( $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) {
global $order;
}
// The Order ID
$order_id = $order->get_id();
// your code goes below
}
I cant tell you anything more
I am not an expert but maybe sth. like this could work:
global $order_id;
$order = wc_get_order($order_id);
In WooCommerce, I am using a custom function hooked in woocommerce_thankyou action hook, to execute some code after a payment has been made. The hook works but it seems that I can't get the order.
This is the simplified code. By the looks of it $order isn't found:
add_action( 'woocommerce_thankyou', 'afterorder', 10, 1 );
function afterorder($order_id) {
//$order = new WC_Order($order_id);
$order = wc_get_order($order_id);
$order_items = $order->get_items();
$order_comment_list = explode('\n', $order->customer_message);
$payment_method = $order->payment_method_title;
foreach( $order_items as $product ) {
$order->add_order_note('order for '.$product['name'].' received', false);
}
}
What am I missing here?
Your code is a partially outdated and with some errors since WooCommerce 3+. Order line items are now WC_Order_Item_Product class objects.
For Order "line items" you need to use WC_Order_Item_Product available methods to get the related data, like the corresponding product title:
add_action( 'woocommerce_thankyou', 'afterorder', 10, 1 );
function afterorder( $order_id ) {
// The WC_Order object
$order = wc_get_order($order_id);
$order_comment_list = explode( '\n', $order->get_customer_note() ); // Changed
$payment_method = $order->get_payment_method_title(); // Changed
foreach( $order->get_items() as $line_item ) {
// The WC_Product object
$product = $line_item->get_product(); // Added
$note = 'order for '.$product->get_title().' received';// Changed
$order->add_order_note( $note, false );
}
}
Code goes in function.php file of your active child theme (or active theme) or in any plugin file.
You should check may be the WC_Order method add_order_note() to see if you have correctly set it as you want.
I need to add custom args to PayPal paying link.
The PHP function looks like this:
$paypal_args = apply_filters( 'woocommerce_paypal_args', $paypal_args );
add_filter( 'woocommerce_paypal_args' , 'change_paypal_args' );
function change_paypal_args( $paypal_args ) {
global $wp;
global $woocommerce;
$order = wc_get_order( $order_id );
$paypal_args['invoice'] = 'spi432';
$paypal_args['txn_type'] = 'cart';
$paypal_args['payment_date'] = $order->order_date;
return $paypal_args;
}
I added txn_type and invoice as arguments to the link. But payment_date is not shown.
What may be the problem? Also, how can I display email of the customer?
If you enabled WP_DEBUG you would probably see that $order_id is undefined, and therefore $order is not an order object, so $order->order_date is likely a fatal error. Try passing the order as the second parameter instead.
add_filter( 'woocommerce_paypal_args' , 'so_42424283_change_paypal_args', 10, 2 );
function so_42424283_change_paypal_args( $paypal_args, $order ) {
$paypal_args['invoice'] = 'spi432';
$paypal_args['txn_type'] = 'cart';
// WC 2.6+
$paypal_args['payment_date'] = $order->order_date;
// WC 2.7
//$paypal_args['payment_date'] = $order->get_date_created();
return $paypal_args;
}