• Resolved qtwrk

    (@qtwrk)


    Hi,

    sometimes order got duplicated , rarely to happen , like once for couple days

    I saw on this https://www.ads-software.com/support/topic/duplicate-orders-problem/ , where I also make WP MEMORY to 4GB , but still happened

    the last time it happens , was not at traffic peak

    on these 2 orders , the post_modified_gmt is 2023-08-05 16:56:22 and 2023-08-05 16:56:16 , so I guess something happened in this 6 seconds ?

    the server specs is good , dedicated server on Ryzen 5950X

    WP/WOO are latest version , on php81

    from access log , it seems there was only one request to finalize the order?

    root@reverse:# cat mydomain.com.access.log | grep -i  "05/Aug/2023:18:56" | grep "client_ip_addr"
    https://www.mydomain.com - *1/925990* - client_ip_addr - - [05/Aug/2023:18:56:09 +0200] "GET /finalizar-compra/ HTTP/2" 200 76718 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
    https://www.mydomain.com - *0/335631* - client_ip_addr - - [05/Aug/2023:18:56:18 +0200] "POST /?wc-ajax=update_order_review HTTP/2" 200 1590 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
    https://www.mydomain.com - *8/7524311* - client_ip_addr - - [05/Aug/2023:18:56:15 +0200] "POST /finalizar-compra/ HTTP/2" 302 0 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
    https://www.mydomain.com - *1/940446* - client_ip_addr - - [05/Aug/2023:18:56:23 +0200] "GET /finalizar-compra/order-received/195608/?key=wc_order_JXX78RPovZRfD&utm_nooverride=1 HTTP/2" 200 72473 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"
    https://www.mydomain.com - *3/2803428* - client_ip_addr - - [05/Aug/2023:18:56:21 +0200] "POST /?wc-ajax=checkout HTTP/2" 200 0 "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/115.0.0.0 Safari/537.36"

    emm, please let me know if there is any more information you need to help to troubleshoot this issue

    best regards,

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter qtwrk

    (@qtwrk)

    forgot to mention , only “cash on delivery” payment is enabled , so no external payment gateway callback or something involved

    Thread Starter qtwrk

    (@qtwrk)

    add_action('woocommerce_checkout_create_order', 'check_for_duplicate_order', 10, 2);
    
    function check_for_duplicate_order($order, $data) {
        $user_email = $order->get_billing_email();
        $cart_contents = $order->get_items();
        $cart_total = $order->get_total();
        $current_order_date = $order->get_date_created();
    
        $product_quantities = array();
    
        foreach ($cart_contents as $cart_item) {
            $product_id = $cart_item->get_product_id();
            $quantity = $cart_item->get_quantity();
            $product_quantities[$product_id] = $quantity;
        }
    
    
        $previous_orders = wc_get_orders(array(
            'billing_email' => $user_email,
            'status' => 'processing',
            'date_created' => '>=' . date('Y-m-d H:i:s', strtotime('-1 minutes')),
        ));
    
    
    
        if (!empty($previous_orders)) {
            $prev_order = reset($previous_orders);
            $prev_order_created_date = $prev_order->data['date_created'];	
            $prev_date = $prev_order_created_date->format('Y-m-d H:i:s');
            $prev_cart_contents = $prev_order->get_items();
            $prev_cart_total = $prev_order->get_total();
    
            $prev_product_quantities = array();
    
            foreach ($prev_cart_contents as $prev_cart_item) {
                $prev_product_id = $prev_cart_item->get_product_id();
                $prev_quantity = $prev_cart_item->get_quantity();
                $prev_product_quantities[$prev_product_id] = $prev_quantity;
            }
            ksort($product_quantities);
            ksort($prev_product_quantities);
    
    
            if ($product_quantities == $prev_product_quantities && $cart_total === $prev_cart_total && abs(time() - strtotime($prev_date)) <= 30 ) {
                $response_data = array(
                    'status' => 'failed',
                    'message' => 'Order failed placed.',
                );
                wp_send_json($response_data);
            }	
        }
    }

    somehow , I was automagically worked out this

    it will query by user’s mail , for his or her last order within 1 minute , and extract product id/qty to form an array and also the order total amount, compare with current order’s product id/qty + total

    if product id/qty array and total matches for last order and current order , and the timing is within 30 seconds , consider it as duplicated and reject it

    the error message seems I can not customize it , but I just kind of hope there is some kind of official solution for this , instead of me messing around the checkout process…

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @qtwrk

    It sounds like you’re experiencing a rare issue where orders are getting duplicated. This can be a result of various factors, including server response times, WooCommerce settings, or even customer behavior, such as clicking the “Place Order” button multiple times.

    Your approach to handling the duplication issue by creating a function to check for duplicate orders based on the user’s email, order contents, and order total within a specific time frame is quite insightful. However, I understand your concerns about customizing the checkout process and seeking an official solution.

    • Check for Plugin Conflicts: Sometimes, other plugins might interfere with WooCommerce’s checkout process. Try deactivating other plugins to see if the issue persists. If it doesn’t, reactivate the plugins one by one until you identify the conflicting one.
    • Update WooCommerce Database: Ensure your WooCommerce database is up-to-date. You can do this by navigating to WooCommerce > Status > Tools and Update Database. Please remember to backup your site before performing this action.
    • Server Settings: Although you mentioned your server specs are good, you may want to check your server’s PHP settings. Specifically, check your max_execution_time. If it’s set too low, it might be causing the duplication issue.

    Furthermore, your function checks for orders from the same user within a one-minute time frame. It then compares the product IDs, quantities, and total order cost. If these parameters match and the time difference between the orders is less than or equal to 30 seconds, the function considers the order as a duplicate and rejects it.

    This is a good approach; however, you should consider adding some error handling to give more specific feedback to the user. As for the error message customization, you can indeed customize it. You can replace the “Order failed placed.” string in the $response_data array with your custom message.

    Here’s an example of how you can do it:

    $response_data = array(
    'status' => 'failed',
    'message' => 'Your custom error message here.',
    );

    Remember to make your error message user-friendly and informative to help the customer understand why their order was rejected.

    I hope this helps! If you have any other questions, feel free to ask.

    Thanks!

    Thread Starter qtwrk

    (@qtwrk)

    thanks for the reply

    the issue right now is that , the $response_data never shows up in front

    it just gives Error processing checkout. Please try again. error no matter what I put in that array

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hello @qtwrk

    I suggest checking whether the function that generates the $response_data is being executed properly. You can do this by adding some log statements inside the function and checking the log after a checkout attempt.

    Another reason the issue might be occurring is that your custom code is not throwing an exception that WooCommerce can catch and display. Instead of using wp_send_json(), you should throw an exception that WooCommerce will catch and display the message from.

    Can you modify your code like bellow and try again:

    if ($product_quantities == $prev_product_quantities && $cart_total === $prev_cart_total && abs(time() - strtotime($prev_date)) <= 30 ) {
    throw new Exception('Your custom error message here.');
    }

    Please note that custom coding is not something we can assist with directly. However, if you’re still having trouble, don’t hesitate to reach out to some of our great resources available for support. Our WooCommerce community is brimming with skilled open-source developers who are active on the following channels:

    Thanks!

    Thread Starter qtwrk

    (@qtwrk)

    oh yeah , thanks, this did the trick , thanks again

    Hi @qtwrk that’s great to know! We’re glad we were able to help! If you have a few minutes, we’d love if you could leave us a review: https://www.ads-software.com/support/plugin/woocommerce/reviews/

    I’m going to mark this thread as resolved, but please don’t hesitate to start a new topic if you have any more questions down the line.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Order duplicated’ is closed to new replies.