簡體   English   中英

有條件的定制產品周圍的產品銷售價格和正常價格

[英]Conditional custom output around products sale price and regular price

我正在嘗試使用自定義條件輸出,當找到具有銷售價格的產品循環時,它會在銷售價格標簽中添加一個類。 如果只有正常價格,則會將此類添加到常規價格標簽中。

從不同的文檔中查看和關閉后,我似乎無法使其工作:

add_filter( 'woocommerce_get_price_html', 'custom_price_html', 100, 2 );
function custom_price_html( $price, $product ){
    ob_start();
        global $product; 
        if (isset($product->sale_price)) {
            return str_replace( '</del>', '<span class="amount">text</span></del>', $price );
            return str_replace( '</ins>', '<span class="highlight amount">highlight here</span></del>', $price );
        }
        else {
            return str_replace( '</ins>', '<span class="highlight amount">highlight here</span>text</del>', $price );
        }
}

我正在使用常規價格過濾器並嘗試將span class =“amount”標簽更改為ins span class =“amount”,但我仍然得到相同的輸出。
任何想法?

add_filter( 'woocommerce_price_html', 'price_custom_class', 10, 2 );
function price_custom_class( $price, $product ){ 
    return str_replace( '<span class="amount"></span>', '<ins><span class="amount">'.woocommerce_price( $product->regular_price    ).'</span></ins>', $price );
}

這個鈎子是一個帶有2個變量( $price$instance )的過濾器,你return $price而不是echo $price )。 您可以嘗試以這種方式使用它:

add_filter('woocommerce_sale_price_html','price_custom_class', 10, 2 ); 
function price_custom_class( $price, $product ){ 
    if (isset($product->sale_price)) {
        $price = '<del class="strike">'.woocommerce_price( $product->regular_price ).'</del> 
        <ins class="highlight">'.woocommerce_price( $product->sale_price ).'</ins>';
    }
    else
    {
        $price = '<ins class="highlight">'.woocommerce_price( $product->regular_price ).'</ins>';
    }
    return $price;
}

這個鈎子通常是出售價格。

參考: woocommerce_sale_price_html

對於正常價格,你有woocommerce_price_html過濾鈎子:

add_filter( 'woocommerce_price_html', 'price_custom_class', 10, 2 );
function price_custom_class( $price, $product ){ 
    // your code
    return $price;
}

參考: woocommerce_price_html

這里需要過濾器鈎子而不是動作鈎子來將函數或方法掛鈎到特定的過濾器動作。 改成

add_filter('woocommerce_sale_price_html','price_custom_class'); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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