简体   繁体   中英

Remove “View Cart” link which appears after click on the “Add To Cart” button in WooCommerce

I am using Wordpress version 4.5.2 and WooCommerce version 2.5.5.

After clicking on the "Add To Cart" button, one link appears "View Cart".

Can anyone help me to remove that link?

Text to remove:

要删除的文本

Because this functionality is hard-baked into the JS of WooCommerce, you can't disable it with a filter or hook. However, after trawling through WC's code, I discovered a handy (and slightly hacky) caveat:

If you add the following empty div to the same container as your Add to Cart button , the View Cart (or View Basket if you're in the UK) link won't get added:

<div class="added_to_cart"></div>

This is because the Javascript file checks to see if an element with that class already exists:

// View cart text
if ( ! wc_add_to_cart_params.is_cart && $thisbutton.parent().find( '.added_to_cart' ).size() === 0 ) {
    $thisbutton.after( ' <a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' +
        wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' );
}

If it finds an existing .added_to_cart element, it won't try to append another.

It's worth noting that Sathyanarayanan G's answer should totally work too (and I'm surprised it didn't - that sort of points to something else being wrong), but in a slightly different way.

将以下代码行添加到您的子主题的 style.css 应该可以解决问题。

a.added_to_cart {display:none !important}

I think this may be useful.

Just add this code in css.

a.added_to_cart.wc-forward {display:none}

This is code for change name of view cart button text and change button URL .

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

function change_view_cart( $params, $handle )
{
    switch ($handle) {
        case 'wc-add-to-cart':
            $params['i18n_view_cart'] = "Proceed to Cart"; //chnage Name of view cart button
            $params['cart_url'] = "http://shop.com/cart"; //change URL of view cart button
        break;
    }
    return $params;
}
add_filter( 'woocommerce_get_script_data', 'change_view_cart',10,2 );

Please do not change any code into the add-to-cart.js file. Otherwise its not work.

Any help would be greatly appreciated.

That will do the job (but its not the ideal solution)

// Removes Product Successfully Added to Cart
// Woocommerce 2.1+

add_filter( 'wc_add_to_cart_message', 'wc_custom_add_to_cart_message' );

function wc_custom_add_to_cart_message() {
    echo '<style>.woocommerce-message {display: none !important;}</style>';
}

If you want to keep the "Add To Cart" button and remove the "View Basket"

// Makes "Add to Cart" button visible again
.add_to_cart_button.added  {display:  inline-block}
// Removes "View Basket"
.added_to_cart.wc-forward {display: none}

Adding this to style.css in child theme worked for me.

 body .dhvc-woo-addtocart a.added_to_cart { display: none; }

Sorry for not having explanation. Add to functions.php the following lines

add_action( 'wp_footer', 'no_view_cart' );
function no_view_cart() {
?>
<script type="text/javascript">
jQuery( function($){

    $(document.body).on( 'added_to_cart', function( event, fragments, cart_hash, $button ) {
        $.ajax({
                type: 'POST',
                url: wc_add_to_cart_params.ajax_url,
                data: {
                    'action': 'checking_items',
                    'id'    : $button.data( 'product_id' )
                },
                success: function (response) {

                    $button.removeClass( 'added' );
                    $button.parent().find( '.added_to_cart' ).remove();

                }
            });
        });
    });
</script>
<?php
}

Other thing not related to this OP question. If you want to remove the "View Cart" button after item(s) has been removed from mini cart, you can add another jQuery function, but this should not be placed in no_view_cart() function:

$('.button[data-product_id="' + product_id + '"]').removeClass( 'added' );
$('.button[data-product_id="' + product_id + '"]').parent().find( '.added_to_cart' ).remove();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM