防止支付网关将订单状态从已取消更改为待处理/完成

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

我正在使用 WooCommerce 销售一些数字产品。由于我们业务的性质,我们不希望商品库存低于 0(-1、-2 等)

我们目前接受不同的付款方式,包括加密货币和 Stripe。由于加密货币的交易速度较低,有时需要长达1小时,所以我们面临这样的问题:

  1. 当用户(用户 A)购买商品并使用加密货币付款时,商品库存将被保留(从 1 变为 0)。由于区块链速度较慢,需要2个小时才能完成支付。由于速度太慢,商品库存从 0 回到 1。

  2. 当区块链支付尚未完成时,另一个用户(用户B)来购买相同的产品。此时,该物品不再被持有。因此,该用户获得了该产品。商品库存从 1 变为 0。

  3. 区块链支付终于确认。 Woocommerce 还将产品提供给该用户。商品库存从 0 变为 -1。

因此 2 个用户获得相同的产品。

我想阻止 coinpaids 和 coinbase 将订单状态从已取消更改为待处理或已完成。

所以我尝试使用以下代码:

add_action('woocommerce_order_status_changed', 'prevent_status_change_after', 99, 4);

function prevent_status_change_after($order_id, $old_status, $new_status, $order) {
    $payment_method = $order->get_payment_method();
    
    if ($payment_method == 'coinbase' || $payment_method == 'coinpayments') {
        if ($old_status == 'cancelled' && ($new_status == 'completed' || $new_status == 'pending')) {
            // Optionally, add a note to the order
            $order->add_order_note('Attempt to change status from "cancelled" was prevented.');
            
            // Revert back to 'cancelled' status
            remove_action('woocommerce_order_status_changed', 'prevent_status_change_after', 99);
            $order->update_status('cancelled');
            add_action('woocommerce_order_status_changed', 'prevent_status_change_after', 99, 4);
        }
    }
}

但是好像不起作用。

如有任何帮助,我们将不胜感激。

wordpress woocommerce hook-woocommerce
1个回答
0
投票

在没有任何保证的情况下,请尝试以下操作:

add_action('woocommerce_order_status_cancelled_to_pending', 'prevent_cryto_cancelled_status_change', 99, 2);
add_action('woocommerce_order_status_cancelled_to_completed', 'prevent_cryto_cancelled_status_change', 99, 2);
function prevent_cryto_cancelled_status_change( $order_id, $order ) {
    if ( in_array( $order->get_payment_method(), ['coinbase', 'coinpayments'] ) ) {
        // Revert back to 'cancelled' status
        $order->set_status('wc-cancelled', __('Attempt to change status from "cancelled" was prevented.')); 
        $order->save();
    }
}

代码位于子主题的functions.php 文件中(或插件中)。应该可以。

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