gpt4 book ai didi

php - 在 WooCommerce 中添加到购物车后重新标记 "add to cart"按钮

转载 作者:可可西里 更新时间:2023-11-01 00:55:13 25 4
gpt4 key购买 nike

我想在点击后重新标记我的添加到购物车按钮并将一件商品添加到购物车到再添加一件到购物车

这可能吗?

我有 child 主题 function.php 和第二个 go to cart 按钮,这是有效的。

但我不知道如何解决将一件商品添加到购物车后重新标记的问题(商店只出售一件不同尺寸的商品)。我希望我是清楚的。

这是我的代码:

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' );
}

最佳答案

更新:您将在下面找到使此重新标记有效的正确条件:

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;
}

代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

经过测试并有效。


如果您启用了 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;
}

代码位于您的事件子主题(或主题)的 function.php 文件中或任何插件文件中。

经过测试并有效。

关于php - 在 WooCommerce 中添加到购物车后重新标记 "add to cart"按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47471293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com