WooCommerce 教程:隐藏价格并添加到购物车仅登录用户可见

WooCommerce 教程:隐藏价格并添加到购物车仅登录用户可见

WooCommerce 未登录用户隐藏价格,您可能希望强制用户登录以查看价格并将产品添加到购物车。

所有您需要的是将以下代码粘贴到您的 functions.php 中(请注意:您的主题可能已经覆盖了一些原始的 WooCommerce 钩子和过滤器,因此下面的代码可能无法正常工作。如果您需要自定义代码,请与我联系)

WooCommerce:隐藏价格并添加到购物车登录用户

WooCommerce PHP 代码段:隐藏添加到购物车和价格如果注销并显示 “登录查看价格”(#1)

/**
 * @snippet       Hide Price & Add to Cart for Logged Out Users
 * @sourcecode    https://businessbloomer.com/?p=299
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.1.1
 */

add_action('init', 'bbloomer_hide_price_add_cart_not_logged_in');

function bbloomer_hide_price_add_cart_not_logged_in() {
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', 'bbloomer_print_login_to_see', 31 );
 add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_print_login_to_see', 11 );
}
}

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

替代 PHP 片段(#2)

<?php

function woocommerce_template_loop_price() {
    if ( is_user_logged_in() )
        woocommerce_get_template( 'loop/price.php' );
}

function woocommerce_template_loop_add_to_cart() {
    if ( is_user_logged_in() )
        woocommerce_get_template( 'loop/add-to-cart.php' );
}

function woocommerce_template_single_price() {
    if ( is_user_logged_in() )
        woocommerce_get_template( 'single-product/price.php' );
}

function woocommerce_template_single_add_to_cart() {
    global $product;

    if ( is_user_logged_in() )
        do_action( 'woocommerce_' . $product->product_type . '_add_to_cart'  );
}

替代 PHP 片段(#3)

2020 年 10 月 14 日补充,使用 WooCommerce 的另一个函数 woocommerce_is_purchasable 让全站产品不可购买。

//this will remove add to cart button and add a read more button to all products in the cart. and remove the add to cart everywhere. 
// only for logged out users 
 
add_action( 'init', 'shessvy_hide_price_add_cart_not_logged_in' );
 
function shessvy_hide_price_add_cart_not_logged_in() {
    if ( ! is_user_logged_in() ) {
 
        add_filter( 'woocommerce_is_purchasable', '__return_false');
 
        add_action( 'woocommerce_single_product_summary', 'shessvy_print_login_to_see', 31 );
        add_action( 'woocommerce_after_shop_loop_item', 'shessvy_print_login_to_see', 11 );
        add_action( 'woocommerce_simple_add_to_cart', 'shessvy_print_login_to_see', 30 );
    }
}
 
function shessvy_print_login_to_see() {
    echo '<a href="' . get_permalink(wc_get_page_id('myaccount')) . '" rel="nofollow ugc">' . __('Login to see prices', 'theme_name') . '</a>';
}

如何添加此代码?

1 、您可以将 PHP 代码片段放置在主题或子主题的 functions.php 文件的底部(如果是 CSS 代码,请添加到子主题的 style.css 文件底部)修改之前建议先备份原始文件,若出现错误请先删除此代码。

2 、 WordPress 4.9 后改进了主题编辑器,对于 CSS 代码也可打开网站前台编辑器的【自定义】,复制代码添加到自定义 css 中。

此代码是否可用?

如需帮助或是您有更好的方案想分享?请到薇晓朵 WooCommerce 中文论坛留言告知,我们希望可以帮到更多国内的 WooCommerce 用户也希望您的参与。

文章没看懂?代码不会用?需要帮助您可以去论坛提问自助服务台

作者风间

在哪里跌倒,就在哪里趴着。