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