簡體   English   中英

替換 WooCommerce 中未登錄用戶的添加到購物車按鈕

[英]Replace add to cart button for unlogged users in 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;
}

它適用於除單個產品頁面之外的所有頁面。 產品頁面繼續顯示默認的添加到購物車按鈕。 我錯過了什么?

更新:在我的帳戶登錄 URL 中添加了單個產品上的按鈕鏈接

以下將以更好的方式完成您想要的所有操作(替換您的代碼):

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

代碼進入您的活動子主題(活動主題)的functions.php文件。 測試和工作。

要在單個產品頁面上禁用按鈕,請改用:

 // The custom replacement button function with a disabled button function custom_product_button(){ 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() {
    // Only for unlogged user
    if( ! is_user_logged_in() )
        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() {
    // 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;
}

代碼進入您的活動子主題(活動主題)的functions.php文件。 測試和工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM