WooCommerce 按用户角色删除购物车 [英] WooCommerce remove shopping cart by user role

查看:29
本文介绍了WooCommerce 按用户角色删除购物车的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Woocommerce 中,我有一个功能,可以将添加到购物车按钮替换为商店和存档页面中产品的链接按钮:

In Woocommerce, I have a function that replace add to cart button by a linked button to the product in shop and archive pages:

function quantity_inputs_for_woocommerce_loop_add_to_cart_link( $html, $product ) {
if ( ! current_user_can('customer') ) {
    $link = get_permalink($product_id);
    $button_text = __( "View product", "woocommerce" );
    $html = '<a href="'.$link.'" class="button alt add_to_cart_button">'.$button_text.'</a>';
}
return $html;
}
add_filter( 'woocommerce_loop_add_to_cart_link', 'conditionally_change_loop_add_to_cart_link', 10, 2 );

如果用户不是以客户身份登录,我想删除所有页面上的添加到购物车按钮.

I would like to remove the add to cart button on all pages if a user is not logged in as a customer.

有人可以帮忙吗?

推荐答案

代替您的实际代码,请尝试以下将在任何地方执行所有操作并在用户未登录时删除添加到购物车按钮的操作:

Instead of your actual code, try the following that will do everything everywhere and will remove add to cart button when user is not logged in:

add_filter('woocommerce_is_purchasable', 'woocommerce_is_purchasable_filter_callback', 10, 2 );
function woocommerce_is_purchasable_filter_callback( $purchasable, $product ) {
    if ( ! is_user_logged_in() ) 
        $purchasable = false;

    return $purchasable;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.

Code goes in function.php file of your active child theme (or active theme).

这篇关于WooCommerce 按用户角色删除购物车的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆