![](/img/trans.png)
[英]Add text before product price for specific category in Woocommerce
[英]WooCommerce product price visibility: add price back only for a specific category
我正在開發的網站要求您登錄才能查看價格,而我一直在使用插件來執行此操作。 然而,我只是被扔了一個曲線球,並告訴我網站上的一個特定類別必須始終顯示價格,無論用戶是否登錄。
看起來插件使用
remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
和
remove_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
刪除價格。 這就是我嘗試為特定類別中的產品重新添加價格的方式:
function make_surplus_price_always_visible(){
global $post;
$terms = wp_get_post_terms( $post->ID, 'product_cat' );
foreach ( $terms as $term ) $categories[] = $term->slug;
if ( in_array( 'surplus-allison-parts', $categories ) && !is_user_logged_in()) {
?>
<script>
alert('product in surplus');
</script>
<?php
//add_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
}
}
add_action('woocommerce_after_shop_loop_item_title', 'make_surplus_price_always_visible', 50);
但它不會把價格加回來。 jQuery 警報正在運行,因此它不是滿足“if”語句要求的問題。
如何將特定類別的產品價格加回?
更新:這是使其工作的正確方法:
has_term()
Wordpress 函數更好、更短。編碼:
add_action('woocommerce_after_shop_loop_item_title', 'shop_loop_make_surplus_price_always_visible', 8 );
function shop_loop_make_surplus_price_always_visible(){
global $post;
// Set here your product categories (Names, slugs or IDs) in this array
$categories = array( 'surplus-allison-parts' );
if ( has_term( $categories, 'product_cat', $post->ID ) && ! is_user_logged_in()) {
add_action('woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10);
}
}
和:
add_action('woocommerce_single_product_summary', 'single_product_make_surplus_price_always_visible', 8 );
function single_product_make_surplus_price_always_visible(){
global $post;
// Set here your product categories (Names, slugs or IDs) in this array
$categories = array( 'surplus-allison-parts' );
if ( has_term( $categories, 'product_cat', $post->ID ) && ! is_user_logged_in()) {
add_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10);
}
}
代碼位於活動子主題(或主題)的 function.php 文件或任何插件文件中。
測試和工作
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.