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

问题描述 投票:1回答:3

我在woocommerce商店工作,这里是the link of the website shop page

在这个页面上第一个产品是out of stock。我想展示Out of stock而不是“Sale!”图像上的徽章。

我怎样才能做到这一点?

php wordpress woocommerce product badge
3个回答
1
投票

如果你看一下loop / sale-flash.php模板,你可以看到它有一个销售闪存的过滤器。您可以将其添加到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;
    }
}

1
投票

添加主题的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>';
}

1
投票

以下代码将在Woocommerce存档页面(作为商店)上添加“Out of Stock”徽章,用于替换“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;
}

您可能必须根据主题进行一些挂钩优先级更改。此代码也支持店面主题。

代码位于活动子主题(或活动主题)的function.php文件中。经过测试和工作。

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.