2013-05-12 71 views
3

一直在用這個代碼隱藏價格..顯示添加到購物車的登錄用戶only..woocommerce

add_filter('woocommerce_get_price_html','members_only_price'); 
function members_only_price($price){ 
if(is_user_logged_in()){ 
    return $price; 
} 
else return '<a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Login</a> or <a href="'.site_url('/wp-login.php?action=register&redirect_to=' . get_permalink()).'">Register</a> to see price!'; 
} 

試圖修改它使用隱藏添加到購物車作爲well..but無濟於事.. 有人嗎?

回答

1

你有沒有試過類似的東西?當用戶登錄時,你會設置woocommerce,只顯示價格

add_filter('catalog_visibility_alternate_price_html', 'my_alternate_price_text', 10, 1); 
function my_alternate_price_text($content) { 
    return '<a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Login</a> or <a href="'.site_url('/wp-login.php?action=register&redirect_to=' . get_permalink()).'">Register</a> to see price!'; 
} 

參考:http://docs.woothemes.com/document/catalog-visibility-options/

編輯:

參考材料有車的知名度參考

add_filter('catalog_visibility_alternate_add_to_cart_button', 'my_alternate_button', 10, 1); 

function my_alternate_button($content) { 

    return '<a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Login</a> or <a href="'.site_url('/wp-login.php?action=register&redirect_to=' . get_permalink()).'">Register</a> to see cart!'; 

} 
+1

感謝answer..my代碼適用於隱藏價格未記錄in..i只是需要一些隱藏添加到購物車對於非登錄用戶以及用戶.. – Parm 2013-05-13 21:32:09

+0

試過了。 ..我猜想,只有當我購買woocommerce的目錄可見性擴展時,過濾器纔會工作..謝謝,雖然.. – Parm 2013-05-14 01:02:41

5

擴展上面的代碼(感謝Ewout),以下代碼將擺脫所有woocommerce產品上的所有價格和「添加到購物車」按鈕,如以及爲什麼提供解釋。我需要一個提供直銷產品的網站的代碼,並遵守其規則,我無法向公衆顯示價格。

將過濾器添加到您主題的functions.php文件中。

add_filter('woocommerce_get_price_html','members_only_price'); 

function members_only_price($price){ 

if(is_user_logged_in()){ 
return $price; 
} 

else { 
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); 
return 'Only <a href="' .get_permalink(woocommerce_get_page_id('myaccount')). '">Registered Users</a> are able to view pricing.'; 
    } 

} 
0

怎麼樣CSS?

button.add-to-cart { 
    display: none; 
} 

body.logged-in button.add-to-cart { 
    display: block; 
} 
相關問題