The code in this article is considered a customisation. As such, it's outside the scope of our support service. Should you need assistance implementing the code, please feel free to contact us to request a consultation.


When the feed generated for entities such as Facebook, or Google Merchants, has the wrong currency, or changes currency without apparent reason, the most likely cause of the issue is that the plugin that generates the feed is taking the prices in the currency active at the time when the feed is prepared. The active currency may change depending on the context in which the process runs, thus causing the feed to take unexpected prices.

One of the most conditions that can cause this issue is the generation of a feed "on the fly". A remote server connects to the WooCommerce site, to request the feed, and the currency is set depending on the IP address of the request. Most feed services use bots that connect from the US, and they receive a feed in USD, even though they might expect it in a different currency.


1. How to generate a feed in the correct currency
There are many different plugins that generate a feed, and the solution depends on the specific one being used. Below we describe a couple of solutions that should cover the most common scenarios.

1.1. Set the feed currency by using a special URL
Product feeds are generated by a plugin. If the plugin produces the feed "on the fly" by calling a special URL (e.g. https://example.org?generate_feed=1), one solution could be to enable the "currency by URL" feature in Currency Switcher > Currency Selection (see https://prnt.sc/gpd3vj), then pass the currency via the URL, as https://example.org?generate_feed=1&aelia_cs_currency=GBP. That would change the active currency on the fly, and it should result in a feed generated in that currency.

1.2. Set the feed currency using a filter when the feed URL is called

If it's not possible to add a "currency" argument to the URL called by the remote services, an alternative would be to set the active currency on the fly, with a filter (see Aelia Currency Switcher - How to change the selected currency via code, example #3), after checking if the URL is the one used to fetch the feed. 


Here's an example of this logic, showing how to set the currency to GBP:
add_filter('wc_aelia_cs_selected_currency', function($selected_currency) {
    // If current page URL contains a specific "generate feed" argument,
    // force the currency to GBP.
    // In this example <SOME ARGUMENT> and <SOME VALUE> would have to be 
    // replaced by the actual name and value of the argument passed by 
    // Facebook, or Google Merchants, when calling the site to fetch the feed.
    if(isset($_GET['<SOME ARGUMENT>']) && ($_GET['<SOME ARGUMENT>'] === '<SOME VALUE>')) {
     // Force selected currency to GBP
     $selected_currency = 'GBP';
    }

    return $selected_currency;
});

Important

To use the code, you will have to replace <SOME ARGUMENT> and <SOME VALUE> with the actual values used by the feed plugin, and that should be sufficient to return a feed always in GBP (or any other currency of your choice).


1.2.1. Force the active currency for bot visitors

If the remote service (bot) fetching the feed data uses a specific agent ID, it could also be possible to force the active currency for the requests sent with such agent ID. To do so, one could use example #5 from the following article: Aelia Currency Switcher - How to change the selected currency via code. In the code snippet, adding the agent ID to the $bot_types variable would allow to identify the calls from the Facebook bot and set the active currency in that context.


1.3. Set the feed currency for a feed generated by a scheduled event
If the feed plugin generates the feed on a schedule, or a specific event, then it might not be possible to use a special URL, or check for a specific URL argument. Instead, the approach would have to be the following:
  1. Using the actions and filters provided by the feed plugin, intercept the generation of the feed. The authors of the feed plugin should be able to tell what event should be used.
  2. Use some custom code to change the active currency to GBP on the fly, as described in the article mentioned earlier (How to change the selected currency via code, example #3), just before the feed data is generated.


Here's an example of how this logic. It shows how to set the currency to GBP just before the feed generation starts, and removes the override after the feed has been generated:
/**
 * Sets the active currency to a specific one
 *
 * @param string $selected_currency
 * @return string
 */
function aelia_set_feed_currency($selected_currency) {
  // Set the currency to GBP
  return 'GBP';
}

/**
 * Overrides the active currency before the generation of a feed.
 */
add_action('<BEFORE FEED GENERATION STARTS>', function() {
  add_filter('wc_aelia_cs_selected_currency', 'aelia_set_feed_currency', 99);
});

/**
 * Removes the currency overrides after the generation of a feed.
 */
add_action('<FEED GENERATION COMPLETE>', function() {
  remove_filter('wc_aelia_cs_selected_currency', 'aelia_set_feed_currency', 99);
});

Important

The key aspect, in this second case, is to find out which events are triggered by the feed plugin, so that one can add the filter to set the currency on the fly. We recommend to contact the authors of the specific plugin in use, they should be able to tell you which actions are available.

1.4. Use a feed plugin that supports multi-currency configurations
If adding some custom code to set the feed currency is not possible, an alternative solution would be to use a plugin that can generate a currency-specific feed. One of such plugins is called Product Feed PRO, developed by AdTribes.io.


2. Contact us if you have any questions

As indicated at the top of the article, all the code examples are provided on an "as is" basis, and they fall outside the scope of our support service. Although we won't be able to implement them for you, nor provide a guarantee that they will work, we will be happy to answer any questions you might have. You can simply contact us, and we will get back to you as soon as possible.


You can purchase the Currency Switcher from our online shop.