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

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

问题描述

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

我设置了购物车"和结帐"页面,但它们没有链接到这些页面.

我可以通过网址直接查看这些页面.

解决方案

似乎您的主题在某处存在问题(或在插件中),因为迷你购物车按钮链接始终指向正确的购物车和结帐页面.

迷你车按钮在 woocommerce_widget_shopping_cart_buttons 动作挂钩(在cart/mini-cart.php WooCommerce 模板中)中挂钩.您会在此处找到详细信息/wc-template-hooks.php 核心文件.它调用2个显示按钮的函数.

<块引用>

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

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

add_action('woocommerce_widget_shopping_cart_buttons', function(){//删除按钮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);//添加自定义按钮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_widget_shopping_cart_button_view_cart() {$original_link = wc_get_cart_url();$custom_link = home_url( '/cart/?id=1' );//这里替换购物车链接echo '<a href="' .esc_url( $custom_link ) .'" class="button wc-forward">'.esc_html__( '查看购物车', 'woocommerce') .'</a>';}//自定义结账按钮函数 custom_widget_shopping_cart_proceed_to_checkout() {$original_link = wc_get_checkout_url();$custom_link = home_url('/checkout/?id=1');//这里替换结帐链接echo '<a href="' .esc_url( $custom_link ) .'" class="button checkout wc-forward">'.esc_html__( 'Checkout', 'woocommerce') .'</a>';}

代码位于活动子主题(或主题)的 function.php 文件或任何插件文件中.

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

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

解决方案

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.

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

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