How can I place an order using AJAX in Woocommerce? - php

I am using Woocommerce on a locally hosted WordPress.
I would like the 'Place order' button in the order review section/checkout section of Woocommerce to process the order using AJAX (so that the order details are added to the database without refreshing the page).
I can't seem to find any information on this anywhere. Thanks

You'll have to add an eventlistener to the "Place order" button's click event, and prevent the default behaviour. Then use Ajax to make a POST request to the WooCommemerce REST API's order endpoint. The documentation contains all the information needed to set it up, and examples of such requests.
You can use the response to update your page with relevant information.

Related

Wordpress - Hook on submit form

On a page managed by each customer of my Wordpress website, I have a form with multiple fields, not only Wordpress core fields, but also WooCommerce fields, JetEngine fields, etc.
I need to update the user data on another app, via API. I can't find a hook where I can plug my own function, hook fired when all other hook are processed. Because if I hook profile_update, for example, the updated_user_meta calls have not been made, and all the data are not updated yet.
So my thought led me to find a hook which is fired when the form is submit. This way I will be able to get the form fields in my function (called with add_action) and then launch my API calls.
The problem is I can't find a hook which is fired on form submit. Do you have any idea of how I can manage that?
Thanks for your help!
PS: this question is not helping, because it's about admin form, and I'm not on admin side but on customer side, on his profile form.

Woocommerce action after an order is saved with code (not checkout)

I import orders from Shopify into Wordpress using a plugin, it works fine the orders are created. I'm using the Shopify API to import data from the order Timeline, since the sync plugin isn't capable of doing that on its own. But I need the Shopify ID to import the correct Timeline data, which is stored by the sync plugin as order meta data.
I'm using the action save_post_shop_order which fires every time a new order is imported from Shopify. The problem is, it fires before the meta data is saved, which means the Shopify_ID meta data is always empty, failing to import the Timeline data. If I save the order again, it works fine and imports the Timeline data correctly. save_post_shop_order fires again when an order is saved, and the Shopify_ID meta is available at that point, so it works when saving an already existing order, but not when the Order is first added.
I want a Woocommerce action that fires -after- the Order meta is stored. Some actions to consider:
woocommerce_thankyou : won't work since there is no checkout involved
save_post_shop_order : doesn't work because it's fired before meta is saved in an order
woocommerce_checkout_update_order_meta : there is no checkout, order is created with code
woocommerce_saved_order_items : triggers before meta is saved
From reading this post: WooCommerce: what hook AFTER order is saved/updated from ANY context
I tried using woocommerce_after_order_object_save but that caused a memory overflow, not sure why that happened. Plus it appears to be called on every object change.
Any suggestions?
Edit
The sequence of events:
An order is made in Shopify
Plugin gets the Order and transfers it to Woocommerce
Order is created in Woocommerce by the plugin
save_post_shop_order action hook is triggered, but the meta data required (Shopify_ID) is not yet saved, only the rest of the Order data
I try to get Timeline data for that Order but fails because the Shopify ID isn't valid
After save_post_shop_order is finished, the order is saved WITH the Shopify_ID as well
I want to find an action that executes -after- step 6, because what I've tried is fired either at step 4, or at checkout (I have no checkout)

WooCommerce - How to Call an Xero Action

I have some code that generates a WooCommerce order in the backend using wc_create_order(). My issue is that none of the order actions get called like they would if you had made an order through the front end. For example I have the Xero plugin integrated with WooCommerce but if I create the order using wc_create_order() the invoice never gets sent to my Xero account.
What I want to know is how could I trigger the following xero invoice order action (shown in image on woocommerce order) without having to go to the order page and manually send them.
I have possibly found the action I want to call manually it can be found in the setup_hooks() function on this page git hub link. Is it possible for me to call the woocommerce_order_action_xero_manual_invoice action manually if so how?
You will want to call do_action('woocommerce_order_action_xero_manual_invoice', $order).
$order must be an object of class WC_Order.

Use woocommerce with calculated quot from front end

I'm developing a website for a courier service, that has service quotation filling at the front end.
I did all form things working, a quotation will output variable with the cost of service in PHP.
After filling the form there will be checkout button, with that checkout button i need to save the details of the user, and that price to the woo commerce page to check-out/or fill further details. I searched throughout the google and got 1 solution to create a product with these values in front end. But its became very difficult to figuring out signing in, saving values, keep details in specific formate etc... for me as a beginner in PHP.
Can anyone help me with this?.
i just need woo commerce to make checkout with user entered details in the form.... .
EDIT & Updated :
Thanks for reply LoicTheAztec, I have gone through quick toure now
About code : I'm sending the values calculated in form to the page called buy-now using URL using javascript window.location.href=.
like http://example.com/buy-now?price=120&weight=80&location=000001
And also I'm able to fetch the value using
if (isset($wp_query->query_vars['pricef'])) in page template. (with StackOverflow help) and I'm checking user is logged in? using if(!is_user_logged_in()) and showing login form, After login also i can get the variables.
Now I need to create a transaction using this.
Saving data like a product or post and proceed for payment, I don't need to use woocommerce to do this but need some sort of idea to make it work.
Thank you.

How to pass variables of each item through to paypals cart when selecting paypal's 'add to cart' button?

I am developing a website selling gardening products using PHP and SQL. The user searches for a plant, finds the one they want, clicks on it for more details and there they see an option to add to cart. I have signed up to Paypal's business account and used their add to cart button. But currently, when I try it out and click the button, it doesn't do anything. Obviously this is because know variables have been passed about that specific product. What do I need to do in order to get each item into the cart?
#QUESTION:
To impliment a normal POST to pass to standard form, follow this tutorial:
http://www.evoluted.net/thinktank/web-development/paypal-php-integration
#INTEGRATED SOLUTION:
Integrated solutions will allow you to make these transactions without going offsite.
Using this method requires alot more security.

Categories