在Woocommerce单一产品页面中的单击事件上更改添加到购物车按钮文本 [英] Change add to cart button text on click event in Woocommerce single product pages
本文介绍了在Woocommerce单一产品页面中的单击事件上更改添加到购物车按钮文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我单击添加..."时,我正在尝试交换woocommerce添加到购物车"按钮的名称. 如何在下面的函数中添加onclick行为?(无需编辑woocommerce模板文件.)
I am trying to swap the name of the woocommerce "Add to cart" button when clicked to "Adding..." how can I add the onclick behavior to the function below?( without editing the woocommerce template file.)
add_filter( 'woocommerce_product_single_add_to_cart_text', 'quailstudio_add_to_cart_text' );
function quailstudio_add_to_cart_text() {
return __( 'Adding...', 'your-slug' );
}
推荐答案
以下代码将通过单击事件中的自定义文本替换正常的添加到购物车文本按钮:
The following code will replace the normal add to cart text button by a custom text on click event:
add_action( 'wp_footer', 'single_add_to_cart_event_text_replacement' );
function single_add_to_cart_event_text_replacement() {
global $product;
if( ! is_product() ) return; // Only single product pages
if( $product->is_type('variable') ) return; // Not variable products
?>
<script type="text/javascript">
(function($){
$('button.single_add_to_cart_button').click( function(){
$(this).text('<?php _e( "Adding...", "woocommerce" ); ?>');
});
})(jQuery);
</script>
<?php
}
此代码位于您的活动子主题(或活动主题)的function.php文件中.经过测试并可以工作.
This code goes on function.php file of your active child theme (or active theme). tested and works.
这篇关于在Woocommerce单一产品页面中的单击事件上更改添加到购物车按钮文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文