woocommerce empty cart via api not working - wordpress

I'm building an SPA using wordpress and woocommerce. Everything works fine (displaying products, handling payments, etc), except for emptying the user's cart after payment is complete. I wrote most endpoints custom.
In my payment handling endpoint, I wrote WC()->cart->empty_cart() after successful payments. This seems to work when I check WC()->cart->get_cart before and after emptying it (it's full before, empty after), however, when I subsequently open my cart modal (also using WC()->cart->get_cart(), but via a GET request), the items are still in there. If I complete the checkout via the woocommerce standard checkout pages instead of the SPA the cart empties just fine.
I know there are some other questions on this forum regarding this issue, but none of the solutions work for me. I tried WC()->cart->empty_cart(true) and setting 'cart' in the WC()->session to an empty array. I don't really know where else the cart is stored, or how WC()->cart accesses this information, but I'm guessing it has to do with the fact that I'm trying to empty via an asynchronous request?

As it turns out, this problem only occurred for logged in users. There is a persistent cart for logged in users, that is supposed to be cleared when true is passed into empty_cart (default). However, since this request is made via API, the wordpress function get_current_user_id that is used by empty_cart returns 0 because it doesn't have access to the correct user global on the server. This can be solved by adding a wordpress nonce in the headers of the ajax request to make the user available to the server. When this nonce is supplied, the aforementioned function to clear the cart works just fine. More info about nonces: https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

PHP call too WC()->cart->empty_cart() won't magically call the frontend work which is needed to refresh the HTML.
to update the HTML you would have to do something like
jQuery(document.body).trigger('wc_fragment_refresh');
Feel free to check wp-content/plugins/woocommerce/assets/js/frontend/cart-fragments.js and see what it actually does.

Related

WooCommerce pay for manually created orders without logging in

I have a situation where customers cannot check out for manually created orders without logging in. When checking out via the direct pay link, if they are not logged in they are still able to see the order total and check out via PayPal or card, however when checking out via PayPal they will be returned to an empty cart page and when checking out via card, they will be sent to the page before the 3D secure redirect and thus their order will not be processed.
We had taken payments before in this way, via manually created orders and sending the customer the payment link however I believe updating Wordpress/PHP caused this error. Can anyone help?
I think this snippet will surely help to allow customer to pay without login in.
Please have a look over here

Is it safe to make a Facebook Graph API Call from the Client using a Page Access Token?

I want to call Facebook's Graph API from the client to READ public posts on a page using a permanent Page Access Token. I'm curious if this is safe (or how to make it safe). From everything I've read and tried, an access token is required to perform this operation and none of Facebook's other functionality will suit my needs (see background below).
https://developers.facebook.com/docs/javascript/reference/FB.api/ suggests that it is safe to expose Page Access Tokens to the client, but I'm suspicious they're lying to me.
Background:
I'm working with a group that wants to display some posts from their Facebook feed on their WordPress website. They don't want to display all posts, but rather filter them based on a hashtag. I don't know a ton about WordPress, but I'm guessing I can't implement custom back-end API calls on a WordPress site. Please suggest any alternative solutions that you might have!
Access Token are like house keys, you NEVER leave them in front of the door. Which means, you should NEVER hardcode any Access Token. No matter if it is an App, Page or User Token. The docs do not state anywhere that it is ok to expose tokens, they just state how you can use a Page Token on that page in your question - but you cannot just hardcode it on the client.
Vulnerabilities: Without extra permissions, this would be more like a privacy issue, because you can get a lot more info with a Page Token (as the Page owner) than with a simple App Token.
Of course it is possible to do that kind of backend-calls with wordpress. And it would be the far better way, i think.
First you can use the WP-Cron to time your Facebook-Calls. You could create a Cron-Job every hour or so.
WP Developer Scheduling WP Cron Events
Wordpress Codex wp_cron Function Reference
Kinsta Knowledgebase Wordpress Cron Job Tutorial
If you work first time with the WP-Cron have one thing in mind: WP-Cron depends on Site-Activity to be run. So if there is no Traffic on the Wordpress-Site, no Cron-Jobs are run. Here is a link how to add WP-Cron to your systems cron, so your API-Call will be called every time you intend to:
WP Developer Hooking WPCron into System Task Scheduler
The Cron-Job than can use the Wordpress HTTP API to do the call. You should check the Docs on the HTTP API and the wp_remote_request function in special.
Wordpress Codex HTTP API
WP Developer wp_remote_request Reference
Or you could install the Facebook SDK with your Theme/Plugin.
I'm not sure about the security impacts when using a Page Access Token on the Client, but it seems to me, that it would be a better way to do that kind of job.

Google Forms PayPal Express Integration

I am doing work for a non profit with 0 budget for IT. They need to allow users to enter information on a Google Form and then collect payment. I have done a lot of research on the topic and currently appears you can only add a hyperlink on the Google Forms to link to PayPal. However, I was wondering if there was some way to link the PayPal Express Checkout javascript(https://developer.paypal.com/docs/integration/direct/express-checkout/integration-jsv4/add-paypal-button/) into the Google Form so it calls back with a transaction id and status. Then I would be able to somehow insert those values into the Google Form. Any thoughts on how to integrate would be helpful. I am a developer but not familiar with Google Forms.
I recently wrestled with this exact question and ended up building a G Suite addon to make this easier for others. Coincidentally I also do volunteer IT for a small nonprofit and we wanted to use PayPal with our signup form (there's pricing for the addon but just ping me if you're a small nonprofit).
Here's what I learned while building it:
There's no way for Apps Script to modify the form once it's shown to the user, or to use Apps Script to respond to user input
There's no way to redirect the user automatically to PayPal after a form is submitted
That means if you want to send someone to PayPal, you've got to use an ordinary link. You can do this within the form by adding the URL to a question description, or you can add the URL to the text shown once someone has submitted the form. For PayPal, you've got two options for this kind of link:
PayPal buttons
Express checkout
PayPal buttons are a static link managed by PayPal. They don't require any coding - just go here and create a button. Then you need to get the raw text link for the button, which PayPal calls the "email" version. You can insert that into your form directly and tell users to access it. The one problem is there's no real way to get the transaction ID. You could try correlating the form submit time with the payment time, or the payer email with the form submit email. It's possible that neither of them will match up and you'll have to do it manually.
Express checkout requires you to dynamically create a new link for each payment by calling the PayPal API. That means your link needs to open a page that then generates an express checkout URL and redirects the user to it. You can do this using a web app in Google Apps Script using a doGet() trigger, or you can create your backend on your server in any language.
If you can run your own server somewhere, I recommend that (plenty of PaaS services have a free tier). It's much easier to test and debug things when you aren't using Apps Script. I used the PayPal Node SDK which works great despite being unmaintained. (Express checkout is "deprecated" by PayPal, but I'll bet it's not going away anytime soon). Their example will get you most of the way there. When the user arrives, generate a payment link and redirect them. When they finish they'll return to your server, and you can display whatever you want. For example, you can ask them to copy the transaction ID and paste it into your form.
Finally, when your form is submitted an onSubmit() trigger can be set up. There are actually two kinds of onSubmit() triggers for forms - one for the form itself, and a second for the spreadsheet linked to the form. You can register a trigger to do extra processing (e.g. look up the transaction ID), but you can't modify the response in the trigger. You can however modify the spreadsheet where the trigger gets sent, which for most cases is equivalent. For example, you could add a column to the spreadsheet with a link to the PayPal transaction based on the transaction code.

Woocommerce checkout form after filling gives error "Fill in the required fields"

I have worked on a website that was built many years ago by a developer who is not updating it. recently I have added Woocommerce. but there is a problem. after filling the checkout form there is an error msg appears saying please fill in all the required fields although filling them all. it happens for all payment gateways. but if the user logged in it works and proceed to Paypal or whatever payment gateway. so this problem happens only if the user is not logged in.
I have dequeued the javascript files and deactivated the plugins and there is no way. is there any idea?
The information is most likely a primary key for the table. When a user signs up they enter the data into the SQL DB so it will not render an error for a signed up user to make a purchase. If a user is not signed up the information will need to be entered for your database to be relational(SQL will have a requirement for this data). This is the reason you are rendering the error. Your fix is one of the following:
1)Display the required entry field by commenting out the css.
2)Depending on the size of the shop and how much work you need to do it might be faster and easier to rebuild it.
3)Rewrite the Database from normal form.

paypal buy it now button process flow

Im adding paypal payments to my site via a buy it now button, which as i understand it posts a form to paypal then the user completes payment on paypal's site.
To process the order after payment is confirmed im using an IPN listener, but id like to do some processing on our server at the time of ordering too. Is it possible to have the buy it now button submit to our own server first so i can record the order, before redirecting to paypal for payment details?
You have two options here.
First option that is more simple but can fail if the user did not have javascript, is to make a post to your server with what data you like, with ajax, and only after the return you let the user continue the submit.
Second option is to post your data to your server on code behind, there you prepare a simple page with auto submit to paypal - actually you make a redirect with post. Here you can find source code and one example on how to make it:
http://www.codeproject.com/Articles/37539/Redirect-and-POST-in-ASP-NET
Actually, you have many options.
As you said, the buy now button is nothing more than a form post to PayPal. You can build your own form using any of their standard variables rather than be limited to these buttons. They call this the cart upload method.
With this method you can do whatever you need to do whatever local processing you need to do prior to ever sending the user over to PayPal. It's a good practice to create an order record of some kind in your own system which you can then include with your PayPal payment info. That same value will come back in IPN so you can process it post-payment as well.
Another option that would you open you up even more is to use the Express Checkout APIs. This gives you complete control of your checkout system and has more features than standard payments. I recommend this if you're familiar with web service APIs.
Of course, IPN works with Express Checkout, too, so you can still use that for whatever you want/need as well.

Resources