Resolving Order Creation with WooCommerce and WCML

If you're running a WordPress site with WooCommerce and the WooCommerce Multilingual & Multicurrency (WCML) plugin, you might have encountered an error when trying to create orders in the admin panel. This post will help you understand the issue and provide guidance on potential solutions while we wait for an official fix from the WCML team.

Order Creation Bug

Users are experiencing an error when attempting to create orders in the WordPress admin panel. The error message looks something like this:

Uncaught TypeError: array_key_exists(): Argument #2 ($array) must be of type array, null given in [...]/wp-content/plugins/woocommerce/includes/wc-core-functions.php:1331

This error occurs in the `wc_get_customer_default_location()` function and is caused by an interaction between WooCommerce and WCML.

Affected Versions

- WordPress: 6.6

- WooCommerce: 9.0.0+

- WCML: 5.3.6

- PHP: 8.2.21

However, this issue may affect other versions as well.

The Cause of the Fatal Error on Order Creation

The error stems from the way WCML handles currency and location data, specifically in a class called `ResolverForModeLocation`. The issue appears to occur when this class tries to determine the client's currency before WooCommerce is fully initialized or when some expected WooCommerce data is not available.

Understanding the Solution and Temporary Fix for Order Creation

While we can't provide the exact code to modify (as it would involve sharing proprietary code), we just got to wait for an official fix from the WCML development team, The temporary Fix would be updating the core files of the plugin and this isn't recommended action to do, I reported the bug and got to wait:

the file causing us bug is Located in your WordPress installation.

../wp-content/plugins/woocommerce-multilingual/classes/Multicurrency/Resolver/ResolverForModeLocation.php

Solution Logic

we can explain the logic that could potentially fix this issue:

  1. Check for WooCommerce Initialization: Before performing any operations, check if WooCommerce is fully loaded and initialized. This can prevent errors that occur when trying to access WooCommerce data that isn't yet available.
  2. Implement Error Handling: Wrap the main logic in a try-catch block. This can catch any unexpected exceptions and prevent them from causing the entire process to fail.
  3. Provide Fallback Mechanisms: If the expected data (like stored currency or user country) is not available, provide fallback options. For example, if you can't determine the user's country, you might assume the currency is valid rather than throwing an error.
  4. Add Logging: Implement error logging for situations where expected data is missing or exceptions occur. This can help with debugging and identifying the root cause of issues.
  5. Create a Default Currency Method: Have a method that returns a default currency when all else fails. This ensures that even if there are issues with determining the correct currency, the system can still function with a fallback option.

What This Fix Does

This modified version of the `ResolverForModeLocation` class adds several safeguards:

  1. It checks if WooCommerce is fully initialized before proceeding.
  2. It adds error logging to help with debugging.
  3. It implements a fallback mechanism to return a default currency if there are any issues.
  4. It wraps the main logic in a try-catch block to handle any unexpected exceptions.

Important Notes

  • This is a temporary fix. It will be overwritten when you update the WCML plugin.
  • Always back up your site before making any changes to core files.
  • This solution may not work for everyone, depending on your specific setup and other plugins.

Steps to Consider

  1. Backup Your Site: Always create a full backup of your website before attempting any changes.
  2. Check for Updates: Ensure that WordPress, WooCommerce, and WCML are all updated to their latest versions. Sometimes, issues are resolved in newer versions.
  3. Consult with Developers: If you're not comfortable with PHP development, it's best to consult with a experienced WordPress developer who can implement these logic changes safely.
  4. Report the Issue: Report this problem to the WCML support team. Provide them with your WordPress, WooCommerce, and WCML versions, along with steps to reproduce the issue.
  5. Monitor for Updates: Keep an eye out for updates to the WCML plugin, as a future version may include an official fix for this issue.

Long-Term Considerations

While implementing the logic described above might resolve the immediate issue, it's important to note that any changes to plugin files will be overwritten when the plugin updates. A more sustainable long-term solution might involve:

  1. Creating a custom plugin that hooks into WCML and modifies its behavior without changing core files.
  2. Using WordPress filters and actions, if available, to alter the behavior of the problematic functions.
  3. Working with the WCML development team to implement a permanent fix in the core plugin.

Encountering errors in complex e-commerce setups can be challenging, especially when they affect critical functionality like order creation. While we can't provide a direct code fix, understanding the underlying issues and the logic behind potential solutions can help you work towards resolving the problem.

Remember, it's always best to work with official support channels and experienced developers when dealing with issues in premium plugins. The WCML team is the best resource for an official, long-term solution to this problem.

Have you encountered this issue? How have you addressed it? Share your experience!

Gain free access to in-depth, step-by-step guides designed to help you master WordPress management. Join as a Learner Member—it’s quick, easy, and completely free!
Get Access Now
wpmadeasy logo new white
2025 © All Rights Reserved.