我有一些代码blocking the price from being shown on all products, if the user is not logged in.. 这就是我想要的。
我的问题是1 product that is free, and I need the price to show if the user is not logged in. 仅在此单一产品上。。。
有没有人能帮我按id定位单个产品,并显示用户未登录的价格。。。
这是我在Functions中的原始php代码段。php,阻止用户未登录时显示价格
// Hide prices on public woocommerce (not logged in)
add_action(\'after_setup_theme\',\'activate_filter\') ;
function activate_filter(){
add_filter(\'woocommerce_get_price_html\', \'show_price_logged\');
}
function show_price_logged($price){
if(is_user_logged_in()){
return $price;
}
else
{
remove_action( \'woocommerce_after_shop_loop_item\',
\'woocommerce_template_loop_add_to_cart\' );
remove_action( \'woocommerce_single_product_summary\',
\'woocommerce_template_single_price\', 10 );
remove_action( \'woocommerce_single_product_summary\',
\'woocommerce_template_single_add_to_cart\', 30 );
remove_action( \'woocommerce_after_shop_loop_item_title\',
\'woocommerce_template_loop_price\', 10 );
return \'<a href="\' . get_permalink(woocommerce_get_page_id(\'myaccount\')) .
\'">Call for pricing</a>\';
}
}
SO网友:fakemeta
如果您知道id,只需在woocommerce_get_price_html 措施:
add_action(\'after_setup_theme\',\'activate_filter\') ;
function activate_filter() {
add_filter(\'woocommerce_get_price_html\', \'show_price_logged\');
}
function show_price_logged($price) {
global $product; // get current product
if(is_user_logged_in() || $product->id === 8) { // check product id
return $price;
} else {
remove_action( \'woocommerce_after_shop_loop_item\', \'woocommerce_template_loop_add_to_cart\' );
remove_action( \'woocommerce_single_product_summary\', \'woocommerce_template_single_price\', 10 );
remove_action( \'woocommerce_single_product_summary\', \'woocommerce_template_single_add_to_cart\', 30 );
remove_action( \'woocommerce_after_shop_loop_item_title\', \'woocommerce_template_loop_price\', 10 );
return \'<a href="\' . get_permalink(woocommerce_get_page_id(\'myaccount\')) . \'">Call for pricing</a>\';
}
}
但如果您需要更大的灵活性,可以查看产品自定义字段。例如,您可以设置
is_free 自定义字段到
true 或您在产品编辑页面上选择的任何其他值,并检查其值,如下所示:
...
global $product;
$is_free_product = get_post_meta($product->id, \'is_free\', true);
if(is_user_logged_in() || $is_free_product) {
return $price;
} else {
...
SO网友:MasterFuel
向函数中添加了$product,还添加了一个带有产品ID的变量
add_action(\'after_setup_theme\',\'activate_filter\') ;
function activate_filter(){
add_filter( \'woocommerce_get_price_html\', \'show_price_if_logged_in\', 100, 2
); }
function show_price_if_logged_in( $price, **$product** ){
// Define here your free product ID (here for example ID is 121)
**$free_product_id = 121;**
// Shows the price if customer is logged in or if product is your free product
if( is_user_logged_in() || **$free_product_id == $product->id** )
{
return $price;
}
else
{
remove_action( \'woocommerce_after_shop_loop_item\', \'woocommerce_template_loop_add_to_cart\' );
remove_action( \'woocommerce_single_product_summary\', \'woocommerce_template_single_price\', 10 );
remove_action( \'woocommerce_single_product_summary\', \'woocommerce_template_single_add_to_cart\', 30 );
remove_action( \'woocommerce_after_shop_loop_item_title\', \'woocommerce_template_loop_price\', 10 );
return \'<a href="\' . get_permalink(woocommerce_get_page_id(\'myaccount\')) . \'">Call for pricing</a>\';
}
}