Support » Plugin: Currency per Product for WooCommerce » Question about currency setup

  • Resolved torbent

    (@torbentschechnegmailcom)


    Hello,
    thanks for this useful plugin! I installed the plugin locally and it works fine. However when I use the plugin I need to update all of my products, as I want not to I tried to create the functions needed for changing the currency on product level in my functions.php. However I am a bit puzzled, because of the following:

    First I commented all the add_filter statements in your core_class. After that the change of the currency was gone. That was okay, then I uncommented

    add_filter( 'woocommerce_currency', array( $this, 'change_currency_code' ), PHP_INT_MAX );

    And the correct currency for my product is displayed. For the product I am looking at I set it up as “USD”, the usual store currency is CAD.

    Why I am puzzled is because of this, I checked the function that is tied to the hook:

    function change_currency_code( $currency ) {
    		if ( false != ( $_currency = $this->get_current_product_id_and_currency() ) ) {
    			return $_currency;
    		} elseif ( $this->is_cart_or_checkout() ) {
    			return ( false != ( $_currency = $this->get_cart_checkout_currency() ) ) ? $_currency : $currency;
    		}
    		var_dump($currency);
    		die();
    		return  $currency;
    	}

    $currency, was “CAD”, but the product is displayed in USD, which is correct.

    When I do copy the necassary functions from your plugin to my functions.php, the result is the same – the filter does return “CAD”. However my code in my functions.php has no effect. So the plugin changes the language to USD, but my code not.

    Am I missing something that changes the currency? Within the function which is tied to the hook does not happen that much…

    $this->get_current_product_id_and_currency() this does return false and I am on a single product page…

    Even when I change my code to this, the price is displayed only as ‘3.409’ – so without a dollar sign. Once I uncomment your hook, it is displayed with the dollar sign:

    function filter_woocommerce_currency( $currency ) { 
    	if ( false != ( $_currency = get_current_product_id_and_currency() ) ) {
    			return $_currency;
    	}
    	$currency = 'USD';
    	return $currency;
    };       
    add_filter( 'woocommerce_currency', 'filter_woocommerce_currency', PHP_INT_MAX, 1); 

    It would be great if you can point me in the right direction…

    Thanks!

    • This topic was modified 2 years ago by  torbent.
Viewing 4 replies - 1 through 4 (of 4 total)
  • alvinkonda

    (@alvinkonda)

    Algoritmika Ltd

    (@algoritmika)

    Hi @torbentschechnegmailcom,

    Sorry for a delayed reply. I would like to help, but I’m not sure I understood the task correctly. If you want to change product currency via PHP, you need to set currency in product’s _alg_wc_cpp_currency meta, something like this:

    update_post_meta( $product_id, '_alg_wc_cpp_currency', 'USD' );

    If you don’t set the meta, get_current_product_id_and_currency() will return false as there is no currency set for the product (this is unless you use setting currency for the whole products category, tag etc.).

    Hope I made it little more clear, and please let me know if you have any more questions.

    Best regards,
    Tom

    torbent

    (@torbentschechnegmailcom)

    Hey Tom,
    thanks for getting back to me. I solved it partly. The reason at my side was, that I did not return “USD”, I returned usd. So the missing uppercase made the point.

    However I have the strange behaviour that I have different results when I am logged in and when I am not logged in. So in the cart I still have the product in CAD, but only when I am not logged in as a customer.

    How is that possible? I debugged this filter woocommerce_get_cart_item_from_session and item[‘data’] has the currency USD.

    Do you have any idea why there is a different behaviour on the cart page depended on the state of the user (logged in, logged out)?

    torbent

    (@torbentschechnegmailcom)

    I solved it, thanks for your support!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Question about currency setup’ is closed to new replies.