加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 13:47:51 所属栏目:PHP教程 来源:网络整理
导读:在Woocommerce上,我们如何更改显示在主页上的购物车图标上方的下拉菜单中的“查看购物车”和“结帐”链接上的网址? 我有“购物车”和“结帐”页面设置,但它们没有链接到这些. 我可以直接使用网址查看这些网页. http://mysite/cart和http://mysite/checkout
在Woocommerce上,我们如何更改显示在主页上的购物车图标上方的下拉菜单中的“查看购物车”和“结帐”链接上的网址?

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

我可以直接使用网址查看这些网页. http://mysite/cart和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 on includes/wc-template-hooks.php核心文件.它调用显示按钮的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.

在下面的代码中,我首先删除原始按钮,然后将其替换为自定义链接的相同按钮.对于每一个,您可以将链接更改为满足您的需求(我已添加链接?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上进行测试并且有效.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读