Reputation: 21
What are some ways to get the current order ID from within a callback function hooked to a WooCommerce filter where the callback is not passed the order ID by default?
Example hook to a WooCommerce filter that meets this criteria:
add_filter('woocommerce_get_downloadable_file_urls', array('fooClass', 'fooFighter'), 10, 4);
This hook passess the following variables to the callback:
I have looked into accessing the order ID through $woocommerce by defining it as a global. In this instance (when WooCommerce sends the customer the completed order email), $woocommerce does not contain a WC_Order object.
For additional information, I am attempting to use the aforementioned hook to create custom download links for specific WooCommerce products. This filter is called each time a downloadable product is listed on the completed order email and again on the order complete page). To create the custom link, I need the order_id.
Upvotes: 2
Views: 797
Reputation: 26319
I also know this is pretty old, but just wanted to note that for WooCommerce 2.2 the get_downloadable_file_urls()
method is deprecated in favor of get_item_downloads()
. That means the that appropriate filter would now be woocommerce_get_item_downloads
.
return apply_filters( 'woocommerce_get_item_downloads', $files, $item, $this );
The third parameter is $this
which since get_item_downloads()
is a method in the WC_Abstract_Order
abstract (responsible for WC_Order) would ultimately mean it is the $order
object and $order->id
would be the ID.
Upvotes: 1
Reputation: 177
I know it's been a while since this was asked, but I stumbled upon the same problem today and fixed it by putting the following code inside my filter function.
global $woocommerce;
$orderId = $woocommerce->woocommerce_email->emails['WC_Email_New_Order']->object->id;
The $woocommerce-woocommerce_email->emails variable is only set when sending out e-mails, so adding some checks might also be necessary.
Hope this helps someone someday. :)
Upvotes: 1