• 9
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

WooCommerce hook for “after payment complete” actions

I'm using WooCommerce and Wordpress. I have a custom license key generator, and I'd like it to generate a license key when someone successfully purchases my plugin through WooCommerce.

It seems pretty straight-forward:

  1. User completes checkout on my site
  2. User is re-directed to Paypal, where they enter their payment credentials
  3. Paypal tells my site that the payment is complete
  4. I hook in to some sort of "payment complete" or "order_complete" WooCommerce action and generate the license.

Here's the problem: I'm really not sure what hook would work well for this. Woocommerce has their entire collection of hooks listed on their site, but virtually no documentation about which is good for what.

Based on just the hook names, I'd think that woocommerce_payment_complete would be a good action to use. Unfortunately it doesn't seem to be fired at all; some places I've read say that it isn't ever fired.

I've also read something about Paypal IPN, but I don't understand how I could hook in to the notification from that (does that connect to a Woocommerce hook?)

In short, I'd like to generate the license key as soon as the payment has been verified. What do I hook in to in order to achieve this?

Okay, I've come up with a couple possible solutions.

DEALING WITH PAYMENT VERIFICATION

WooCommerce's Paypal Standard gateway actually has support for Paypal's IPN service built right in to it. You'll want to use that regardless of the 'hook' route you choose.

HOW TO SET UP PAYPAL IPN

Paypal IPN is connected to whichever account is the receiver of the money. For example, if you're getting paid at myemail@gmail.com, then you need to set up IPN with that account. All IPN is, is Paypal sending out a "Payment Complete!" (of sorts) notification to the URL of your choice. In order to choose the url that you want Paypal to send the notice to, you need to do these steps:

  1. Log in to Paypal
  2. Click on the little face icon on the top right
  3. Click "Profile and settings"
  4. Click "My selling tools" in the left sidebar
  5. Find "Instant payment notifications" and click "Update"
  6. Enable them and set the url. Your WooCommerce Paypal Payments Standard gateway url is: http://yoursite.com/?wc-api=WC_Gateway_Paypal (which is noted in WooCommerce's Paypal Standard documentation).
  7. Save your settings

Your WooCommerce installation will now hear from Paypal when the payment is complete. Oh, and by the way, make sure that:

  • Your "Receiver E-mail" in your Paypal settings is correct
  • fsockopen is enabled on your server (you can check this "System Status" in your WooCommerce settings)

NOW LET'S GET TO WORK ON THE HOOKS

OPTION 1: Auto-complete orders that have completed payment and hook in to woocommerce_order_status_completed for your special actions/functions

In order to get your products to auto-complete upon verified payment, you can simply install Mirko Grewing's fabulous WooCommerce Autocomplete Order plugin. Be sure to set the correct settings (which will appear under the "Woo Extra Options" tab in your WooCommerce settings).

Now that your orders are being autocompleted when the payments are verified, you can simply hook in to your completion hook, `woocommerce_order_status_completed, for your special functionality.

The benefit to that method is that if allows you to hook in any order that is completed--regardless of whether or not you manually completed it or it was automatically completed. In other words, it's pretty versatile.

OPTION 2: Hook in to the woocommerce_payment_complete_order_status filter, which denotes a successful payment.

I guess that one is pretty self-explanatory. The benefit to this method is that it's pretty much the first step in the whole process, and takes place prior to the completion hook above (if you have your priorities set appropriately).

I don't know if one is better than the other, but they both seem to accomplish my goal: perform an action AFTER payment has been verified.

  • 29
Reply Report
      • 2
    • Ummm...this doesn't depend on Paypal. :-) This only looks at when an order receives the 'complete' order status. Which means that it will also work with manually completed orders (there's even a line in my post that says "The benefit to that method is that if allows you to hook in any order that is completed--regardless of whether or not you manually completed it or it was automatically completed"). The extra autocomplete plugin simply removes the step of having to manually complete the orders when you receive payment.
      • 2
    • Pete, Are you getting OrderID from this hook? I am not getting any data. Content posted is {"action":"woocommerce_payment_complete_order_status","arg":"processing"}
      • 2
    • @PonSaravanan: The woocommerce_payment_complete_order_status filter takes two arguments. The first is the order status, the second is the order id. Alternatively, you can use the woocommerce_payment_complete action, which provides only the order id.

woocommerce_payment_complete_order_status also fires when the payment has been canceled. I've just tested using Paypal as a gateway on Woocommerce 3.1.2.

The action woocommerce_payment_complete seems to fire only when the payment has been successful.

  • 16
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions