在WooCommerce迷你购物车小部件上更改购物车和结帐按钮链接 [英] Change cart and checkout button links on WooCommerce mini cart widget

查看:588
本文介绍了在WooCommerce迷你购物车小部件上更改购物车和结帐按钮链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Woocommerce上,我们如何更改悬停在主页上的购物车图标上方的下拉菜单上查看购物车"和结帐"链接上的URL?

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.

我可以直接使用url查看这些页面. > http://mysite/cart

I can view these pages directly with urls. 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.

minicart按钮钩在 woocommerce_widget_shopping_cart_buttons 操作钩中(在cart/mini-cart.php WooCommerce模板中).您会发现详细信息包含/上的HERE wc-template-hooks.php 核心文件.它将调用显示按钮的 2个功能

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.

首先,您应该尝试刷新WP2的WordPress永久链接,设置> 永久链接:
在页面的最后,点击保存" .清空购物车,然后再试一次,看它是否有所改变.

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.

在下面的代码中,我首先删除了原始按钮,然后将它们替换为自定义链接的相同按钮.对于每一个,您都可以根据自己的需要更改链接(我在链接?id=1中添加了(最后)只是为了测试目的,以检查更改):

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

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

所有代码都在Woocommerce 3+上进行了测试,并且可以正常工作.

All code is tested on Woocommerce 3+ and works.

这篇关于在WooCommerce迷你购物车小部件上更改购物车和结帐按钮链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆