0

I am having some issues with the get_option method in Wordpress.

I'm running a WooCommerce website and for the currency symbol position is set to "Right with space". In the wp_options table the option_name "woocommerce_currency_pos" is correctly set to "right_space".

When viewing products on the normal website, all works well, the currency code is displayed on the right and the option value is coming back correct.

The issue however is that we use a little JS function to show the cart (mini_cart.php) in a lightbox and the code that is returned shows the currency code on the left.

After a bit of debugging I found the method get_woocommerce_price_format and the first line here is: $currency_pos = get_option( 'woocommerce_currency_pos' );

So this is just a default function to get the value of the stated option_name from the options table. This however returns "left" in this Ajax call.

I can't figure out how on earth, this is returning a different value than that in the database.

Obvious potential issues:

  • Ajax is calling the wrong domain/website - But this is not happening, it is in the same project, the same code. Changes to the code reflect fine (a die($currency_pos) returns the string)
  • It's pulling from another database - There is only 1 for the project and 1 set of config settings, so it's almost impossible that it's getting this data from elsewhere (I only have 1 project with WooCommerce).

What could this weird issue be?

p.s. The AJAX is called via /wp-admin/admin-ajax.php

This is the full function, it is not part of a class or anything

/**
 * Get the price format depending on the currency position
 *
 * @return string
 */
function get_woocommerce_price_format ()
{
    $currency_pos = get_option('woocommerce_currency_pos');

    switch ($currency_pos) {
    case 'left' :
        $format = '%1$s%2$s';
        break;
    case 'right' :
        $format = '%2$s%1$s';
        break;
    case 'left_space' :
        $format = '%1$s %2$s';
        break;
    case 'right_space' :
        $format = '%2$s %1$s';
        break;
    }

    //$format = '%2$s %1$s';

    return apply_filters('woocommerce_price_format', $format, $currency_pos);
}
  • Is the code sample you provided wrapped in any kind of conditional statement (such as if)? There may be a default value somewhere that is being set and the code above could be ignored - but that's just a guess without more code. – David Gard Jan 22 '15 at 11:50
  • Updated the question with the full function that is called. – Richard Jan 22 '15 at 12:12
  • Thanks. To confirm, you are adding die($currency_pos) directly below the line $currency_pos = get_option('woocommerce_currency_pos'); and it is returning left? – David Gard Jan 22 '15 at 12:29
  • Yes, that's correct. But I've solved the issue now. See my answer below. – Richard Jan 22 '15 at 12:34
  • Good work, thanks for posting your answer for others. – David Gard Jan 22 '15 at 12:58
1

Well, for anyone that might need this in the future.

Woocommerce overrides the get_option() method with this line: add_filter('option_woocommerce_currency_pos', array($this, 'filter_currency_position_option'));

Which then calls filter_currency_position_option

The first few lines of this method are:

function filter_currency_position_option($value){
    global $pagenow;

    if( ( is_ajax() || ( $pagenow == 'post-new.php' && isset( $_GET['post_type'] ) && $_GET['post_type'] == 'shop_order' ) ) && isset( $_COOKIE[ '_wcml_order_currency' ] ) ){
        $currency_code = $_COOKIE[ '_wcml_order_currency' ];

W-T-F

Okay, so it stores the currency position in a cookie for AJAX visits... I'm not sure if I can agree with this, but at least now I know.

Hope this will help someone else.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.