Php 替换“;销售「;徽章由「;“缺货”;电子商务档案页面中

Php 替换“;销售「;徽章由「;“缺货”;电子商务档案页面中,php,wordpress,woocommerce,product,badge,Php,Wordpress,Woocommerce,Product,Badge,我在woocommerce商店工作,这里是 在此页面上,第一个产品缺货。我想在图片上显示缺货,而不是“销售!”徽章 如何执行此操作?添加主题的functions.php文件: add_filter('woocommerce_sale_flash', 'woocommerce_custom_sale_text', 10, 3); function woocommerce_custom_sale_text($text, $post, $_product) { return '<span

我在woocommerce商店工作,这里是

在此页面上,第一个产品
缺货
。我想在图片上显示
缺货
,而不是“销售!”徽章


如何执行此操作?

添加主题的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>';
}
add_filter('woocommerce_sale_flash','woocommerce_custom_sale_text',10,3);
功能商业\自定义\销售\文本($text、$post、$\产品)
{
退货“缺货”;
}

如果查看loop/sale-flash.php模板,可以看到它有一个用于sale flash的过滤器。您可以将其添加到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;
    }
}
add_filter('woocommerce_sale_flash'、'sale_flash_stock_status');
函数sale\u flash\u stock\u status($output\u html,$post,$product){
如果($product->is\ U in\ U stock()){
//如果有库存,请保持销售闪光灯不变。
返回$output_html;
}
否则{
//更改html输出自定义库存状态
$output\u html=''.esc\u html\uuuu('缺货','woocommerce')。'
返回$output_html;
}
}

以下代码将在Woocommerce存档页面(如商店)上添加一个缺货产品的“缺货”标识,以取代产品销售时的“销售”标识:

// 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;
}
//添加徽章“缺货”(并替换销售徽章)
添加动作('woocommerce\u之前的商店\u循环\u项目\u标题','custom\u之前的商店\u循环\u项目\u标题',2);//档案页
函数自定义\u前\u车间\u循环\u项目\u标题(){
删除行动(“woocommerce”在“店铺”之前、在“商品”之前、在“商品”之前、在“产品”之前、在“销售”之前、在“快闪”之前”,10);
删除店面主题的操作('woocommerce\u在店铺之后\u循环\u项目\u标题','woocommerce\u展示\u产品\u循环\u销售\u flash',6);//对于店面主题
添加行动(“店铺前的商业活动”、“商品名称”、“展示产品”、“商品循环”、“库存外标识”,10);
}
功能显示(产品)(循环)(库存外)(徽章){
全球$post$product;
如果($product->get_stock_status()=='outofstock'):
回显“.esc_html_uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu;
其他($product->正在销售()):
echo'.esc_html_uuu('Sale!','woocommerce');
endif;
}
根据主题的不同,您可能需要对钩子优先级进行一些更改。此代码还支持店面主题

代码进入活动子主题(或活动主题)的function.php文件。测试和工作