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

在 WooCommerce 中,我不想为未登录的用户显示产品“添加到购物车”按钮。我正在使用下面的代码更改“添加到购物车”按钮:


// 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;

}

它适用于除单个产品页面之外的所有页面。产品页面继续显示默认的“添加到购物车”按钮。我缺少什么?


繁华开满天机
浏览 142回答 1
1回答

慕容森

更新:将单个产品上的按钮链接添加到我的帐户登录 URL以下将以更好的方式完成您想要的一切(替换您的代码):// Replacing the button add to cart by a link to the product page in Shop and archives pagesadd_filter( 'woocommerce_loop_add_to_cart_link', 'replace_loop_add_to_cart_button', 10, 2 );function replace_loop_add_to_cart_button( $button, $product&nbsp; ) {&nbsp; &nbsp; // Only for unlogged user&nbsp; &nbsp; if( ! is_user_logged_in() ){&nbsp; &nbsp; &nbsp; &nbsp; $button_text = __( "Sign up for pricing", "woocommerce" );&nbsp; &nbsp; &nbsp; &nbsp; // $button_link = get_permalink( wc_get_page_id( 'myaccount' ) ); // Login Url&nbsp; &nbsp; &nbsp; &nbsp; $button_link = $product->get_permalink(); // Single product Url&nbsp; &nbsp; &nbsp; &nbsp; $button = '<a class="button" href="' . $product->get_permalink() . '">' . $button_text . '</a>';&nbsp; &nbsp; }&nbsp; &nbsp; return $button;}// Replacing the single product button add to cart by a custom buttonadd_action( 'woocommerce_single_product_summary', 'disabled_single_add_to_cart_button', 1 );function disabled_single_add_to_cart_button() {&nbsp; &nbsp; global $product;&nbsp; &nbsp; // Only for unlogged user&nbsp; &nbsp; if( ! is_user_logged_in() ){&nbsp; &nbsp; &nbsp; &nbsp; // For variable product types (keeping attribute select fields)&nbsp; &nbsp; &nbsp; &nbsp; if( $product->is_type( 'variable' ) ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remove_action( 'woocommerce_single_variation', 'woocommerce_single_variation_add_to_cart_button', 20 );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add_action( 'woocommerce_single_variation', 'custom_product_button', 20 );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // For all other product types&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart', 30 );&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add_action( 'woocommerce_single_product_summary', 'custom_product_button', 30 );&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}// The custom replacement button function inked to loggin pagefunction custom_product_button(){&nbsp; &nbsp; $login_url = get_permalink( wc_get_page_id( 'myaccount' ) );&nbsp; &nbsp; echo '<a class="button" href="'.$login_url.'">' . __( "Sign up for pricing", "woocommerce" ) . '</a>';}代码位于活动子主题(活动主题)的functions.php 文件中。经过测试并有效。要在单个产品页面上禁用按钮,请改用:// The custom replacement button function with a disabled buttonfunction custom_product_button(){&nbsp; &nbsp; echo '<a class="button disabled">' . __( "Sign up for pricing", "woocommerce" ) . '</a>';}要隐藏商店和存档页面上的价格:add_filter( 'woocommerce_after_shop_loop_item_title', 'remove_woocommerce_loop_price', 2 );function remove_woocommerce_loop_price() {&nbsp; &nbsp; // Only for unlogged user&nbsp; &nbsp; if( ! is_user_logged_in() )&nbsp; &nbsp; &nbsp; &nbsp; remove_action( 'woocommerce_after_shop_loop_item_title', 'woocommerce_template_loop_price', 10 );}要从单个产品页面中删除产品价格:add_filter( 'woocommerce_single_product_summary', 'remove_woocommerce_single_price', 2 );function remove_woocommerce_single_price() {&nbsp; &nbsp; // Only for unlogged user&nbsp; &nbsp; if( ! is_user_logged_in() )&nbsp; &nbsp; &nbsp; &nbsp; remove_action('woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );}// Hide variations price for variable productsadd_filter( 'woocommerce_available_variation', 'hide_variations_price_html', 10, 3) ;function hide_variations_price_html( $data, $product, $variation ) {&nbsp; &nbsp; // Only for unlogged user&nbsp; &nbsp; if( ! is_user_logged_in() )&nbsp; &nbsp; &nbsp; &nbsp; $data['price_html'] = ' ';&nbsp; &nbsp; return $data;}代码位于活动子主题(活动主题)的functions.php 文件中。经过测试并有效。
打开App,查看更多内容
随时随地看视频慕课网APP