I'd like to relabel my add to cart button after click on it and add one item to cart into add one more to cart.
我想点击它后重新标记我的添加到购物车按钮,并将一个项目添加到购物车,再添加一个到购物车。
Is this possible?
这可能吗?
I have Child-Theme function.php with a second go to cart button and this is working.
我有Child-Theme function.php,第二个转到购物车按钮,这是有效的。
But I don't know how to solve this re-label after one item has been added to cart (shop only sells one item with different sizes). I hope that I am clear.
但是我不知道如何在将一件商品添加到购物车后解决这个重新标签(商店只销售一件不同尺寸的商品)。我希望我很清楚。
Here is my code:
这是我的代码:
add_filter( 'woocommerce_product_add_to_cart_text',
'customizing_add_to_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text',
'customizing_add_to_cart_button_text', 10, 2 );
function customizing_add_to_cart_button_text( $button_text, $product )
{
if ( WC()->cart->get_cart_contents_count() )
return __( 'Add one more to cart', 'woocommerce' );
} else {
return __( 'Add to cart ', 'woocommerce' );
}
2 个解决方案
#1
2
UPDATE: Below you will find the correct conditions to make this relabeling working:
更新:您将在下面找到使此重新标记工作的正确条件:
add_filter( 'woocommerce_product_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
function customizing_add_to_cart_button_text( $button_text, $product )
{
$is_in_cart = false;
foreach ( WC()->cart->get_cart() as $cart_item )
if ( $cart_item['product_id'] == $product->get_id() ) {
$is_in_cart = true;
break;
}
if( $is_in_cart )
$button_text = __( 'Add one more to cart', 'woocommerce' );
return $button_text;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
Tested and works.
经过测试和工作。
if you have enabled Ajax, for NON variable products on archives pages (like shop pages or product category pages) and you want to get this live event, you should add this too:
如果你已经启用了Ajax,对于档案页面上的非变量产品(如商店页面或产品类别页面),你想要获得这个实况事件,你也应该添加它:
add_action('wp_footer','custom_jquery_add_to_cart_script');
function custom_jquery_add_to_cart_script(){
if ( is_shop() || is_product_category() || is_product_tag() ): // Only for archives pages
$new_text = __( 'Add one more to cart', 'woocommerce' );
?>
<script type="text/javascript">
// Ready state
(function($){
$('a.add_to_cart_button').click( function(){
$this = $(this);
$( document.body ).on( 'added_to_cart', function(){
$($this).text('<?php echo $new_text; ?>');
console.log('EVENT: added_to_cart');
});
});
})(jQuery); // "jQuery" Working with WP (added the $ alias as argument)
</script>
<?php
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
Tested and works.
经过测试和工作。
#2
0
Add a add-to-cart.php to your theme/woocommerce/loop folder. Then add the code below before the sprintf button function.
在你的theme / woocommerce / loop文件夹中添加一个add-to-cart.php。然后在sprintf按钮功能之前添加下面的代码。
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$inCart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if($values['product_id'] == $product->id && $values['quantity'] > 1){
$inCart = true;
break;
}
}
$buttonText = $inCart?'add one more':'add to cart';
Change the last line in the sprintf function to
将sprintf函数中的最后一行更改为
esc_html( ( !empty( $product_addons ) )?'Select options':$buttonText )
You might need to refine this if you switch on the Ajax cart
如果您打开Ajax购物车,可能需要对其进行优化
#1
2
UPDATE: Below you will find the correct conditions to make this relabeling working:
更新:您将在下面找到使此重新标记工作的正确条件:
add_filter( 'woocommerce_product_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text', 'customizing_add_to_cart_button_text', 10, 2 );
function customizing_add_to_cart_button_text( $button_text, $product )
{
$is_in_cart = false;
foreach ( WC()->cart->get_cart() as $cart_item )
if ( $cart_item['product_id'] == $product->get_id() ) {
$is_in_cart = true;
break;
}
if( $is_in_cart )
$button_text = __( 'Add one more to cart', 'woocommerce' );
return $button_text;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
Tested and works.
经过测试和工作。
if you have enabled Ajax, for NON variable products on archives pages (like shop pages or product category pages) and you want to get this live event, you should add this too:
如果你已经启用了Ajax,对于档案页面上的非变量产品(如商店页面或产品类别页面),你想要获得这个实况事件,你也应该添加它:
add_action('wp_footer','custom_jquery_add_to_cart_script');
function custom_jquery_add_to_cart_script(){
if ( is_shop() || is_product_category() || is_product_tag() ): // Only for archives pages
$new_text = __( 'Add one more to cart', 'woocommerce' );
?>
<script type="text/javascript">
// Ready state
(function($){
$('a.add_to_cart_button').click( function(){
$this = $(this);
$( document.body ).on( 'added_to_cart', function(){
$($this).text('<?php echo $new_text; ?>');
console.log('EVENT: added_to_cart');
});
});
})(jQuery); // "jQuery" Working with WP (added the $ alias as argument)
</script>
<?php
endif;
}
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
代码放在活动子主题(或主题)的function.php文件中,或者放在任何插件文件中。
Tested and works.
经过测试和工作。
#2
0
Add a add-to-cart.php to your theme/woocommerce/loop folder. Then add the code below before the sprintf button function.
在你的theme / woocommerce / loop文件夹中添加一个add-to-cart.php。然后在sprintf按钮功能之前添加下面的代码。
global $woocommerce;
$items = $woocommerce->cart->get_cart();
$inCart = false;
foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
if($values['product_id'] == $product->id && $values['quantity'] > 1){
$inCart = true;
break;
}
}
$buttonText = $inCart?'add one more':'add to cart';
Change the last line in the sprintf function to
将sprintf函数中的最后一行更改为
esc_html( ( !empty( $product_addons ) )?'Select options':$buttonText )
You might need to refine this if you switch on the Ajax cart
如果您打开Ajax购物车,可能需要对其进行优化