简体   繁体   English

在 WooCommerce 的单个产品页面上为低库存产品添加 CSS 类

[英]Add CSS class for low stock products on single product page in WooCommerce

I am not very familiar with php, and I am trying to add a class to 'Low stock' in the snippet below.我对 php 不是很熟悉,我试图在下面的代码片段中向“低库存”添加一个类。

What I am trying to achieve is making the Low Stock orange vis CSS in WooCommerce.我想要实现的是在 WooCommerce 中使低库存橙色可见 CSS。


function change_stock_text( $availability, $_product ) {

    if ( $_product->is_in_stock() ) {

        if ( $_product->get_stock_quantity() < 3 ) {

            $qty                          = $_product->get_stock_quantity();
            $availability['availability'] = __( "Low stock", 'woocommerce' );
        }
    }

    return $availability;
}
  • add_filter() is missing缺少add_filter()
  • $availability contains [availability] & [class] , you change the text, while you want to add a class $availability包含[availability] & [class] ,你改变文本,同时你想添加一个类
  • $qty is unused in your code $qty在您的代码中未使用
  • A class doesn't have to be translatable, so using __() is not necessary类不必是可翻译的,因此不需要使用__()

To add a class to the already existing class with the if condition, use the woocommerce_get_availability_class filter hook instead要将类添加到具有 if 条件的现有类,请改用woocommerce_get_availability_class过滤器钩子

// Availability class
function filter_woocommerce_get_availability_class( $class, $product ) {
    // In stock
    if ( $product->is_in_stock() ) {
        // Stock quantity less then 3
        if ( $product->get_stock_quantity() < 3 ) {
            $class .= ' low-stock';
        }
    }

    return $class;
}
add_filter( 'woocommerce_get_availability_class', 'filter_woocommerce_get_availability_class', 10, 2 );

it's been sometime since I worked with WooCommerce, so i am not sure if this will do the trick, but you can try:自从我使用 WooCommerce 以来已经有一段时间了,所以我不确定这是否会奏效,但您可以尝试:

$availability['availability'] = __( "<em class='low_stock'>Low stock</em>", 'woocommerce' );

This might work, otherwise there's likely a hook for it.这可能会奏效,否则可能会有一个钩子。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM