Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can't get currency updates for WooCommerce/WooCommerce MultiLingual #2

Closed
jkomar opened this issue Feb 5, 2015 · 34 comments
Closed

Comments

@jkomar
Copy link

jkomar commented Feb 5, 2015

I have the current version of WooCommerce with WooCommerce MultiLingual on WordPress 4.1. I set up Canadian and US dollar currencies in WooCommerce MultiLingual with Canadian dollars being my store's base currency. I got a forever free API key from openexchangerates.org for testing before I invest in a higher level plan.

When I try to trigger an update in WP Currencies by changing the update interval and clicking Save Changes, my WooCommerce MultiLingual settings automatically switch back to 'No multi-currency.' When I turn multi-currency back on, the US dollar exchange that I had manually set has been cleared and there is no exchange rate shown there.

I have checked my database with phpMyAdmin and the wp_currencies table has not even been created.

Thanks for any help you can give,
Jason

@jkomar
Copy link
Author

jkomar commented Feb 5, 2015

Just to add another note, I logged into my openexchangerates.org dashboard to check if there were any API requests and there were 25. It appears that requests are being made.

Thanks,
Jason

@unfulvio
Copy link
Owner

unfulvio commented Feb 6, 2015

Hi, maybe it was a issue with the activation hook. Can you try uninstalling / deactivating the plugin, download the latest version from the most recent commit I just made, overwrite the plugin files, reactivate and see if it works? thank you!

@jkomar
Copy link
Author

jkomar commented Feb 6, 2015

I will give it a try and report back.

Thank you,
Jason

@jkomar
Copy link
Author

jkomar commented Feb 6, 2015

I deactivated and uninstalled the plugin. I re-installed from master (v.1.2.1). It is exhibiting the same behavior (except for the link from the other issue I reported which is fixed). I also tried setting my base currency to USD in case the plugin relies on some of the advanced features not available in the free account at openexchange.org.

Thanks,
Jason

@jkomar
Copy link
Author

jkomar commented Feb 6, 2015

I didn't mention my version of WooCommerce Multilingual before. It is Version 3.5.1.

Thanks,
Jason

@unfulvio
Copy link
Owner

unfulvio commented Feb 6, 2015

Hi, I did some change and tested again on a clean install on my machine... the currencies are fetched correctly. Can you try again deactivating, updating the plugin files and reactivating please? Then, before going to WCML, can you check if your currencies were updated in the database? so we make sure first that the currency exchange rates are there

@jkomar
Copy link
Author

jkomar commented Feb 6, 2015

I will give it a try and report back.

Thanks,
Jason

@jkomar
Copy link
Author

jkomar commented Feb 6, 2015

I installed and activated version 1.2.2 and it created the currencies table in the database. At first the table was empty. I switched the update interval and clicked 'Save Changes' and the table was populated. Once I did this, my WooCommerce Multilingual multi-currency setting was turned off again. I turned it back on and there was no exchange rate set. I visited a product page and the exchange was showing as 1 = 1. I went back to the currencies plugin page again, changed the update interval and clicked 'Save Changes'. Again the WooCommerce Multilingual multi-currency setting was turned off. I turned it back on and it showed the exchange rate from openexchangerates.org. It took a bit of playing around to get it going, but it seems to be working. I have set my update interval for hourly. I will keep checking to see if the rates are updating and if WooCommerce Multilingual is using the updated rates.

I have tried to give you as much information as I can. Let me know if there is anything else I can help you with. I am happy to keep helping with testing if you would like or to provide any information and feedback that I can.

Thanks for your help,
Jason

@unfulvio
Copy link
Owner

unfulvio commented Feb 6, 2015

Hi Jason, thanks for writing back.

WCML support is a bit experimental. What WCML does natively is to store fixed change rates in a WordPress option (http://codex.wordpress.org/Options_API), which contains different data besides the exchange rates.

What WP Currencies does is to update this option (by overwriting the relevant keys in the array) at the same intervals as the rates are updated in the database. If WCML in a future update changes the way it stores the currencies exchange rates, things might break.

Probably WP Currencies can improve on how it updates the currency rates and catches any error/exception if something goes wrong as in your case with WCML - right now there's no option either to activate individual components of the plugin (such as WCML support).

I did speak with WCML people back then and they told me in future they want to change the way WCML handles exchange rates to allow dynamic updating but they didn't give a roadmap.

WP Currencies schedules currency exchange rates updates as a wp cron. To check the status of WordPress cron jobs you can use this plugin https://wordpress.org/plugins/wp-crontrol/

Thank you for your feedback, you can always report more errors/quirks or submit pull requests if you want to improve the plugin. I will leave this issue open since others may want to read about WCML support.

Cheers

@jkomar
Copy link
Author

jkomar commented Feb 6, 2015

I installed the cron plugin and the crons are running. I verified that I am receiving updated rates in the wp_currencies table in the database. It would seem that they are not transferring properly to WCML though. To get the rate to transfer to WCML, I had to change the update interval in wp-currencies which disabled multi-currency in WCML. When I re-enabled multi-currency in WCML, the new rate was there.

As an update, after 2 days, the updates are still being done correctly to the wp_currencies table, but WCML is not using the updated rates.

Thanks,
Jason

@unfulvio
Copy link
Owner

unfulvio commented Feb 9, 2015

thanks for noticing, I'll be looking into it today - most likely sounds as I missed to call the option update function when the cron job is triggered, if it's that, should be easy to fix

@unfulvio
Copy link
Owner

unfulvio commented Feb 9, 2015

ok on my install seems to work... can you try if it with hourly/daily updates the WCML exchange rates sync correctly? thank you

@jkomar
Copy link
Author

jkomar commented Feb 11, 2015

I had it set for hourly. I switched it to daily and when I toggled it, it updated WCML immediately. Over the next couple of days however the wp_currencies table was updated daily, but WCML didn't pick up the new rates.

I just switched it back to hourly and WCML immediately updated again, but as in past behaviours, I expect WCML will be "stuck" on that rate.

Thanks,
Jason

@unfulvio
Copy link
Owner

Hi

I've done some test and noticed other quirks with WCML. I'm still running 3.4.x because of wp database errors i got from 3.5 (appparently solved in 3.5.1 but I need to check). Then for example if one doesn't have extra languages set... the currency switcher magically disappears or the multicurrency option sets itself to off. I'm going to let WCML devs have a look in one of my setups. But it looks like the issue might not be solved fast. I need a multicurrency feature in WooCommerce with dynamic rates so I'll make it work.

@HvTMedia
Copy link

Just installed the plugin and experience the same problem as the original poster, i.e. the no-multi currency setting. The exchange rates seem to update and if I deactivate the plugin I can switch back to multi-currency and the switcher and currencies show up on the product page but as soon as I reactivate the no-multi currency setting comes back. Are there any specific settings that has to be done or actions to be taken to have the multi-currncy setting and the plug-in to interact (haven't changed anything, just installed and activated)?

Regards

Hans

@unfulvio
Copy link
Owner

@HvTMedia thanks for confirming the issue - I'm a bit busy right now to follow up with WCML.

Unfortunately their product is (was?) advertised as beta when I started adding WCML support to WP Currencies. On their (WPML / WCML) forums they mentioned they were going to change things.

They didn't provide a hook to filter currency data on WCML end, so what WP Currency does is updating a options meta field job which is assumed to be a serialized array with key-value pairs organized in a certain way. WCML uses WordPress Settings API to store its data. If they are not consistent in how this is data is organized the integration will fail.

I might have to remove WCML support because or this - or if anyone wants to contribute to this plugin can issue a pull request.

WP Currency doesn't have an administration settings panel for extra features, which might be useful to users who want to deactivate things like ACF support or WCML integration. So currently you can only set an update frequency and enter your openexchangerates.org API key. But again if someone wants to contribute to this project I'd be very happy to catch up with its development.

@HvTMedia
Copy link

Thanks for your quick reply. I really like your plugin as it does what I expect and want, save for the integration with WooCoommerce. It would be a pity if you removed support and I can’t understand why WCML don’t take an interest in solving the issue as a dynamic currency converter at is, if not a "must have” at least a very usefull feature. Please enlighten me what you mean by “issue a pull request” as I would hate to see the WCML support be removed.

Regards

//Hans

@unfulvio
Copy link
Owner

Well, it appears that WCML support is broken so if it's removed at least one can use WP Currencies and WCML without breaking WCML and nothing really will change.

Nothing wrong on WPML side, they "don't have to" support WP Currencies... mine was more a hack than anything - but perhaps they will introduce a hook to filter the data more consistently - I was also told they had in mind features for dynamic multicurrency.

A "pull request" is part of the workflow on how you contribute to collaborative projects with a version control system like git (which github is based on) - you clone someone else projects, make changes locally and if you add an interesting feature or a fix to a bug you can submit your changes to where the project is stored. It's not too hard to learn and it will bring benefits to your work if you are a developer, even when working alone.

@extrapixel
Copy link

So, please change the description on the official plugin page. I just wasted hours because you claim it's working with wpml/woocommerce - while it is actually a "hack" that does not work with newest version of wp/wc/wpml. Don't advertise a non-working hack as a core feature, man!

@unfulvio
Copy link
Owner

I'm offering this plugin and my work for free and you could also submit a pull request to fix it and contribute to it - if WPML changed the way their options are stored it's not my fault and neither theirs. I'm going to close this ticket and I will update the plugin this week or the other by removing that feature (I was postponing it because I wanted also to do some rewrite and improve the features, but since the faulty parts get more attention than the good bits I guess the latter will have to wait).

@extrapixel
Copy link

sorry man, didn't want to offend. actually, this part gets most attention, because it's the most wanted feature :)

Ok i think I found the problem. check this out. Following is the options array before activating WP-Currencies.

Array
(
    [currency_options] => Array
        (
            [CHF] => Array
                (
                    [languages] => Array
                        (
                            [en] => 1
                            [de] => 1
                        )

                    [rate] => 0.92753
                    [position] => left_space
                    [thousand_sep] => ,
                    [decimal_sep] => .
                    [num_decimals] => 2
                    [rounding] => disabled
                    [rounding_increment] => 1
                    [auto_subtract] => 0
                )

            [EUR] => Array
                (
                    [languages] => Array
                        (
                            [en] => 1
                            [de] => 1
                        )

                    [rate] => 0.888376
                    [updated] => 2015-04-01 20:05:00
                    [position] => left
                    [thousand_sep] => .
                    [decimal_sep] => ,
                    [num_decimals] => 2
                    [rounding] => up
                    [rounding_increment] => 1
                    [auto_subtract] => 0
                )

            [USD] => Array
                (
                    [languages] => Array
                        (
                            [en] => 1
                            [de] => 1
                        )

                    [rate] => 1
                    [updated] => 2015-04-01 20:05:26
                    [position] => left_space
                    [thousand_sep] => ,
                    [decimal_sep] => .
                    [num_decimals] => 2
                    [rounding] => up
                    [rounding_increment] => 1
                    [auto_subtract] => 0
                )

        )

    [file_path_sync] => 1
    [is_term_order_synced] => yes
    [enable_multi_currency] => 2
    [dismiss_doc_main] => 0
    [trnsl_interface] => 1
    [currency_switcher_product_visibility] => 1
    [wc_admin_options_saved] => 1
    [products_sync_date] => 1
    [products_sync_order] => 1
    [display_custom_prices] => 0
    [set_up] => 1
    [downloaded_translations_for_wc] => 1
    [multi_currency] => Array
        (
            [set_up] => 1
        )

    [default_currencies] => Array
        (
            [en] => 
            [de] => 
        )

    [untranstaled_terms] => Array
        (
            [product_shipping_class] => Array
                (
                    [count] => 0
                    [status] => 0
                )

            [product_cat] => Array
                (
                    [count] => 0
                    [status] => 0
                )

            [product_tag] => Array
                (
                    [count] => 0
                    [status] => 0
                )

        )

)

And this is the array after activating WP-Currenciest:

Array
(
    [currency_options] => Array
        (
            [CHF] => Array
                (
                    [languages] => Array
                        (
                            [en] => 1
                            [de] => 1
                        )

                    [rate] => 0.92753
                    [position] => left_space
                    [thousand_sep] => ,
                    [decimal_sep] => .
                    [num_decimals] => 2
                    [rounding] => disabled
                    [rounding_increment] => 1
                    [auto_subtract] => 0
                )

            [EUR] => Array
                (
                    [languages] => Array
                        (
                            [en] => 1
                            [de] => 1
                        )

                    [rate] => 0.888376
                    [updated] => 2015-04-01 20:05:00
                    [position] => left
                    [thousand_sep] => .
                    [decimal_sep] => ,
                    [num_decimals] => 2
                    [rounding] => up
                    [rounding_increment] => 1
                    [auto_subtract] => 0
                )

            [USD] => Array
                (
                    [languages] => Array
                        (
                            [en] => 1
                            [de] => 1
                        )

                    [rate] => 1
                    [updated] => 2015-04-01 20:05:26
                    [position] => left_space
                    [thousand_sep] => ,
                    [decimal_sep] => .
                    [num_decimals] => 2
                    [rounding] => up
                    [rounding_increment] => 1
                    [auto_subtract] => 0
                )

        )

)

So, basically currently all other options in the array except "currency_options" are truncated. Should be an easy fix.
Oh and... sorry again. I know you're doing this for free and believe me, lots of people appreciate your work!

@unfulvio
Copy link
Owner

ok Patrick, I've checked the current status for WCML

last time it was WCML that suggested updating periodically their option - WCML was storing the currency rates by hardcoding them in their options. You had to enter manually the conversion rates. They told me a way to automate the rate updates was to overwrite this option. Without a hook or a cron job the only way to achieve that was to overwrite WCML option using WordPress settings API at periodical intervals. Which is what WP Currencies did. I think WCML in newer versions changed their option group (it's essentially an array), WP Currencies can't find the same array key and fails.

I've made a quick search and they seem finally to offer some hooks: https://wpml.org/documentation/related-projects/woocommerce-multilingual/multi-currency-support-woocommerce/ - with these hooks should be easy to take the values from WP Currencies and filter the rates of WCML, without the need of a support of WP Currencies for WCML. I will make now an update to remove the feature from WP Currencies and mention that document for people to use the two plugins together. Since both WP Currencies and WCML use the same 3-letters ISO codes for currencies should be easy. Give me 2 min. The update on this repo should be faster on github than WordPress since I changed system recently and I have to set up svn again.

@extrapixel
Copy link

ok... actually i really like the feature. and it was an easy fix. Problem was, as i said, your code didn't take into account that there is more entries in that option after "currency_options". My fix just re-uses the $wcml array instead of creating a new one (was $updated). And then we only replace "currency_options" in the old array - voilà.

created a pull request if you want to have the feature... or direct people to my fork if they want this feature.

@extrapixel
Copy link

WP Currencies can't find the same array key and fails.

actually, no, that was not the problem. it deleted all other entries in that options instead of overwriting "currency_options" only.

@unfulvio
Copy link
Owner

I think it's best to use hooks - updating each other plugin option was not really a good way from the beginning and I placed it there because there was no other way. If WCML changes something again, it will be broken again. That's why WordPress has hooks, you don't mess with settings in the database. Now they have hooks, people can use those more efficiently.

@extrapixel
Copy link

If WCML changes something again, it will be broken again.

well... probably. but then again, the fix is pretty future-proof ;)

I think it's best to use hooks

True. Do you want to implement this in the plugin itself? Well, because - it really is THE killer feature of your plugin, compared to all the other currency plugins. just my 2 cents.

@unfulvio
Copy link
Owner

Ok I updated the repo on github, it's not on the official directory on .org yet

can you do a favor and test if it works and doesn't break WCML?

then, try the following - in your theme functions.php add this filter:

 add_filter( 'wcml_exchange_rates', 'my_wp_currencies_filter');

 function my_wp_currencies_filter( $rates_array ) {
     var_dump( $rates_array );
     $wp_curr_rates = get_exchange_rates();
     var_dump( $wp_curr_rates);
     return $rates_array;
 }

then call the function in WCML where this hook is used - I think it's the currency switcher

do a comparison between the output of the first dump with the array outputted by get_exchange_rates - if you replace the relevant values in $rates_array with the keys from $wp_curr_rates you will have updated rates on the fly. I suppose a foreach loop should do.

thanks

@unfulvio
Copy link
Owner

if it works then I suppose it's safe to introduce the filter internally in WP Currencies, HOPING that WCML doesn't change the array again

@extrapixel
Copy link

great! I'll test it in an hour or two.

@unfulvio
Copy link
Owner

if WCML was broken before this, you might want to reset its option or deactivate/reactivate it before reinstalling and reactivating WP Currencies

@extrapixel
Copy link

i did:

  • uninstalled my "fixed" version
  • made sure multi-currency is working fine
  • set all rates to 1:1 manually
  • installed your new version
  • added your code to my functions.php

...but can't get the output of the var_dump()s.... should it print to the page source? sorry my php is a little rusty. but it seems that filter-function is never triggered.

@extrapixel
Copy link

wait - the frontend switcher doesn't trigger it, but the wcml backend does.

@extrapixel
Copy link

sorry, can't get to output anything from the filter function, i think it's not triggered. As I'm really busy with my own stuff, I'll just use my hack-fixed fork as it works like a charm (for the moment). I do have a test installation however which I could give you access to.

@extrapixel
Copy link

Just in case someone stumbles upon this... The WCML filter (namely "wcml_exchange_rates") does not work. It never fires. See https://wpml.org/forums/topic/multicurrency-filter-wcml_exchange_rates-not-working/#post-645334

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants