4

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' );
}
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Christian
  • 41
  • 1
  • 4
  • `Is this possible?`Yes. `But i do not know how to solve the relabel thing` Show us your attempt so far? – ProEvilz Nov 24 '17 at 10:32
  • Is the cart using ajax or refreshing the page? – iSZ Nov 24 '17 at 10:35
  • right now is using refreshing – Christian Nov 24 '17 at 10:43
  • lol, really ? you tried refreshing only ? no code ? – Alice Nov 24 '17 at 11:29
  • sorry cart is using ajax. but i am trying to relabel my button on single product page not in cart. – Christian Nov 24 '17 at 11:34
  • my atttempts so far: 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' ); } – Christian Nov 24 '17 at 12:08

3 Answers3

6

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • I just tested it and it do not work in my case. Maybe because it´s a varible product? (shirt with different sizes) – Christian Nov 24 '17 at 13:42
  • 1
    @Christian I have updated my code… It's working now for variable products… try it please and let me know. – LoicTheAztec Nov 24 '17 at 13:50
  • With all the answers posted here, this work with variable products and still work as of today's date. – PlainH2O Oct 18 '21 at 05:23
  • May I know is it possible to add a css class to the relabelled button to style it differently from the default "Add to cart" button? – PlainH2O Oct 18 '21 at 05:26
0

Add a add-to-cart.php to your theme/woocommerce/loop folder. Then add the code below before the sprintf button function.

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

esc_html( ( !empty( $product_addons ) )?'Select options':$buttonText )

You might need to refine this if you switch on the Ajax cart

iSZ
  • 682
  • 5
  • 10
  • i tried something different but it dont work. i posted it into my original question. i will try your code. thank you – Christian Nov 24 '17 at 12:17
  • @Christian no problem. It might need some refining to fit your template but it should work. You should be able to apply the same logic to your filter. – iSZ Nov 24 '17 at 12:21
0

This should work

 // Part 1
    // Single Product Page Add to Cart
     
    add_filter( 'woocommerce_product_single_add_to_cart_text', 'custom_add_cart_button_single_product', 9999 );
     
    function custom_add_cart_button_single_product( $label ) {
       if ( WC()->cart && ! WC()->cart->is_empty() ) {
          foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
             $product = $values['data'];
             if ( get_the_ID() == $product->get_id() ) {
                $label = 'Added to Cart';
                break;
             }
          }
       }
       return $label;
    }
     
    // Part 2
    // Loop Pages Add to Cart
     
    add_filter( 'woocommerce_product_add_to_cart_text', 'custom_add_cart_button_loop', 9999, 2 );
     
    function custom_add_cart_button_loop( $label, $product ) {
       if ( $product->get_type() == 'simple' && $product->is_purchasable() && $product->is_in_stock() ) {
          if ( WC()->cart && ! WC()->cart->is_empty() ) {
             foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
                $_product = $values['data'];
                if ( get_the_ID() == $_product->get_id() ) {
                   $label = 'Added to Cart';
                   break;
                }
             }
          }
       }
       return $label;
    }
Rawny
  • 1