umpirsky
umpirsky

Reputation: 10024

Proper way of adding some information about product price

I am writing a simple WooComerce plugin. This is the first time I work with WordPress so my question may be quite noobish.

The plugin should just provide some additional information about product price, which is approximate price in local currency (based on visitors location).

enter image description here

Pseudo dummy code:

add_action(
    'woocommerce_get_price_html', 
    function append_local_price($content, $product) {
        return $content . ' <span class="amount-local-price">Approx. (RSD 3,804)</span>';
    }, 
    10, 
    2
);

Dummy implementation, formatting and other issue aside, since that is not my focus in this question, what is the proper way of hooking into WooCommerce workflow in this case?

Because what I see as a pros of this approach is that you just install plugin and it works, zero configuration. But downsides are html rendering is not easily configurable (css class etc...) and you cannot control when it is applied.

Is some kind of short code more appropriate in this case?

This will require editing the theme in order to get local price shown, right?

Upvotes: 1

Views: 208

Answers (2)

helgatheviking
helgatheviking

Reputation: 26329

Other than not using an anonymous function (a named function can be removed), I don't see a problem with your current approach. You can use a very minimal stylesheet or even non at all and leave styles to the themes. As far as html content, you can run yours through a filter so that people can modify it if desired. Don't forget to make your strings translation-ready.

add_filter( 'woocommerce_get_price_html', 'so_31702808_local_price' );
function so_31702808_local_price( $content, $product) {
        $local_price = "RSD 3,804"; // retrieved from your API
        $local_price_html = apply_filters( 'so_31702808_local_price_html', ' <span class="amount-local-price">'. $sprintf( __e( 'Approx. (%s)', 'your-text-domain'), $local_price ) .'</span>', $local_price, $product );
        return $content . $local_price_html;
    }, 

);

If the local price does not come back formatted locally, then you can use wc_price() with its args to format it as needed.

Upvotes: 2

dingo_d
dingo_d

Reputation: 11680

Well you can always enqueue your own css that will point to whatever content you added with hooks.

Hooks in woocommerce can be frustrating, trying to figure out what goes where, but as you've said, the upside is that you can just add what you need where you need it. And you can append element with a specific class in it that you can target with your css and style appropriately.

About controlling it when it's applied I think that it's up to woocommerce to determine that, so you don't really have a choice. Not sure how you'd go about using a shortcode. I mean you can add it via add_action by hooking somewhere, but I'm not sure what you'd get by it, seeing that you can just do the same thing by hooking into the desired place.

As for theme editing for local price I don't think you need to do this. Woocommerce has two functions get_woocommerce_currency() and get_woocommerce_currency_symbol() that should help you determine the currency set by the user on his site.

The 'hard' part is configuring all the currency exchange, because the relations between them change. So you'll need some kind of API that tracks this and returns the relation for you, that you can later on apply to your plugin.

This is more like a long comment than a real answer tbh...

Upvotes: 0

Related Questions