简体   繁体   English

在Woocommerce存档页面中将“销售”徽章替换为“缺货”

[英]Replace “Sale” badge by “Out of Stock” in Woocommerce archive pages

I am working on woocommerce shop and here is the link of the website shop page 我正在开发woocommerce商店,这里是网站商店页面的链接

On this page the first product is out of stock . 在此页面上,第一个产品out of stock I would like to show Out of stock instead of "Sale!" 我想展示Out of stock而不是“促销!” badge on the image. 图像上的徽章。

How can I do this? 我怎样才能做到这一点?

Add in the functions.php file of your theme: 添加主题的functions.php文件:

add_filter('woocommerce_sale_flash', 'woocommerce_custom_sale_text', 10, 3);
function woocommerce_custom_sale_text($text, $post, $_product)
{
    return '<span class="onsale">out of stock</span>';
}

If you look at the loop/sale-flash.php template you can see it has a filter for the sale flash. 如果你看一下loop / sale-flash.php模板,你可以看到它有一个销售闪存的过滤器。 You can add this to your functions.php file to modify that output. 您可以将其添加到functions.php文件中以修改该输出。

add_filter( 'woocommerce_sale_flash', 'sale_flash_stock_status' );
function sale_flash_stock_status( $output_html, $post, $product ){
    if( $product->is_in_stock() ){
        // Leave the sale flash unchanged if it's in stock.
        return $output_html;
    }
    else {
        // Change the html output custom stock status
        $output_html = '<span class="stock-status">' . esc_html__( 'Out of stock', 'woocommerce' ) . '</span>'
        return $output_html;
    }
}

The following code will add, on Woocommerce archives pages (as shop), a "Out of Stock" badge for out of stock products replacing the "Sale!" 以下代码将在Woocommerce存档页面(作为商店)上添加“Out of Stock”徽章,用于替换“Sale!”的缺货产品。 badge when products are on sale: 产品发售时的徽章:

// Add badge  "Out of stock" (and replace sale badge)
add_action('woocommerce_before_shop_loop_item_title','custom_before_shop_loop_item_title', 2 ); // Archives pages
function custom_before_shop_loop_item_title(){
    remove_action('woocommerce_before_shop_loop_item_title','woocommerce_show_product_loop_sale_flash', 10 );
    remove_action('woocommerce_after_shop_loop_item_title','woocommerce_show_product_loop_sale_flash', 6 ); // For storefront theme
    add_action('woocommerce_before_shop_loop_item_title','show_product_loop_outofstock_badge', 10 );
}

function show_product_loop_outofstock_badge(){
    global $post, $product;

    if ( $product->get_stock_status() == 'outofstock' ) :
        echo '<span class="onsale outofstock">'. esc_html__('Out of stock', 'woocommerce') .'</span>';
    elseif ( $product->is_on_sale() ) :
        echo '<span class="onsale">'. esc_html__( 'Sale!', 'woocommerce' ) .'</span>';
    endif;
}

You might have to make some hook priority changes, depending on your theme. 您可能必须根据主题进行一些挂钩优先级更改。 This code support also storefront theme. 此代码也支持店面主题。

Code goes in function.php file of your active child theme (or active theme). 代码位于活动子主题(或活动主题)的function.php文件中。 Tested and works. 经过测试和工作。

在此输入图像描述

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

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