您可能希望強制使用者登入以檢視價格並將產品新增到購物車。所有您需要的是將以下程式碼貼上到您的 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 中文論壇提問,會有專業技術團隊提供相關幫助。