dongzhui9936 2019-05-19 13:28
浏览 132
已采纳

在WooCommerce中显示图标而不是文本库存可用性

I am trying to show icons instead of text as the availability in WooCommerce. The code I have shows text, but I like it to be icons instead.

I have three icons: Red, Orange and Green for Out of stock, Half stock left and fully stocked.

Here's the code I need help changing:

add_filter( 'woocommerce_get_availability', 'dispay_custom_icons_for_availability', 1, 2);
function dispay_custom_icons_for_availability( $availability, $product ) {
   global $product;

   // available
    if ( $product->is_in_stock() ) {
        $availability['availability'] = __('GREEN ICON HERE', 'woocommerce');
    }

    // middle stock
    if ( $product->is_in_stock() && $product->get_stock_quantity() <= 20 ) {
        $availability['availability'] = sprintf( __('ORANGE ICON HERE', 'woocommerce'), $product->get_stock_quantity());
 }


   // out of stock
    if ( ! $product->is_in_stock() ) {
        $availability['availability'] = __('RED ICON HERE', 'woocommerce');
    }

    return $availability;
}

All help is appreciated.

  • 写回答

1条回答 默认 最新

  • douyi1963 2019-05-19 14:30
    关注

    Try the following, based on Fontawesome icons that are embedded in WooCommerce:

    add_filter( 'woocommerce_get_availability', 'dispay_custom_icons_for_availability', 1, 2);
    function dispay_custom_icons_for_availability( $availability, $product ) {
       global $product;
    
       // available
        if ( $product->is_in_stock() ) {
            $availability['availability'] = '<i class="fa fa-lg fa-smile" style="color:green;"></i>';
            $availability['class'] = 'in_stock';
        }
    
        // middle stock
        if ( $product->is_in_stock() && $product->get_stock_quantity() <= 20 ) {
            $availability['availability'] = '<i class="fa fa-lg fa-meh" style="color:orange;"></i>';
            $availability['class'] = 'low_stock';
     }
    
    
       // out of stock
        if ( ! $product->is_in_stock() ) {
            $availability['availability'] = '<i class="fa fa-lg fa-frown" style="color:red;"></i>';
            $availability['class'] = 'out_of_stock';
        }
    
        return $availability;
    }
    

    Code goes in functions.php file of your active child theme (or active theme). Tested and work.

    You will get one of the following icons enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行