問題描述
在 Woocommerce 上,我們如何更改將鼠標懸停在主頁上的購物車圖標上時顯示的下拉菜單上的查看購物車"和結帳"鏈接上的 URL?
我設置了購物車"和結帳"頁面,但它們沒有鏈接到這些頁面.
我可以通過網(wǎng)址直接查看這些頁面.
似乎您的主題在某處存在問題(或在插件中),因為迷你購物車按鈕鏈接始終指向正確的購物車和結帳頁面.
迷你車按鈕在 woocommerce_widget_shopping_cart_buttons
動作掛鉤(在cart/mini-cart.php WooCommerce 模板中)中掛鉤.您會在此處找到詳細信息/wc-template-hooks.php 核心文件.它調用2個顯示按鈕的函數(shù).
首先,您應該嘗試刷新 WordPress 永久鏈接,繼續(xù) 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 );//自定義購物車按鈕函數(shù) 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>';}//自定義結賬按鈕函數(shù) 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 迷你購物車小部件上的購物車和結帳按鈕鏈接的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!