簡體   English   中英

根據 Woocommerce 中的產品類別隱藏價格

[英]Hide Price based on product category in Woocommerce

在 Woocommerce 中,我試圖根據類別在存檔頁面和單個產品頁面上隱藏產品,但是條件似乎不起作用,無論我是否設置類別,都隱藏所有價格

add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );

function woocommerce_remove_prices( $price, $product ) {
     if(is_product_category('sold')){   
        $price = '';
        return $price;
     } 
}

為了使您的代碼正常工作,您應該需要對單個產品頁面使用has_term()條件函數,並且您需要始終在最后返回價格,在if語句之外:

add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'woocommerce_remove_prices', 10, 2 );
add_filter( 'woocommerce_get_price_html', 'woocommerce_remove_prices', 10, 2 );
function woocommerce_remove_prices( $price, $product ) {
    if( is_product_category('sold') || has_term( 'sold', 'product_cat', $product->get_id() ) )
        $price = '';

    return $price;
}

有用! 但這不會刪除選定的產品變體價格,並且您仍然可以在任何地方添加添加到購物車按鈕。

代碼位於活動子主題(或活動主題)的 function.php 文件中。


相反,您可以使用以下內容刪除該特定產品類別上的所有價格、數量按鈕和添加到購物車按鈕:

// Specific product category archive pages
add_action( 'woocommerce_after_shop_loop_item_title', 'hide_loop_product_prices', 1 );
function hide_loop_product_prices(){
    global $product;

    if( is_product_category('sold') ):

    // Hide prices
    remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
    // Hide add-to-cart button
    remove_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart', 30 );

    endif;
}

// Single product pages
add_action( 'woocommerce_single_product_summary', 'hide_single_product_prices', 1 );
function hide_single_product_prices(){
    global $product;

    if( has_term( 'sold', 'product_cat', $product->get_id() ) ):

    // Hide prices
    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

    // Hide add-to-cart button, quantity buttons (and attributes dorpdowns for variable products)
    if( ! $product->is_type('variable') ){
        remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
    } else {
        remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
    }

    endif;
}

代碼位於活動子主題(或活動主題)的 function.php 文件中。

測試和工作。

暫無
暫無

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

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