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

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

问题描述

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

所以此时你可以找到一些有用的东西(但不是很方便):

<块引用>

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

/*** 自动完成所有 WooCommerce 订单.*/add_action('woocommerce_thankyou', 'custom_woocommerce_auto_complete_order');函数 custom_woocommerce_auto_complete_order( $order_id ) {如果(!$order_id){返回;}$order = wc_get_order( $order_id );$order->update_status('完成');}

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

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

还有……

<块引用>

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

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

我的问题:

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

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

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

一些帮助会非常好.

解决方案

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

这个过滤器钩子位于:

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

<块引用>

###说明:

<块引用>

  1. 轻巧有效:

<块引用>

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

<块引用>

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

<块引用>

  1. 准确 (避免多次通知):

<块引用>

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

<块引用>

因此客户只会在完成"时收到一次通知.订单状态.

使用下面的代码,将改变支付订单状态 (支付网关为支付订单设置)到已完成":

add_action( 'woocommerce_payment_complete_order_status', 'wc_auto_complete_paid_order', 10, 3 );函数 wc_auto_complete_paid_order( $status, $order_id, $order ) {返回完成";}

代码位于活动子主题(或活动主题)的 function.php 文件中.

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


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

基于Woocommerce官方hook,我找到了解决这个问题的方法*(Works with WC 3+).

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

所以我的目标是处理"所有支付网关(如 Paypal 或信用卡支付)的订单状态,更新订单状态以完成.

代码:

add_action( 'woocommerce_thankyou', 'wc_auto_complete_paid_order', 20, 1 );函数 wc_auto_complete_paid_order( $order_id ) {如果(!$order_id)返回;//获取 WC_Product 对象的实例$order = wc_get_order( $order_id );//没有更新通过银行电汇、货到付款和支票付款方式交付的订单的状态.if ( in_array( $order->get_payment_method(), array( 'bacs', 'cod', 'cheque', '' ) ) ) {返回;}//对于所有其他付款方式的已付款订单(已付款订单状态正在处理")elseif( $order->has_status('processing') ) {$order->update_status('完成');}}

代码位于活动子主题(或活动主题)的 function.php 文件中.


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

代码:

/*** 在 WOOCOMMERCE 中自动完成付款订单*/add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_paid_order', 10, 1 );函数 custom_woocommerce_auto_complete_paid_order( $order_id ) {如果(!$order_id)返回;$order = wc_get_order( $order_id );//没有更新通过银行电汇、货到付款和支票付款方式交付的订单的状态.if ( ( 'bacs' == get_post_meta($order_id, '_payment_method', true) ) || ( 'cod' == get_post_meta($order_id, '_payment_method', true) ) || ( '支票' == get_post_meta($order_id, '_payment_method', true) ) ) {返回;}//对于所有其他付款方式的已付款订单(付款状态为正在处理")elseif( $order->get_status() === '处理' ) {$order->update_status('完成');}}

代码位于活动子主题(或活动主题)的 function.php 文件中.

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

用这个:get_post_meta( $order_id, '_payment_method', true ); 来自 helgatheviking

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

更新代码以兼容 WC 3.0+ (2017-06-10)

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) 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' );
}

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 is a Direct Bank transfer payment method

And …

2) A plugin: WooCommerce Autocomplete Orders

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

My question:

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

How can I implement conditional code based on woocommerce payment methods?

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

Some help will be very nice.

解决方案

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

This filter hook is located in:

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

###Explanations:

  1. Lightweight and effective:

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.

  1. Accurate (avoid multiple notifications):

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.

So customer is only notified once on "completed" order 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';
}

Code goes in function.php file of the active child theme (or active theme).

Related: WooCommerce: autocomplete paid orders based on shipping method


2018 - Improved version (For WooCommerce 3 and above)

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

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".

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

The code:

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' );
    }
}

Code goes in function.php file of the active child theme (or active theme).


Original answer (For all woocommerce versions):

The code:

/**
 * 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' );
    }
}

Code goes in function.php file of the active child theme (or active theme).

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

with this : get_post_meta( $order_id, '_payment_method', true ); from helgatheviking

"Bank wire" (bacs), "Cash on delivery" (cod) and "Cheque" (cheque) payment methods are ignored and keep their original order status.

Updated the code for compatibility with WC 3.0+ (2017-06-10)

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

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