更改“查看购物车"Woocommerce 中产品循环上的产品叠加按钮 [英] Change the "view cart" product overlay button on product loops in Woocommerce

查看:58
本文介绍了更改“查看购物车"Woocommerce 中产品循环上的产品叠加按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WooCommerce 相对较新,我正在尝试对您成功将产品添加到购物车后出现的按钮进行简单更改.

Relatively new to WooCommerce and I'm trying to make a simple change to a button that appears after you have successfully added a product to the cart.

我正在使用 Shop Isle 并创建了一个子主题.

I am using Shop Isle and have created a child theme.

当您将鼠标悬停在产品上时,会出现一个叠加层,允许您将产品添加到购物车.此按钮会触发添加产品的 AJAX 调用,并在成功时显示一个新的查看篮子按钮.

When you hover over a product an overlay appears that allows you to add a product to the basket. This button triggers an AJAX call which adds the product and on success it shows a new View Basket button.

据我所知,这是由 Woocommerce 文件(不是特定主题的文件)完成的,并在 add-to-cart.js (js/前端/添加到购物车.js)

From what I can tell this is being done by a Woocommerce file (not a theme specific one) and is handled in add-to-cart.js (js/frontend/add-to-cart.js)

我想要做的是更改此文本和链接以直接转到结帐页面.

What I want to do is change this text and the link to go directly to the checkout page.

我找到的代码如下:

if ( ! wc_add_to_cart_params.is_cart && $button.parent().find( '.added_to_cart' ).length === 0 ) 
{
    $button.after( ' <a href="' + wc_add_to_cart_params.cart_url + '" class="added_to_cart wc-forward" title="' +
    wc_add_to_cart_params.i18n_view_cart + '">' + wc_add_to_cart_params.i18n_view_cart + '</a>' );
}

我知道我可以简单地覆盖这里的代码,但是当插件更新时它不会成​​为未来的证明,更重要的是我实际上不确定该函数从哪里获取 wc_add_to_cart_params以及我什至会去覆盖它的地方.

I know I could simply overwrite the code here but it wouldn't be future proof when the plugin gets updated and even more importantly I'm not actually sure where the function is getting the wc_add_to_cart_params from and where I would even go about overwriting this.

任何帮助将不胜感激.

推荐答案

从 js 的查看购物车"按钮更改文本和 URL 的正确和最干净的方法是:

The right and cleanest way to change the text and URL from js "View cart" button is:

add_filter( 'woocommerce_get_script_data', 'change_js_view_cart_button', 10, 2 ); 
function change_js_view_cart_button( $params, $handle )  {
    if( 'wc-add-to-cart' !== $handle ) return $params;

    // Changing "view_cart" button text and URL
    $params['i18n_view_cart'] = esc_attr__("Checkout", "woocommerce"); // Text
    $params['cart_url']      = esc_url( wc_get_checkout_url() ); // URL

    return $params;
}

代码位于活动子主题(或活动主题)的 functions.php 文件中.经测试有效.

这篇关于更改“查看购物车"Woocommerce 中产品循环上的产品叠加按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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