douwen3127 2018-04-23 15:17
浏览 115
已采纳

在Woocommerce产品中为库存通知添加额外的CSS类

I need an extra class for the stock notice on the WooCommerce product page. At the moment, there are two different classes in-stock and out-of-stock. But there is a third option in WooCommerce. If there a only few items in stock, the class would also be in-stock. It would be nice to have something like few-in-stock.

I checked the template file stock.php which generates the output. There is only one line of code:

<p class="stock <?php echo esc_attr( $class ); ?>"><?php echo wp_kses_post( $availability ); ?></p>

And the $class has only the two options from above. Is there any function I could use to add a third class?

At the moment I count the stock items of a product like this:

$stock_count = $product->get_stock_quantity();

And add my new class like this:

<p class="stock <?php echo esc_attr( $class ); ?> <?php if ( $stock_count <= '2' ) : ?>few-in-stock<?php endif; ?>"><?php echo wp_kses_post( $availability ); ?></p>

But maybe there is a better way to do that?!

  • 写回答

2条回答 默认 最新

  • dongyou9818 2018-04-23 15:51
    关注

    Here is the function hooked in woocommerce_get_stock_html filter hook, that will allow you to add an extra stock notice for low stock (with custom CSS):

    add_filter( 'woocommerce_get_stock_html', 'filter_get_stock_html', 10, 2 );
    function filter_get_stock_html( $html, $product ) {
        // Low stock quantity amount
        $low_stock_qty = 3;
    
        $availability = $product->get_availability();
    
        if ( ! empty( $availability['availability'] ) ) {
            $class = esc_attr( $availability['class'] );
            $avail_text = wp_kses_post( $availability['availability'] );
            $stock_qty = $product->get_stock_quantity();
    
            if( $stock_qty <= $low_stock_qty ){
                $class .= ' few-in-stock';
                $avail_text = __('Few in stock', 'woocommerce');
            }
            ob_start();
    
            // Make your changes below
            ?>
            <p class="stock <?php echo $class; ?>"><?php echo $avail_text; ?></p>
            <?php
    
            $html = ob_get_clean();
        }
        return $html;
    }
    

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

    Get optionally the low stock amount from general product inventory settings

    If you have set "low stock threshold" in Settings > Product > Inventory, you can get it and replace the manual inputed value, replacing the following line:

    // Low stock quantity amount
     $low_stock_qty = 3;
    

    By this:

    // Low stock quantity amount
     $low_stock_qty = get_option('woocommerce_notify_low_stock_amount');
    

    (Thanks to @Demian)

    enter image description here

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

报告相同问题?

悬赏问题

  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解