Adding custom args to Paypal Woocommerce - php

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;
}

Related

Woocommerce get_items() doesn't return anything [duplicate]

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);
}

Access the order object from woocommerce_order_details_after_order_table hook

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);

Hook woocommerce_thankyou doesn't receive order

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.

WooCommerce new order action - get order information

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);
}

update woocommerce order status after payment process complete and redirect to store

I am using woo-commerce for my shopping site. I want to update the order status to complete after payment was made and then return to a success page.
I used the following code:
add_filter( 'woocommerce_payment_complete_order_status', 'my_change_status_function', 10, 2 );
function my_change_status_function ($order_status, $order_id) {
$order = new WC_Order($order_id);
return 'completed';
}
But this function is called before the payment was made and redirects to the payment page.
I want to change the status after the payment was completed and then return to redirect URL.
Here is my redirect link:
http://example.com/checkout/order-received/82/?key=wc_order_5614e28c9d183&state=return
But the status is not changing when I use the woocommerce_payment_complete_order_status hook.
The hook should be called after the payment is completed.
Try using the following code in your plugin
add_action( 'woocommerce_payment_complete', 'my_change_status_function' );
function my_change_status_function( $order_id ) {
$order = wc_get_order( $order_id );
$order->update_status( 'completed' );
}
Check out this piece of code
add_action('woocommerce_checkout_order_processed', 'do_something');
function do_something($order_id) {
$order = new WC_Order( $order_id );
// Do something
}
For Cash On Delivery method, this worked for me:
add_filter( 'woocommerce_cod_process_payment_order_status', 'prefix_filter_wc_complete_order_status', 10, 3 );
function prefix_filter_wc_complete_order_status( $status, $order ) {
return 'on-hold';
}
For most other methods, this worked:
add_filter( 'woocommerce_payment_complete_order_status', 'prefix_filter_wc_complete_order_status', 10, 3 );
function prefix_filter_wc_complete_order_status( $status, $order_id, $order ) {
return 'on-hold';
}
Working with WooCommerce v4.4, other answers were not working for me. I had to do it this way: https://stackoverflow.com/a/64285242/7198947

Categories