WooCommerce:自动完成付款的订单 [英] WooCommerce: Auto complete paid orders

查看:134
本文介绍了WooCommerce:自动完成付款的订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常wooCommerce应该自动完成虚拟产品的订单.但这不是事实,这是一个实际的问题,甚至是像BUG这样的问题.

Normally wooCommerce should autocomplete orders for virtual products. But it doesn't and this is a real problem, even a BUG like.

因此,此时您可以找到一些有用的东西(但并不十分方便):

So at this point you can find somme helpful things(but not really convenient):

1)代码段(您可以在wooCommerce文档中找到):

1) A snippet code (that you can find in wooCommerce docs):

/**
 * Auto Complete all WooCommerce orders.
 */
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_order');
function custom_woocommerce_auto_complete_order( $order_id ) {
    if ( ! $order_id ) {
        return;
    }

    $order = wc_get_order( $order_id );
    $order->update_status( 'completed' );
}

但是此代码段不适用于 BACS * 货到付款支票付款方式. Paypal和信用卡网关的付款方式都可以.

But this snippet does not work for BACS*, Pay on delivery and Cheque payment methods. It's ok for Paypal and Credit Card gateways payment methods.

* BACS 是直接银行转帐付款方式

还有……

2)一个插件: WooCommerce自动完成订单

2) A plugin: WooCommerce Autocomplete Orders

此插件适用于所有付款方式,但不适用于其他信用卡网关的付款方式.

This plugin works for all payment methods, but not for other Credit Card gateways payment methods.

我的问题:

使用(作为基础)第1点中的wooCommerce代码段:

Using (as a base) the wooCommerce snippet in point 1:

如何基于woocommerce付款方式实现条件代码?

我的意思是这样的:如果付款方式不是"BACS",货到付款"和支票",则应用代码段代码(与虚拟产品有关的已付款订单的更新状态为已完成").

I mean something like: if the payment methods aren't "BACS", "Pay on delivery" and "Cheque" then apply the snippet code (update status to "completed" for paid orders concerning virtual products).

一些帮助会非常好.

推荐答案

最准确,有效和轻巧的解决方案 (适用于WooCommerce 3及更高版本)- 2019

The most accurate, effective and lightweight solution (For WooCommerce 3 and above) - 2019

此过滤器挂钩位于:

  • WC_Order Class inside payment_complete() method which is used by all payment methods when a payment is required in checkout.
  • WC_Order_Data_Store_CPT Class inside update() method.

如您所见,默认情况下,允许的付款订单状态为处理中"和已完成".

As you can see, by default the allowed paid order statuses are "processing" and "completed".

说明:

  1. 轻巧有效:

由于它是一个过滤器挂钩,woocommerce_payment_complete_order_status 仅在需要在线支付时触发 (不适用于支票","bacs"或鳕鱼"支付方式).在这里,我们只需更改允许的付款订单状态.

As it is a filter hook, woocommerce_payment_complete_order_status is only triggered when an online payment is required (not for "cheque", "bacs" or "cod" payment methods). Here we just change the allowed paid order statuses.

因此无需为支付网关或其他任何条件添加条件.

So no need to add conditions for the payment gateways or anything else.

准确 (避免出现多个通知):

这是避免同时发送2个不同的客户通知的唯一方法:
•一个用于处理"订单状态
•一个用于已完成"订单状态.

This is the only way to avoid sending 2 different customer notifications at the same time:
• One for "processing" orders status
• And one for "completed" orders status.

因此,仅在完成"的订单状态下通知客户一次.

使用下面的代码,只需将更改已付款订单状态 (由付款网关为已付款订单设置)为已完成":

Using the code below, will just change the paid order status (that is set by the payment gateway for paid orders) to "completed":

add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );
function wc_auto_complete_paid_order( $status, $order_id, $order ) {
    return 'completed';
}

代码进入活动子主题(或活动主题)的function.php文件.

相关: WooCommerce:根据运输方式自动完成付款订单

2018-改进版 (适用于WooCommerce 3及更高版本)

基于Woocommerce官方钩子,我找到了解决此问题的方法*(适用于WC 3 +).

Based on Woocommerce official hook, I found a solution to this problem *(Works with WC 3+).

在Woocommerce中,除bacs (银行电汇)chequecod (货到付款)以外的所有其他支付网关已付款订单的状态为处理中"和已完成" .

In Woocommerce for all other payment gateways others than bacs (Bank Wire), cheque and cod (Cash on delivery), the paid order statuses are "processing" and "completed".

因此,我将所有付款网关(例如Paypal或信用卡付款)的处理"订单状态作为目标,以更新订单状态以完成.

So I target "processing" order status for all payment gateways like Paypal or credit card payment, updating the order status to complete.

代码:

add_action( 'woocommerce_thankyou', 'wc_auto_complete_paid_order', 20, 1 );
function wc_auto_complete_paid_order( $order_id ) {
    if ( ! $order_id )
        return;

    // Get an instance of the WC_Product object
    $order = wc_get_order( $order_id );

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
    if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {
        return;
    } 
    // For paid Orders with all others payment methods (paid order status "processing")
    elseif( $order->has_status('processing') ) {
        $order->update_status( 'completed' );
    }
}

代码进入活动子主题(或活动主题)的function.php文件.

原始答案 (适用于所有woocommerce版本):

代码:

/**
 * AUTO COMPLETE PAID ORDERS IN WOOCOMMERCE
 */
add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );
function custom_woocommerce_auto_complete_paid_order( $order_id ) {
    if ( ! $order_id )
    return;

    $order = wc_get_order( $order_id );

    // No updated status for orders delivered with Bank wire, Cash on delivery and Cheque payment methods.
    if ( ( 'bacs' == get_post_meta($order_id, '_payment_method', true) ) || ( 'cod' == get_post_meta($order_id, '_payment_method', true) ) || ( 'cheque' == get_post_meta($order_id, '_payment_method', true) ) ) {
        return;
    } 
    // For paid Orders with all others payment methods (with paid status "processing")
    elseif( $order->get_status()  === 'processing' ) {
        $order->update_status( 'completed' );
    }
}

代码进入活动子主题(或活动主题)的function.php文件.

在这篇文章的帮助下: 如何通过ID检查WooCommerce订单上的付款方式?

With the help of this post: How to check payment method on a WooCommerce order by id?

与此:get_post_meta( $order_id, '_payment_method', true );来自 helgatheviking

银行电汇,货到付款和支票付款方式将被忽略,并保持其原始订单状态.

Bank wire, Cash on delivery and Cheque payment methods are ignored and keep they original order status.

更新了与WC 3.0+(2017-06-10)兼容的代码

这篇关于WooCommerce:自动完成付款的订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆