php 用于“付款完成后”操作的 WooCommerce 挂钩

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28218580/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:45:13  来源:igfitidea点击:

WooCommerce hook for "after payment complete" actions

phpwordpresspaypalwoocommerce

提问by Pete

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.

我正在使用 WooCommerce 和 Wordpress。我有一个自定义许可证密钥生成器,当有人通过 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.
  1. 用户在我的网站上完成结帐
  2. 用户被重定向到 Paypal,在那里他们输入他们的支付凭证
  3. Paypal 告诉我的网站付款已完成
  4. 我连接到某种“付款完成”或“order_complete”WooCommerce 操作并生成许可证。

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

问题是:我真的不确定什么钩子可以很好地解决这个问题。Woocommerce在他们的网站上列出了他们的整个钩子集合,但几乎没有关于哪个有什么好处的文档。

Based on just the hook names, I'd think that woocommerce_payment_completewould 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.

仅基于钩子名称,我认为这woocommerce_payment_complete将是一个很好的操作。不幸的是,它似乎根本没有被解雇;我读过的一些地方说它从未被解雇。

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?)

我也读过一些关于 Paypal IPN 的内容,但我不明白我怎么能从那里连接到通知(是否连接到 Woocommerce 挂钩?)

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?

简而言之,我想在验证付款后立即生成许可证密钥。为了实现这一目标,我需要做什么?

采纳答案by Pete

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.

WooCommerce 的 Paypal Standard 网关实际上支持内置的 Paypal IPN 服务。无论您选择哪种“挂钩”路线,您都需要使用它。

HOW TO SET UP PAYPAL IPN

如何设置 PAYPAL IPN

Paypal IPN is connected to whichever account is the receiver of the money. For example, if you're getting paid at [email protected], 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:

Paypal IPN 连接到收款方的任何帐户。例如,如果您通过 [email protected] 获得报酬,那么您需要使用该帐户设置 IPN。所有的 IPN 都是 Paypal 发出“付款完成!” (各种)通知到您选择的 URL。为了选择您希望 Paypal 将通知发送到的网址,您需要执行以下步骤:

  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
  1. 登录支付宝
  2. 点击右上角的小脸图标
  3. 点击“个人资料和设置”
  4. 点击左侧边栏中的“我的销售工具”
  5. 找到“即时付款通知”并点击“更新”
  6. 启用它们并设置 url。你WooCommerce PayPal付款标准网关地址是:http://yoursite.com/?wc-api=WC_Gateway_Paypal(这是WooCommerce在指出贝宝标准的文档)。
  7. 保存您的设置

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

付款完成后,您的 WooCommerce 安装现在将收到 Paypal 的消息。哦,顺便说一下,请确保:

  • 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)
  • 您的 Paypal 设置中的“收件人电子邮件”是正确的
  • fsockopen 在您的服务器上启用(您可以在 WooCommerce 设置中检查此“系统状态”)

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_completedfor your special actions/functions

选项 1:自动完成已完成付款并关联到woocommerce_order_status_completed您的特殊操作/功能的订单

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

为了让您的产品在验证付款后自动完成,您只需安装 Mirko Grewing 出色的WooCommerce 自动完成订单插件即可。确保设置正确的设置(将显示在 WooCommerce 设置中的“Woo Extra Options”选项卡下)。

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.

现在您的订单会在付款验证时自动完成,您可以简单地连接到您的完成挂钩,`woocommerce_order_status_completed,以获得您的特殊功能。

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_statusfilter, which denotes a successful payment.

选项 2:连接woocommerce_payment_complete_order_status过滤器,表示支付成功。

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.

我不知道一个是否比另一个更好,但它们似乎都实现了我的目标:在验证付款后执行操作。

回答by Daniel T

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

woocommerce_payment_complete_order_status当付款被取消时也会触发。我刚刚在 Woocommerce 3.1.2 上使用 Paypal 作为网关进行了测试。

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

该操作woocommerce_payment_complete似乎仅在付款成功时触发。