您可能希望強制用户登錄以查看價格並將產品添加到購物車。所有您需要的是將以下代碼粘貼到您的 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)

 /**  * @snippet       Hide Price & Add to Cart for Logged Out Users   * @sourcecode    https://businessbloomer.com/?p=299  * @author        Rodolfo Melogli  * @testedwith    WooCommerce 2.4.12  */  add_filter('woocommerce_get_price_html', 'bbloomer_show_price_logged');  function bbloomer_show_price_logged($price){ if(is_user_logged_in() ){ return $price; } else { 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 ); remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 ); 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 ); } }  function bbloomer_print_login_to_see() { echo '<a href="' . get_permalink(wc_get_page_id('myaccount')) . '">' . __('Login to see prices', 'theme_name') . '</a>'; }  

<

替代的 PHP 片段(#3)

add_action('after_setup_theme','activate_filter') ;  function activate_filter(){ add_filter('woocommerce_get_price_html', 'bbloomer_show_price_logged'); }  function bbloomer_show_price_logged($price){ if(is_user_logged_in() ){ return $price; } else { return '<a href="' . get_permalink(woocommerce_get_page_id('myaccount')) . '">Login to See Prices</a>'; 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 ); } } 


可以在哪裏添加此代碼?

您可以將 PHP 代碼片段放置在主題或子主題的 functions.php 文件的底部(如果是 CSS 代碼,請添加到主題的 style.css 文件底部),修改之前建議先備份原始文件,若出現錯誤請先刪除此代碼。


這段代碼是否正常可用?

或者是您有更好的解決方案想要分享?請到薇曉朵 WooCommerce 中文論壇留言告知,我們希望可以幫到更多國內的 WooCommerce 用户也希望您的參與。


需要關於 WooCommerce 的幫助?

請觀看我們提供的免費視頻教程或到薇曉朵 WooCommerce 中文論壇提問,會有專業技術團隊提供相關幫助。