Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
271 views
in Technique[技术] by (71.8m points)

php - Relabel "add to cart" button after add to cart in 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.

Is this possible?

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.

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' );
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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:

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.

Tested and works.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...