• Resolved hansderuiter

    (@hansderuiter)


    I have a weird glitch. WooCommerce analytics double-counted two orders a few days ago. So the order count for the day is +2, and the net sales is also off by the value of those two sales. However, the orders list has the correct number of sales for the day.

    This happened within half a day after I renamed the product that was bought in the two duplicated orders. Perhaps the renaming is somehow the trigger.

    How can I correct the analytics? I’ve already tried clearing the analytics cache and reimporting historical data. Sadly, that did nothing.

Viewing 7 replies - 16 through 22 (of 22 total)
  • Just a quick update for the readers to inform that we found the quirk that causes the data duplication. It’s actually linked to the WooCommerce core. We have a piece of logic that changed slightly. Here’s an example of what changed, simplified for easier understanding:

    add_action('woocommerce_after_order_object_save', function(WC_Order $order) {
    $exchange_rate = get_exchange_rate();
    $order->update_meta_data('_order_total_base_currency', $order->get_total() * $exchange_rate);
    $order->update_meta_data('_order_total_tax_base_currency', $order->get_total_tax() * $exchange_rate);
    // Save more meta, converting order properties

    // Save the exchange rate last
    $order->update_meta_data('_base_currency_exchange_rate', $exchange_rate);
    $order->save_meta_data();
    });

    To this:

    add_action('woocommerce_after_order_object_save', function(WC_Order $order) {
    $exchange_rate = get_exchange_rate();
    // Save the exchange rate first
    $order->update_meta_data('_base_currency_exchange_rate', $exchange_rate);

    $order->update_meta_data('_order_total_base_currency', $order->get_total() * $exchange_rate);
    $order->update_meta_data('_order_total_tax_base_currency', $order->get_total_tax() * $exchange_rate);
    // Save more meta, converting order properties

    $order->save_meta_data();
    });

    For reasons that I can’t explain, if action woocommerce_after_order_object_save is triggered more than once, the behaviour will be different in the two cases:

    1. In the first case, the call to $order->update_meta_data('_base_currency_exchange_rate', $exchange_rate); adds the meta the first time and updates it the second time.
    2. In the second case, the call to $order->update_meta_data('_base_currency_exchange_rate', $exchange_rate); adds the meta twice, resulting in data duplication.

    We couldn’t figure out the reason behind this inconsistent behaviour. The only speculation, which hasn’t been proven, is that calling the WC_Order::get_total(), WC_Order::get_total_tax() and so on causes the custom meta set by WC_Order::update_meta_data() to be “forgotten”, so to speak, despite the call to WC_Order::save_meta_data(). I’m aware that it doesn’t make much sense, but the test results are consistent. Saving the exchange rate last doesn’t cause any duplication, for some reason.

    We’re now implementing a safeguard, so that the meta won’t be duplicated despite this unexpected behaviour.

    Additional note
    So far, we’ve only noticed this behaviour on a site with the HPOS feature disabled, where the orders are stored in the wp_posts table. With the HPOS feature enabled, this doesn’t seem to happen. However, we will run more tests to check if that actually makes a difference.

    • This reply was modified 1 year, 1 month ago by Diego. Reason: Added note about HPOS
    Thread Starter hansderuiter

    (@hansderuiter)

    @daigo75

    Great work! I’ve just had the time to look into the database, and I’ve found the duplicate exchange rates and cleaned them up.

    I am using HPOS, BTW, so it can happen with HPOS enabled.

    Looking forward to the update with the safeguard implemented.

    @hansderuiter thanks for your feedback. It’s curious, because we haven’t been able to trigger the issue on any of our test servers, with our without the HPOS feature enabled. On a customer’s site, it only occurs with the HPOS feature disabled.

    It looks like there is some other element that comes into play, and the fact that the condition in which the glitch occurs is so “nonsensical” is curious to say the least. In any case, the fix we put together is working, so the error should be prevented for the time being.

    Thread Starter hansderuiter

    (@hansderuiter)

    @daigo75

    Maybe there’s another plugin involved, although I have no idea which one(s). If it helps, I can dump a list of all plugins installed, so that we can compare it with @brentst’s store.

    Plugin Support shahzeen(woo-hc)

    (@shahzeenfarooq)

    Hi there!

    I understand your concern. You can view all installed plugins on your site by navigating to Plugins → Installed Plugins in your site dashboard.

    Also, I would suggest you run a conflict test to find which plugin causes that issue. Here is the instruction to guide you on how to run a conflict test without effecting the live site.
    https://woocommerce.com/document/how-to-test-for-conflicts/

    In the meantime, I’d like to understand your site properly. Please share with us the necessary information below for us to investigate the issue further:

    • System Status Report which you can find via WooCommerce > Status > Get system report > Copy for support.
    • Fatal error logs (if any) under WooCommerce > Status > Logs.

    Once we have more information, we’ll be able to assist you further.

    For the benefit of anyone who reads this conversation, we are currently distributing an update of our Aelia Currency Switcher that includes a safeguard to prevent the data duplication. The update server rolls out new versions in batches, if the update is not visible it will become available shortly.

    Zee

    (@doublezed2)

    Hello Diego,

    Thank you for sharing this update.

    It’s great to hear about the safeguard in place.
    Looking forward to seeing how the update performs and hoping it effectively resolves the issue.

    Have a great day!

Viewing 7 replies - 16 through 22 (of 22 total)

The topic ‘Two orders double-counted in WooCommerce Analytics’ is closed to new replies.