實用的 WordPress ICP 備案許可管理器

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 用户也希望您的參與。

文章沒看懂?代碼不會用?需要幫助您可以

風間 的頭像