When Cart Is Empty Trigger A Js Function In Woocommerce


Support » Plugin: WooCommerce » How to Call the Process_Payment($order_id ) inside a JS function

How to Call the Process_Payment($order_id ) inside a JS function

Hi, well i am back on this cool forum! Got a question, i am builing a custom payment gateway plugin for WP. But the callback i receive from the payment gateway in terms of wether the payment was processed correctly is via Javascript not PHP TOKEN or IPN call back like most payment gateways do, so it is via this JS function or once the payment is processed correctly this function response is called:

var onAuthorize = function(response) <

//Here i can make my custom script to trigger something like redirect to a thank you page cause order was processed correctly but in WordPress i need to trigger a PHP function from the Woocommerce API to process the payment: public function process_payment( $order_id ) So i would to call that function inside this JS function in order to process and close the order.

Thefore i need to trigger this PHP function (from the woocomerce API to process the payment, more info here https://docs.woocommerce.com/document/payment-gateway-api/) to process the payment but i would need to call the function, mentioned below this comment. inside the previous JS function:

So the question is can you call a PHP function inside a JS / Ajax function, better to call it on the same file in which i am handling the payment processing, cause sending the checkout process to another page or refreshing the page i guess will loose cart details information which happened to me via GET, via POST will have to double check.

Anyone had to deal with this problem before, calling a PHP function inside JS? Anyone know if there is JS version of the default Woocommerce API process payment PHP function: public function process_payment( $order_id )

How to Check if the WooCommerce Cart is Empty

We had an interesting question the other day from a user who wanted to know if it’s possible to do something only when the WooCommerce cart is empty. WooCommerce core includes several handy methods that can help you out with this, but lots of tutorials show a cart-empty check with the $woocommerce global. However, this is only necessary if working with WooCommerce 2.0 or earlier. You can instead use the WC() global function to cut this down a bit with WooCommerce 2.1 or newer.

We can do our check by using the WC() global function and getting the cart contents count. While there are several ways we could check this, I prefer this method since it returns as an integer, so we simply need to check if it’s equal to zero to determine if the cart is empty.

That’s all you need to check if the WooCommerce cart is empty in your own snippet or plugin.

Let’s take a look at a real use-case. Suppose you want to show a notice in your store if the cart is empty – perhaps this could encourage a promotion or inform customers of coupon codes. It also works really well as a companion to our WooCommerce Cart Notices extension, as this displays notices if items are in the cart.

We’ll create a notice that will only display if there are no products in the cart, and you can determine where you’d like to display the notice (based on which actions you choose to add it to).

Adding the first action will add this to the empty cart template:

While adding the second action will then display this throughout the rest of the WooCommerce pages, such as the shop archives and product pages.

Our notice will display while the WooCommerce cart is empty, then disappear once an item has been added to the cart (which is a great time to let Cart Notices take over!).

Like it? Share the love:

Published by Beka Rice

Beka leads product direction for SkyVerge, focusing on new features for our plugins and Jilt. She spends a lot of time on research and interviews, but likes to write so she has an excuse to spend more time jamming out to anything from The Clash to Lady Gaga.

How to remove update cart button

Do you have any nice snippet to remove/hide update cart button on cart page of woocommerce shop .
And to make it so that if you change quantity price etc. will be automatically updated.

I found page that has CSS code to hide update cart button and also code for function.php to make total price etc. update when you change the quantity. CSS code didnt work at all. PHP code worked almost but still you need to click on quantity number box to make it update so it wont update just by clicking – or +.
Here is link: https://businessbloomer.com/ (Purchase code hidden if logged out) -change/

Any tips how to solve this?

I already solved that php part, do you think its okay ( check the code below)? I am not even on beginner level in coding anything. Anyway now i need to hide that update cart button ( now ” päivitä ostoskori”). Notice because of this code i need to hide it not remove totally.

Thank you for using Enfold.

I don’t think this is a good idea because the page will refresh or load an AJAX request each time the product quantity is changed. Users will have to wait for the ajax request to finish before they can increase the quantity again. If you really want to implement it, replace the script with this one.

And then use this css code to hide the update button.

Best regards,

I changed code to yours.

It worked for little time. Like hour or two. I tested it and it worked.
Later tried it again and now nothing worked. I mean whole Cart page is disappeared. When i go to cart whole page is empty.
Even if i remove this code from editor function.php . Now once i have tried to find solution i found many say do not make these changes through editor.

Any idea what went wrong? By the way i didnt change anything else after i changed that code.

You can try to access your theme files via ftp. Go to the directory wp-content/themes/ and download the modified files. Then edit them, delete the code and upload them again. Afterwards clear your browser cache and (if necessary) the wordpress cache.

Best regards,

Sorry to bother you with this. I just still couldnt solve it.
I have modified function.php file through FTP. Also cleared browser cache. I dont use any cache plugin on wordpress right now. I have W3 total cache but its disabled until my page is ready.

Cart page is still empty. Also noticed the cart still has products i have added before. ( i can see them by hovering over cart icon on store page)

Please create us an admin account and post a link to your website. Maybe we can find the culprit.

Best regards,

PS. Languange is now something else than english let me know if you want me to change it back to english. Also feel free to change it if you want to.

I couldn’t find any reasons why the cart page doesn’t work. However I’m sure it’s not a theme issue because the cart page doesn’t work with the default wordpress themes (TwentyFifteen, etc.) too.

I’d recommend to contact your server administrator/host – hopefully they can provide error logs or useful hints.

Leave a Reply

Your email address will not be published. Required fields are marked *