显示添加到购物车只登录用户..woocommerce

时间:2013-05-12 17:08:25

标签: php wordpress 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!';
}

尝试修改它以用于隐藏添加到购物车..但无济于事.. 任何人吗?

4 个答案:

答案 0 :(得分: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.';
  }

}

答案 1 :(得分: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!';

}

答案 2 :(得分:0)

CSS怎么样?

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

body.logged-in button.add-to-cart {
    display: block;
}

答案 3 :(得分:0)

我们可以通过woocommerce_is_purchasablewoocommerce_get_price_html钩子轻松地做到这一点。

代码:

// Disable purchase for non-logged-in users. (Remove add-to-cart button).
function m3wc_woocommerce_is_purchasable( $is_purchasable, $product ) {
    if ( ! is_user_logged_in() ) {
        return false;
    }

    return $is_purchasable;
}
add_filter( 'woocommerce_is_purchasable', 'm3wc_woocommerce_is_purchasable', 10, 2 );

// Show "Login to see prices" instead of price for non-logged in users.
function m3wc_woocommerce_get_price_html( $price ) {
    if ( ! is_user_logged_in() ) {
        return '<a href="' . get_permalink( wc_get_page_id( 'myaccount' ) ) . '">Login</a> to see prices';
    }

    return $price;
}
add_filter( 'woocommerce_get_price_html', 'm3wc_woocommerce_get_price_html', 10, 2 );

结果:

non-logged in users

来源:WooCommerce - Disable purchase for non-logged-in users. (Remove add-to-cart button).