更改 WooCommerce 迷你购物车小部件上的购物车和结帐按钮链接

Change cart and checkout button links on WooCommerce mini cart widget

在 Woocommerce 上,我们如何更改下拉菜单中 "View cart" 和 "Checkout" 链接上的 URL,这些链接在悬停在主页上的购物车图标上时显示?

我有 "cart" 和 "checkout" 页面设置,但它们没有链接到这些页面。

我可以直接使用 url 查看这些页面。 http://mysite/cart and http://mysite/checkout

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.

微型购物车按钮挂在 woocommerce_widget_shopping_cart_buttons 操作挂钩中(在 cart/mini-cart.php WooCommerce 模板中)。您会找到显示按钮的详细信息 HERE on includes/wc-template-hooks.php core file. It calls 2 functions

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.

在下面的代码中,我首先删除了原始按钮,然后将它们替换为自定义 link 的相同按钮。对于每个你都可以改变 link 来满足你的需要(我在 links ?id=1 (最后)中添加只是为了测试目的,以检查更改):

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

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

所有代码都在 Woocommerce 3+ 上测试并且有效。