duandingyou3331 2015-07-29 14:03
浏览 21
已采纳

添加一些有关产品价格的信息的正确方法

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?

  • 写回答

2条回答 默认 最新

  • douji2520 2015-07-29 19:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮