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

在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?!

图片转代码服务由CSDN问答提供 功能建议

我需要在WooCommerce产品页面上添加额外的股票通知类。 目前,有两种不同的 类库存缺货。 但是WooCommerce还有第三个选项。 如果库存中只有少量商品,那么该类也将是 in-stock 。 有一些像 stock-in-stock 这样的东西会很好。

我检查了模板文件 stock.php ,它生成了 输出。 只有一行代码:

 &lt; p class =“stock&lt;?php echo esc_attr($ class);?&gt;”&gt;&lt;?php  echo wp_kses_post($ availability);  ?&gt;&lt; / p&gt; 
   
 
 

$ class 只有上面的两个选项。 我可以使用任何函数来添加第三个类吗?

目前我计算这样一个产品的库存项目:

 < 代码> $ stock_count = $ product-&gt; get_stock_quantity(); 
   
 
 

并添加我的新类,如下所示:

  &lt; p class =“stock&lt;?php echo esc_attr($ class);?&gt;&lt;?php if($ stock_count&lt; ='2'):?&gt;库存少&lt;?  php endif;?&gt;“&gt;&lt;?php echo wp_kses_post($ availability);  ?&gt;&lt; / p&gt; 
   
 
 

但也许有更好的方法可以做到这一点?!

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

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

相关推荐 更多相似问题