簡體   English   中英

在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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM