重新标记“添加到购物车"在WooCommerce中添加到购物车后的按钮 [英] Relabel "add to cart" button after add to cart in WooCommerce

查看:171
本文介绍了重新标记“添加到购物车"在WooCommerce中添加到购物车后的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击后,我想重新标记我的添加到购物车按钮,然后将一项添加到购物车到添加另一项到购物车.

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.

这可能吗?

我有Child-Theme function.php ,还有第二个 go to cart 按钮,此按钮正常运行.

I have Child-Theme function.php with a second go to cart button and this is working.

但是在将一件商品添加到购物车后,我不知道如何解决此重新标记(商店仅出售一件尺寸不同的商品).我希望我很清楚.

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.

这是我的代码:

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

推荐答案

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

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

代码会出现在您活动的子主题(或主题)的function.php文件或任何插件文件中.

经过测试,可以正常工作.

Tested and works.

如果您已启用Ajax,则可用于存档页面上的NON可变产品(例如商店页面或产品类别页面),并且想要获得此直播活动,也应该添加以下内容:

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:

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文件或任何插件文件中.

经过测试,可以正常工作.

Tested and works.

这篇关于重新标记“添加到购物车"在WooCommerce中添加到购物车后的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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