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条)

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行