简体   繁体   English

替换 WooCommerce 中未登录用户的添加到购物车按钮

[英]Replace add to cart button for unlogged users in WooCommerce

In WooCommerce, I don't want to show product add to cart button for unlogged users.在 WooCommerce 中,我不想为未登录的用户显示产品添加到购物车按钮。 I am using the code bellow changing add to cart button:我正在使用下面的代码更改添加到购物车按钮:

// Replace add to cart button with link for users who aren't logged in
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    // Logged in users see add to cart button
    if( is_user_logged_in() ) return;

    $button_text = __( "Sign up for pricing", "woocommerce" );
    $button = '<div><a class="button" href="https://example.com/link">' . $button_text . '</a></div>';

    return $button;
}

It works on all pages except on single product pages.它适用于除单个产品页面之外的所有页面。 The product page continues to show the default add to cart button.产品页面继续显示默认的添加到购物车按钮。 What am I missing?我错过了什么?

Update : Added button link on single products to My account login URL更新:在我的帐户登录 URL 中添加了单个产品上的按钮链接

The following will do it all you want in a better way (replacing your code):以下将以更好的方式完成您想要的所有操作(替换您的代码):

// Replacing the button add to cart by a link to the product page in Shop and archives pages
add_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );
function replace_loop_add_to_cart_button( $button, $product  ) {
    // Only for unlogged user
    if( ! is_user_logged_in() ){
        $button_text = __( "Sign up for pricing", "woocommerce" );
        // $button_link = get_permalink( wc_get_page_id( 'myaccount' ) ); // Login Url
        $button_link = $product->get_permalink(); // Single product Url
        $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';
    }

    return $button;
}

// Replacing the single product button add to cart by a custom button
add_action( 'woocommerce_single_product_summary', 'disabled_single_add_to_cart_button', 1 );
function disabled_single_add_to_cart_button() {
    global $product;

    // Only for unlogged user
    if( ! is_user_logged_in() ){

        // For variable product types (keeping attribute select fields)
        if( $product->is_type( 'variable' ) ) {
            remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );
            add_action( 'woocommerce_single_variation', 'custom_product_button', 20 );
        }
        // For all other product types
        else {
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );
            add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );
        }
    }
}

// The custom replacement button function inked to loggin page
function custom_product_button(){
    $login_url = get_permalink( wc_get_page_id( 'myaccount' ) );
    echo '<a class="button" href="'.$login_url.'">' . __( "Sign up for pricing", "woocommerce" ) . '</a>';
}

Code goes in functions.php file of your active child theme (active theme).代码进入您的活动子主题(活动主题)的functions.php文件。 Tested and works.测试和工作。

To make a disabled button on single product page use instead:要在单个产品页面上禁用按钮,请改用:

 // The custom replacement button function with a disabled button function custom_product_button(){ echo '<a class="button disabled">'. __( "Sign up for pricing", "woocommerce" ). '</a>'; }

To Hide prices on shop and archive pages:在商店和存档页面上隐藏价格:

add_filter( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );
function remove_woocommerce_loop_price() {
    // Only for unlogged user
    if( ! is_user_logged_in() )
        remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );
}

To remove product price from single product pages:要从单个产品页面中删除产品价格:

add_filter( 'woocommerce_single_product_summary', 'remove_woocommerce_single_price', 2 );
function remove_woocommerce_single_price() {
    // Only for unlogged user
    if( ! is_user_logged_in() )
        remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
}


// Hide variations price for variable products
add_filter( 'woocommerce_available_variation', 'hide_variations_price_html', 10, 3) ;
function hide_variations_price_html( $data, $product, $variation ) {
    // Only for unlogged user
    if( ! is_user_logged_in() )
        $data['price_html'] = ' ';

    return $data;
}

Code goes in functions.php file of your active child theme (active theme).代码进入您的活动子主题(活动主题)的functions.php文件。 Tested and works.测试和工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将WooCommerce上的添加到购物车按钮替换为自定义按钮 - Replace add to cart button with a custom button on WooCommerce 为 Woocommerce 中的登录用户隐藏“添加到购物车”按钮 - Hide "Add to Cart" button for logged in users in Woocommerce 如果用户未登录 Woocommerce,请避免将特定产品类别添加到购物车 - Avoid add to cart for specific product categories if user is unlogged in Woocommerce 用未登录用户的文本和特定产品标签替换WooCommerce产品价格 - Replace WooCommerce product price with a text for unlogged users and a specific product Tag Woocommerce 为未注册用户禁用“添加到购物车”按钮时出现问题 - Woocommerce Problem disabling the Add to Cart button for unregistered users 为产品类别替换 Woocommerce 单一产品页面上的“添加到购物车”按钮 - Replace Add to cart button on Woocommerce Single Product Pages for a product category 替换WooCommerce单品中按重量加入购物车按钮 - Replace add to cart button based on weight in WooCommerce single products 如何用购物车图标和购物车图标以及自定义文本替换Woocommerce的“添加到购物车”按钮? - How to replace Woocommerce “Add to Cart” Button with a Cart Icon and Cart Icon along with custom text? 我如何用图标替换“添加到购物车”文本(添加到购物车按钮woocommerce) - How can i replace text “Add to cart” with an icon (Add to cart button woocommerce) Woocommerce中未登录用户的取消隐藏结帐登录表单 - Unhide Checkout Login form for unlogged users in Woocommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM