Change cart and checkout button links on WooCommerce mini cart widget

22,279

It seems that there is a problem somewhere with your theme (or in a plugin), as the minicart button links always point to the right cart and checkout pages.

The minicart buttons are hooked in woocommerce_widget_shopping_cart_buttons action hook (in the cart/mini-cart.php WooCommerce template). You will find the details HERE on includes/wc-template-hooks.php core file. It calls 2 functions that are displaying the buttons.

First you should try to refresh WordPress Permalinks, going on WP Settings > Permalinks:
Just at the end of the page click on "save". Empty your cart, and try it again to see if it changes something.

In the code below I remove first the original buttons and I replace them by the same ones where the links are customized. For each you can change the link to feet your needs (I have added in the links ?id=1 (at the end) just for testing purpose, to check changes):

add_action( 'woocommerce_widget_shopping_cart_buttons', function(){
    // Removing Buttons
    remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_button_view_cart', 10 );
    remove_action( 'woocommerce_widget_shopping_cart_buttons', 'woocommerce_widget_shopping_cart_proceed_to_checkout', 20 );

    // Adding customized Buttons
    add_action( 'woocommerce_widget_shopping_cart_buttons', 'custom_widget_shopping_cart_button_view_cart', 10 );
    add_action( 'woocommerce_widget_shopping_cart_buttons', 'custom_widget_shopping_cart_proceed_to_checkout', 20 );
}, 1 );

// Custom cart button
function custom_widget_shopping_cart_button_view_cart() {
    $original_link = wc_get_cart_url();
    $custom_link = home_url( '/cart/?id=1' ); // HERE replacing cart link
    echo '<a href="' . esc_url( $custom_link ) . '" class="button wc-forward">' . esc_html__( 'View cart', 'woocommerce' ) . '</a>';
}

// Custom Checkout button
function custom_widget_shopping_cart_proceed_to_checkout() {
    $original_link = wc_get_checkout_url();
    $custom_link = home_url( '/checkout/?id=1' ); // HERE replacing checkout link
    echo '<a href="' . esc_url( $custom_link ) . '" class="button checkout wc-forward">' . esc_html__( 'Checkout', 'woocommerce' ) . '</a>';
}

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

All code is tested on Woocommerce 3+ and works.

Share:
22,279
Suren Konathala
Author by

Suren Konathala

Climbing Different Mountains; Passionate about Technology. Motivated by 10X, Moonshot thinking. JAVA | Angular | Adobe Experience Manager (AEM) Technical Architect | Cloud Computing | Azure, AWS, Google Cloud | Leading and Mentoring Teams | Thought Leadership | Inventor INVENTIONS: The JAQ Stack | Syli.ai | Blob Viewer CODE: Github https://github.com/ksurendra CONTRIBUTOR TO BLOG/ARTCILES: My tech blog: https://medium.com/techinpieces Dzone: https://dzone.com/users/3402382/konathalasuren.html TALK: Seattle CodeCamp https://seattle.codecamp.us/speakers/ Profile https://sessionize.com/suren-konathala/ MENTOR: Microsoft Community Mentors Program (CMP) - Summer 2018

Updated on July 05, 2022

Comments

  • Suren Konathala
    Suren Konathala almost 2 years

    On Woocommerce, how can we change the URLs on "View cart" and "Checkout" links on the drop down menu that show up on hover over the shopping cart icon on the the home page?

    I have the "cart" and "checkout" pages setup but they are not linked to these.

    I can view these pages directly with urls. http://mysite/cart and http://mysite/checkout

    enter image description here