在 WooCommerce 中隐藏未注册用户的产品价格和添加到购物车按钮,但不隐藏变化 [英] Hide product prices and add-to-cart buttons but not variations for unregistered users in WooCommerce

查看:57
本文介绍了在 WooCommerce 中隐藏未注册用户的产品价格和添加到购物车按钮,但不隐藏变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 WooCommerce 商店中,我想在客户登录之前隐藏价格.我有以下代码可以完全做到这一点:

In my WooCommerce shop I want to hide the prices until the customer has logged in. I've got the following code working that does exactly that:

add_action('init','hide_price');
function hide_price(){
    if(!is_user_logged_in()){
        remove_action('woocommerce_after_shop_loop_item','woocommerce_template_loop_add_to_cart',10);
        remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart',30);
        remove_action('woocommerce_single_product_summary','woocommerce_template_single_price',10);
        remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_price',10);
        add_action('woocommerce_single_product_summary','print_login_to_see',31);
        add_action('woocommerce_after_shop_loop_item','print_login_to_see',11);
    }
}

function print_login_to_see(){
    echo '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">' . __('Login to see prices','theme_name') . '</a>';
}

然而,这也消除了变化下拉,我想保留它.

However this also removes the variation drop down and I would like to keep that.

有没有什么直接的方法可以保留变体下拉菜单,但在客户登录之前仍然隐藏价格?

Is there any direct way to keep the variation drop down menu but still hide the prices until the customer has logged in?

谢谢

推荐答案

更新:

您需要将商店和档案页面与单个产品页面分开.在单个产品页面中,您将定位可变产品以添加和删除特定的挂钩函数.

You will need to separate shop and archives pages from single product pages. In single product pages you will target variable products to add and remove the specific hooked functions.

最后一件事,在您的函数 print_login_to_see() 中,woocommerce_get_page_id() 已过时,被替换为wc_get_page_id() ...

Last thing, in your function print_login_to_see(), woocommerce_get_page_id() is obsolete and it's replaced by wc_get_page_id()

所以你的代码会关注这个:

So your code will look to this:

// For product archives pages
add_action( 'init', 'hide_product_archives_prices' );
function hide_product_archives_prices(){
    if( is_user_logged_in() ) return;

    remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
    remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10) ;
    add_action ( 'woocommerce_after_shop_loop_item', 'print_login_to_see', 10 );
}

//
add_action( 'woocommerce_single_product_summary', 'hide_single_product_prices', 1 );
function hide_single_product_prices(){
    if( is_user_logged_in() ) return;

    global $product;

    remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

    if( ! $product->is_type('variable') ){
        remove_action('woocommerce_single_product_summary','woocommerce_template_single_add_to_cart', 30 );
        add_action( 'woocommerce_single_product_summary','print_login_to_see', 30 );
    } else {
        remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation', 10);
        remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
        add_action( 'woocommerce_single_variation', 'print_login_to_see', 20 );
    }
}

// Display a my account link 
function print_login_to_see(){
    echo '<a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '" class="button">' . __('Login to see prices','theme_name') . '</a>';
}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

此代码已在 WooCommerce 3+ 上测试并有效.

This code is tested on WooCommerce 3+ and works.

这篇关于在 WooCommerce 中隐藏未注册用户的产品价格和添加到购物车按钮,但不隐藏变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆