如何在 WooCommerce 中自动完成付款订单?

问题描述 投票:0回答:5

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

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

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

/**
 * 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)、“货到付款”(cod) 和“支票”(支票) 付款方式,因此仅适用于 Paypal、信用卡网关,以及其他付款方式。

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

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


使用第 1 点中的 WooCommerce 代码段(作为基础),如何实现基于 WooCommerce 付款方式的条件代码?

我的意思是:如果付款方式不是“银行电汇”、“货到付款”或“支票”,则应用代码片段(将有关虚拟产品的已付款订单的状态更新为“已完成”)。

php wordpress woocommerce payment-gateway orders
5个回答
63
投票

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

此过滤钩位于:

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

说明:

  1. 轻巧有效:

    由于它是一个过滤器钩子,

    woocommerce_payment_complete_order_status 
    仅在需要在线支付时触发(不适用于“支票”,“bacs”或“cod”付款方式)。这里我们只是更改允许的付费订单状态

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

  2. 准确(避免多次通知)

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

因此客户只会收到一次关于“已完成”订单状态的通知。

使用下面的代码,只会

更改已付款订单状态(由支付网关为已付款订单设置)更改为“已完成”:

add_filter( '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官方hook,我找到了这个问题的解决方案*(适用于WC 3+)。

在 Woocommerce 中,对于除

bacs

 
(银行电汇)cheque
cod
 
(货到付款) 之外的所有其他支付网关, 已付款订单状态为“正在处理”和“已完成”

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

代码:

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; } // Autocomplete all others payment methods else { $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 订单的付款方式?

与此:

get_post_meta( $order_id, '_payment_method', true );

来自
helgatheviking

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

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


6
投票
对我来说,即使付款没有完成或失败,这个钩子也会被调用,这会导致付款失败。经过一番研究后,我将其更改为使用“woocommerce_ payment_complete”,因为只有在付款完成时才会调用它,并且它涵盖了上面@LoicTheAztec提到的问题 –

add_action( 'woocommerce_payment_complete', '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; // Updated status to "completed" for paid Orders with all others payment methods } else { $order->update_status( 'completed' ); } }
    

1
投票
对我来说,付款完成时修改订单状态的最简单的挂钩是“woocommerce_order_item_needs_processing”,因为此过滤器挂钩旨在在付款完成时修改目标订单状态。

这就是最终片段的样子:

add_filter('woocommerce_order_item_needs_processing', '__return_false',999);

它还与网站上的其他插件兼容。


0
投票
对我来说,在使用 PayPal Sandbox(WooCommerce PayPal Payments 插件)的测试系统上,

LoicTheAztec 解决方案(2019 更新)仅在我添加 $order->update_status( 'completed' );

 代码行时才有效。 
return 'completed';
 在我的情况下没有效果,我留下它只是因为它是一个过滤器。

add_filter( 'woocommerce_payment_complete_order_status', function( $status, $order_id, $order ) { $order->update_status( 'completed' ); return 'completed'; }, 10, 3 );
    

-1
投票
如果您正在寻找虚拟订单的自动完成功能(例如课程、电子书、可下载内容等),这可能很有用。

* Auto Complete all WooCommerce virtual orders. * * @param int $order_id The order ID to check * @return void */ function custom_woocommerce_auto_complete_virtual_orders( $order_id ) { // if there is no order id, exit if ( ! $order_id ) { return; } // 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; } // get the order and its exit $order = wc_get_order( $order_id ); $items = $order->get_items(); // if there are no items, exit if ( 0 >= count( $items ) ) { return; } // go through each item foreach ( $items as $item ) { // if it is a variation if ( '0' != $item['variation_id'] ) { // make a product based upon variation $product = new WC_Product( $item['variation_id'] ); } else { // else make a product off of the product id $product = new WC_Product( $item['product_id'] ); } // if the product isn't virtual, exit if ( ! $product->is_virtual() ) { return; } } /* * If we made it this far, then all of our items are virual * We set the order to completed. */ $order->update_status( 'completed' ); } add_action( 'woocommerce_thankyou', 'custom_woocommerce_auto_complete_virtual_orders' );

改编自

https://gist.github.com/jessepearson/33f383bb3ea33069822817cfb1da4258

© www.soinside.com 2019 - 2024. All rights reserved.