Woocommerce一笔交易产生两个订单号的解决方法

我们有客户咨询为什么woocommerce订单后台一笔交易产生了两个订单号,但是客户只使用paypal支付了一次。

 一、问题导致的后果:

存在这个问题会让后台订单页面显得比较混乱。也不利于统计和发货,因此我们需要改进一下。

  二、问题出现的原因:

正常流程:用户付款WooCommerce –创建订单—PayPal 返回 transaction_id—更新订单状态

错误流程:用户付款—创建订单 #1697—Webhook 又触发—再次 create_order()

—生成订单号#1698

核心问题:同一个 transaction 被处理了两次。

可能出现这个问题的原因: 1. 旧 PayPal 插件(最高概率) 2. PayPal IPN + Webhook 同时开启

  1. Checkout 页面缓存

尤其: SiteGround、LiteSpeed、Cloudflare APO等

  1. AJAX Checkout 插件

 三、问题的解决方法:

1.安装WooCommerce PayPal Payments 官方插件,不要再用

PayPal Standard老版 或者PayPal Checkout。

2.如果用了各种缓存插件或者是CDN,务必排除/cart/   /checkout/  /my-account/ 等页面的缓存。

3.做 Transaction ID 去重。

add_action('woocommerce_checkout_order_processed', 'prevent_duplicate_paypal_orders', 1, 3);

function prevent_duplicate_paypal_orders($order_id, $posted_data, $order){

    $transaction_id = $order->get_transaction_id();

    if(empty($transaction_id)){
        return;
    }

    $args = array(
        'limit' => -1,
        'exclude' => array($order_id),
        'transaction_id' => $transaction_id,
    );

    $orders = wc_get_orders($args);

    if(!empty($orders)){

        $order->update_status('cancelled', 'Duplicate PayPal transaction detected.');

    }
}

 

把代码加到Functions.php

  四、总结:

以上只是大胡说跨境给大家说的解决问题的思路和方法。鉴于每个独立站的搭建环境、主题、插件等的不同,有可能不能立即解决问题。但是思路和方法可以借鉴,也需要大家去尝试去实践。

上一篇 Wordpress独立站后台教程
AeroCore图片
AeroCore图片
AeroCore图片
AeroCore图片
AeroCore图片
最新评论
暂无评论