简体   繁体   English

根据支付网关和转换状态更改 Woocommerce 订单状态

[英]Change Woocommerce order status based on payment gateway and transition status

So I know how to change status based on payment gateway.所以我知道如何根据支付网关更改状态。 But is there a way to also take the previous order status into account?但是有没有办法同时考虑之前的订单状态?

My issue is as following: we start processing our orders after the customer sign an e-contract.我的问题如下:在客户签署电子合同后,我们开始处理我们的订单。 They are then free to pay later via a couple gateways (Stripe, COD, bank transfer or invoice - we send payment links).然后他们可以通过几个网关(Stripe、COD、银行转账或发票 - 我们发送付款链接)自由付款。

This means that customers can pay during any of our order statuses (from pending, awaiting signature to processing-unpaid, delivery-unpaid, completed ect).这意味着客户可以在我们的任何订单状态下付款(从待处理、等待签名到处理未付款、交付未付款、已完成等)。

We also have 'Paid' versions of these statuses: processing-paid, delivery-paid etc.我们也有这些状态的“付费”版本:处理付费、送货付费等。

So for example: payment gateway Stripe triggers status 'Processing-paid'.例如:支付网关 Stripe 触发状态“处理付款”。 That's fine if the previous status was 'Processing-unpaid'.如果之前的状态是“Processing-unpaid”,那就没问题了。 However, if we're further along the process, after we already have delivered, say, it should ideally change from 'Delivery-unpaid to 'Completed'.但是,如果我们进一步推进流程,例如在我们已经交付之后,理想情况下应该从“交付未付款”更改为“已完成”。

I cannot for the life of me figure out how to get this done.我一生都无法弄清楚如何完成这项工作。 Any ideas?有任何想法吗?

Here is an example using woocommerce_order_status_changed hook where you can target your orders statuses transition "from" and "to" , to change the order status to any other.这是一个使用woocommerce_order_status_changed钩子的示例,您可以在其中将订单状态转换为"from""to" ,以将订单状态更改为任何其他状态。

In this example I target the statuses "from" delivery-unpaid and "to" delivery-paid to change the order status to completed .在此示例中,我将状态定位为"from" delivery-unpaid"to" delivery-paid以将订单状态更改为completed

As you can see in Woocommerce the Order statuses are slugs (all in lowercase)正如您在 Woocommerce 中看到的,订单状态是 slugs (全部为小写) ……

You can target, at the same time, a specific payment gateway ID like Stripe using something like:您可以同时使用以下内容定位特定的支付网关 ID,例如Stripe

add_action( 'woocommerce_order_status_changed', 'change_order_status_conditionally', 10, 4 );
function change_order_status_conditionally( $order_id, $status_from, $status_to, $order ) {
    if( $order->get_payment_method() === 'stripe' && $status_from === 'delivery-unpaid' && $status_to === 'delivery-paid' ) {
        $order->update_status( 'completed' );
    }
}

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 It should works.它应该有效。

As you can see you can add as many IF statements targeting the order statuses transitions that you need to change it to any other status and payment gateways.如您所见,您可以添加尽可能多的针对订单状态转换的IF语句,您需要将其更改为任何其他状态和支付网关。

Code goes in function.php file of your active child theme (or active theme).代码位于活动子主题(或活动主题)的 function.php 文件中。 It should works.它应该有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM