I am making an ecommerce website using woocommerce, now I want to call a function after use fill the checkout form and submit it. Is there any filter or hooks that allow me to do that?
Or you can:
add_action( 'woocommerce_thankyou', array('Wc_class', 'my_uber_function'));
class Wc_class{
public static function my_uber_function($order_id)
{
stuff to do here
}
}
If you want to do something after WooCommerce order completed you can hook into woocommerce_order_status_completed.
add_action( 'woocommerce_order_status_completed', 'my_function' );
/*
* Do something after WooCommerce sets an order on completed
*/
function my_function($order_id) {
// order object (optional but handy)
$order = new WC_Order( $order_id );
// do some stuff here
}
Related
I want to use woocommerce hook woocommerce_order_status_changed.
I want to fire something when new status of order is trash.
I used this function, which is working good for rest order statuses, except trash.
This is my code:
function custom_order_actions ( $order_id, $old_status, $new_status ){
$order = new WC_Order($order_id);
if ($new_status == 'trash') {
// Do something
}
}
add_action( 'woocommerce_order_status_changed', 'custom_order_actions', 99, 3 );
woocommerce_order_status_changed can not pick up the trash status since it's not one of the registered statuses on woocommerce according to their github page.
However, you could use wp_trash_post action hook instead!
add_action('wp_trash_post', 'custom_order_actions');
function custom_order_actions($order_id)
{
if ('shop_order' == get_post_type($order_id)) {
$order = new WC_Order($order_id);
// Do something
}
}
wp_trash_postDocs
Let me know if it works for you!
I'm looking to loop over all the products added to an order in the admin before the order is actually submitted. So far the only WooCommerce hooks I have found only allow you access the product items individually.
I was looking for a hook that would fire when a user clicks the recalculate button but actually it could trigger when a user adds a product, tax, shipping method, etc.. I just need to loop over all items added to the order so far.
At the moment I'm using woocommerce_admin_order_item_values hook but it's a self-contained loop so doesn't allow me to add all my '$item['product_id']' together.
function action_woocommerce_admin_order_item_values( $null, $item, $absint ) {
$item_ids = array($item['product_id']);
}
add_action( 'woocommerce_admin_order_item_values', 'action_woocommerce_admin_order_item_values', 10, 3 );
You can also use -
woocommerce_before_order_itemmeta Hook but this only accesses each item individually whereas I need to loop over each item in the summary.
There are number of hooks provided by WooCommerce, when you are clicking the Recalculate button. I'm listing here those hooks and it depends on you to choose among them according to your requirement.
$order = WC_Order object
add_action("woocommerce_order_before_calculate_taxes", "custom_order_before_calculate_taxes", 10, 2);
function custom_order_before_calculate_taxes($args, $order) {
// Do something
}
add_action("woocommerce_order_item_after_calculate_taxes", "custom_order_item_after_calculate_taxes", 10, 2);
function custom_order_item_after_calculate_taxes($order, $calculate_tax_for) {
// Do something
}
add_action("woocommerce_before_order_object_save", "custom_before_order_object_save", 10, 2);
function custom_before_order_object_save($order, $data_store) {
// Do something
}
add_action( 'woocommerce_order_before_calculate_totals', "custom_order_before_calculate_totals", 10, 2);
function custom_order_before_calculate_totals($and_taxes, $order ) {
// Do something
}
add_action( 'woocommerce_order_after_calculate_totals', "custom_order_after_calculate_totals", 10, 2);
function custom_order_after_calculate_totals($and_taxes, $order) {
//Do something
}
add_filter("woocommerce_order_is_vat_exempt", function(){
return $boolean;
});
add_filter("woocommerce_order_get_total", "custom_order_get_total", 10, 2);
function custom_order_get_total($value, $order) {
//do somethig
return $value;
}
I'm using this little function here to detect if an order is set into pending. This happens between the payment page and the payment provider notification:
add_action( 'woocommerce_order_status_pending', 'status_pending' );
function status_pending( $related_job ) {
error_log('Triggered');
}
The problem is that I don't get any error log which shows me that the function work. But it becomes crazier. When I update the status via the dashboard from completed to pending, the log appears. So I've absolutely no idea why it's not working during the checkout process. Any recommendations or ideas what could be the problem?
The "pending" order status is the default status for orders before customer get on a payment gateway, just after order creation.
So the best way is to use a hook once the order is created, before payment method process:
1) try first the woocommerce_checkout_order_processed action hook (3 args):
add_action( 'woocommerce_checkout_order_processed', 'order_processed_with_pending_status', 10, 3 );
function order_processed_with_pending_status( $order_id, $posted_data, $order ) {
error_log('Triggered');
}
2) Alternatively try the woocommerce_checkout_update_order_meta action hook (2 args):
add_action( 'woocommerce_checkout_update_order_meta', 'order_processed_with_pending_status', 10, 2 );
function order_processed_with_pending_status( $order_id, $data ) {
error_log('Triggered');
}
Both should work…
That's because the hook is only triggering on order status change not on order creation, there is another hook that you can use to detect new orders, you can use the order ID to get order object which you can use to find out the order status:
add_action( 'woocommerce_new_order', 'prefix_new_wc_order', 1, 1 );
function prefix_new_wc_order( $order_id ) {
$order = new WC_Order( $order_id );
}
The hook above is only triggered in checkout process, so creating orders on backend won't trigger it.
Right now I am trying to get it so that whenever someone gets to my site's product page their cart is automatically emptied.
I am using a woocommerce product addon called "Product & Checkout Options for WooCommerce" that allows me to use radiobuttons/checkboxes for my products, I don't know if that will alter any of the code.
I've tried php code such as this but it hasn't worked:
add_filter( 'woocommerce_add_to_cart_validation', 'only_one_in_cart' , 10, 1);
function only_one_in_cart( $cart_item_data ) {
global $woocommerce;
$woocommerce->cart->empty_cart();
unset($cart_item_data['product_meta']);
return true;
}
It's better to add the hook on your single product page, do it with the action woocommerce_before_single_product:
add_action( 'woocommerce_before_single_product', `only_one_in );
function only_one_in_cart() {
global $woocommerce;
$woocommerce->cart->empty_cart();
}
This will empty your cart each time you visit the page, if it's late, then you can add the function into the wp_head hook and validate if you are in the product page by is_product():
add_action( 'wp_head', `only_one_in );
function only_one_in_cart() {
if ( is_product() ){
global $woocommerce;
$woocommerce->cart->empty_cart();
}
}
I am using Woocommerce for some project and i need to send the order id to some remote site when the payment is made. I am not finding the accurate hook to do this. Can anyone help me to find what's the correct hook to perform certain action after order is completed.
Here is what i have tried
add_action( 'woocommerce_thankyou', 'woo_remote_order' );
function woo_remote_order( $order_id ) {
// Lets grab the order
$order = new WC_Order( $order_id );
//Some action to make sure its working.
wp_mail( 'sagarseth9#example.com',' Woocommmerce Order ID is '.$order_id , 'Woocommerce order' );
}
Not sure which is the proper hook to perform this action. I am using paypal payment gateway for payment and orders successfully passes through.
looks like you need add accepted_args on last parameters,
Try this :
add_action( 'woocommerce_thankyou', 'your_func', 10, 1 );
function your_func($order_id) {
$order = new WC_Order( $order_id );
/* Do Something with order ID */
}
Maybe try one of the following.
woocommerce_checkout_order_processed
woocommerce_new_order
add_action( 'woocommerce_subscription_payment_complete', 'YourFunction', 1, 2);
function YourFunction ($order_id)
{
$order = new WC_Order( $order_id );
wp_mail( 'sagarseth9#example.com',' Woocommmerce Order ID is '.$order_id , 'Woocommerce order' );
}
The add_action call must be placed at the very beginning of your plugin, if using wordpress, or if a theme, in functions.php.