Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅为登录用户显示添加到购物车..woocommerce_Php_Wordpress_Woocommerce - Fatal编程技术网

Php 仅为登录用户显示添加到购物车..woocommerce

Php 仅为登录用户显示添加到购物车..woocommerce,php,wordpress,woocommerce,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')). '">Log

一直在使用此代码隐藏价格

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!';
}
add_filter('woocommerce_get_price_html','members_only_price');
功能成员\u仅\u价格($price){
如果(用户是否已登录){
返回$price;
}
否则返回“或查看价格!”;
}
尝试将其修改为用于隐藏“添加到购物车”,但无效。。
有人吗?

你试过这样的东西吗?您可以将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!';
}
add_filter('catalog_visibility_alternate_price_html','my_alternate_price_text',10,1);
函数my_alternate_price_text($content){
返回“或查看价格!”;
}
参考:

编辑:

参考材料具有推车可见性参考

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!';

}
add_filter('catalog_visibility_alternate_add_to_cart_button'、'my_alternate_button',10,1);
功能我的备用按钮($content){
return“或查看购物车!”;
}
扩展上述代码(感谢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.';
  }

}
add_filter('woocommerce_get_price_html','members_only_price');
功能成员\u仅\u价格($price){
如果(用户是否已登录){
返回$price;
}
否则{
删除操作('woocommerce'u在'u shop'u loop'u item'之后,'woocommerce'模板'u loop'u将'u添加到'u cart');
删除行动('woocommerce\u single\u product\u summary'、'woocommerce\u template\u single\u price',10);
删除操作('woocommerce\u single\u product\u summary'、'woocommerce\u template\u single\u add\u to\u cart',30);
删除操作('woocommerce'u在'u shop'u loop'u item'u title'之后,'woocommerce'u模板'u loop'u price',10);
return“只能查看定价”;
}
}
CSS呢

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

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

我们可以通过
woocommerce\u是可购买的
woocommerce\u获取价格\u html
挂钩轻松做到这一点

代码:

//禁用未登录用户的购买。(删除“添加到购物车”按钮)。
功能m3wc\u woocommerce\u可购买($is\u可购买,$product){
如果(!用户是否已登录()){
返回false;
}
return$可购买;
}
添加过滤器('woocommerce'u可购买','m3wc\u woocommerce'u可购买',10,2);
//对于未登录的用户,显示“登录查看价格”而不是“价格”。
函数m3wc\u woocommerce\u get\u price\u html($price){
如果(!用户是否已登录()){
返回“查看价格”;
}
返回$price;
}
添加过滤器('woocommerce\u get\u price\u html','m3wc\u woocommerce\u get\u price\u html',10,2);
结果是:


来源:

谢谢你的回答..我的代码可以为未登录的用户隐藏价格..我只需要为未登录的用户隐藏“添加到购物车”的内容..尝试过了..我想只有在我为woocommerce购买目录可见性扩展时,过滤器才会起作用..谢谢。。
// 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 );