Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

php - Change cart and checkout button links on WooCommerce mini cart widget

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...