Calling 3rd party api on creating new order in woocommerce - wordpress

I have an e-commerce site running on WordPress, I have added functionality in which whenever a user checkout, the API of my courier is hit and a tracking code is saved in the database.
Now I want to create a custom order from the backend (Add new order in Woocommerce) and do the same thing, like calling the API and saving the tracking returned from the API call to the database, but don't know which hook to use.

you should use 'woocommerce_new_order' hook:
add_action( 'woocommerce_new_order', 'api_call', 1, 1 );
function api_call( $order_id ) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
// etc...
}
fires after new order created

Related

Run a custom code after a product is sold in woocommerce

I need to know how to assign custom actions (or run custom code) when someone buys something in the website and the payment has been completed. I need this so our system can send order's data to an API.
i am using wordpress and woocommerce for the website.
how can i find the order dynamics variable to put it in my API's code ?
if there,s anyone who use wordpress and woocommerce, please tell me where can i find order's dynamic variables such as:
product id which the customer has been ordered
product count and etc.
You could write a function that connects to the woocommerce_order_status_processing hook. At this point, the payment has been accepted and WooCommerce is waiting for the store to fulfill the order.
add_action( 'woocommerce_order_status_processing', 'my_order_complete_function', 10, 1 );
function my_order_complete_function( $order_id ) {
$order = wc_get_order( $order_id );
foreach($order->get_items() as $item) {
$product_id = $item->get_product_id();
$product = wc_get_product( $product_id );
// Add your API call here.
}
}

WordPress payment hooks not working for WP Crowdfunding products

Let me put the code that I have tried so far.
Basically, I am trying to capture payments when they are successful by creating a function in the function.php file, the code is provided below.
add_action('woocommerce_payment_complete', 'capture_donation_for_custom_donation_view', 10, 1);
function capture_donation_for_custom_donation_view($order_id){
if ( ! $order_id ) {
return;
}
global $product;
$order = wc_get_order( $order_id );
This function captures the data from the order object and works fine for normal orders. But I am using a plugin WP Crowdfunding to create some crowdfunding projects and data is not being captured through payment hooks.

Display woocommerce 'payment method' in Order Notes section in the Admin Dashboard

I'd like to pass the payment method to the order notes section of a Woocommerce Order (when viewing the order in the backend)
This field gets imported into our ShipStation software. Right now, I have no way of knowing what payment method a customer used when viewing an order from ShipStation. (we give a free gift when a certain payment method is used.)
At first, viewing the payment method in Wordpress Admin is available. You just have to enable it in Screen Options.
If you want to add the Shipment method into the order note, you need to just add this to your functions.php:
add_action( ‘woocommerce_new_order’, ‘add_engraving_notes’, 1, 1 );
function add_engraving_notes( $order_id ) {
$order = new WC_Order( $order_id );
$note = $order->getShippingMethod();;
$order->add_order_note( $note );
$order->save();
}

woocommerce_email_actions not working in custom plugin

I am trying to send a email when a order is moved to a custom status in woocommerce. I have successfully created a plugin that creates a custom status and order is assigned to the custom status.
I have also successfully added a custom email template under Settings -> Emails in WooCommerce and written code which should send a email when the order is moved to that custom status.
However that code is not getting triggered and email is not getting sent.
Below is my code.
apply_filters( 'woocommerce_email_actions', array('woocommerce_order_status_processing_to_partial-shipment'));
// Trigger on new paid orders
add_action( 'woocommerce_order_status_processing_to_partial-shipment_notification', array( $this, 'trigger' ) );
the action never comes in the trigger function i have written. I am using wordpress 4.5.3 and woocommerce version 2.6.4.
Can anyone suggest as to what could be going wrong. Thanks.
Since WooCommerce 2.3, you can use 'woocommerce_email_actions' filter, so you can try this:
function new_woocommerce_email_actions( $actions ){
$actions[] = 'woocommerce_order_status_processing_to_partial-shipment_notification';
return $actions;
}
add_filter( 'woocommerce_email_actions', 'new_woocommerce_email_actions' );
Luck

Woocommerce: How do I generate unique IDs for each purchased item?

I'm working in WordPress 4.3.1., with WooCommerce 2.4.7. For my employer's current store project, we need to have unique IDs generated for each item purchased, which can never be the same as any other purchase.
For example, if someone buys 3 shirts of the same design and size, each of these shirts, once in the cart, should each have a unique item ID displayed. Regardless of the number of orders we get, the unique item IDs cannot be duplicated.
As for the ID, I was considering mixing the product SKU with the date, and possibly the order ID to generate the date. How exactly would I go about this? Is there a plugin that can handle this, or should I deal in straight PHP?
I assume you want to use the unique id for work order tracking internally
use this hook (I am using processing rather then completed)
add_action( 'woocommerce_order_status_processing', 'add_unique_id' );
Then do this
function add_unique_id($order_id) {
$order = new WC_Order( $order_id );
$items = $order->get_items();
foreach ($items as $item_id => $product ) {
$gen_id = "generate id goes here";
wc_add_order_item_meta($item_id, 'unique_id', $gen_id);
}
You could use a WooCommerce hook like woocommerce_order_status_completed in order to run something after every purchase.
add_action( 'woocommerce_order_status_completed', 'custom_function' );
function custom_function($order_id) {
// completed order object (can be useful)
$order = new WC_Order( $order_id );
// run what you want here
}
I can suggest you to use a post meta or an user meta (based on your needs).

Resources