![](/img/trans.png)
[英]Hide out of stock products only on shop archive pages in Woocommerce
[英]Replace “Sale” badge by “Out of Stock” in Woocommerce archive pages
我正在开发woocommerce商店,这里是网站商店页面的链接
在此页面上,第一个产品out of stock
。 我想展示Out of stock
而不是“促销!” 图像上的徽章。
我怎样才能做到这一点?
添加主题的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>';
}
如果你看一下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;
}
}
以下代码将在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文件中。 经过测试和工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.