1

This will change the text into "Add again?" when the customer clicks "Add to cart" and I'm wondering how to change this into applying a discount each time the button is clicked.

Code:

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_add_to_cart_again' );
function woo_add_to_cart_again() {

    global $woocommerce;
    foreach($woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];

        if( get_the_ID() == $_product->id ) {
            return __('Add Again', 'woocommerce');
        }
    }
    return __('Add to cart', 'woocommerce');
}

When clicked the first time, add to cart as normal but change the button text into "Buy again & Get 5% OFF", after pressed the 2nd time, text changes into "Buy 3 and get 10% OFF" and the discount should apply to the product, not the cart.

I would like for this to be a checkbox option beside "Downloadable" in WooCommerce Admin when creating a new product. Any help is highly appreciated.

  • basically you want to check if the product in cart is equal 1 then massages will be Buy again & Get 5% OFF if 2 in cart "Buy 3 and get 10% OFF" right or you want it to add event listener on click ? and then add the discount accordingly – kashalo Oct 01 '18 at 15:59
  • As long as the button text changes based on qty in cart and that it applies the discount, either way is okay for me. –  Oct 01 '18 at 16:26

1 Answers1

0

first to change the text based on the quantity i just added an if condition to your function to check the quantity for that specific product in the cart and change the text accordingly as follow:

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_add_to_cart_again' );
function woo_add_to_cart_again() {

    global $woocommerce;
    foreach ( $woocommerce->cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        if ( $values['quantity'] == 1 && get_the_ID() == $_product->id ) {
            return __( 'Buy again & Get 5% OFF', 'woocommerce' );
        }

        if ( $values['quantity'] == 2 && get_the_ID() == $_product->id ) {
            return __( 'Buy 3 and get 10% OFF', 'woocommerce' );
        }
        if ( $values['quantity'] > 2 && get_the_ID() == $_product->id ) {
            return __( 'Add Again', 'woocommerce' );
        }
    }
    return __( 'Add to cart', 'woocommerce' );
}

Note: if you want to change the text in the archive page too you can use woocommerce_product_add_to_cart_text hook and added it to the above code as follow:

add_filter( 'woocommerce_product_single_add_to_cart_text', 'woo_add_to_cart_again' );
add_filter( 'woocommerce_product_add_to_cart_text', 'woo_add_to_cart_again' );

Then we need to calculate the price for those products which is qualified for the discount and add the discount based on the product prices.

We are going to use woocommerce_cart_calculate_fees to insert this discount to our cart as follow :

add_action( 'woocommerce_cart_calculate_fees', 'add_discount' );


function add_discount( WC_Cart $cart ) {

    $discounts = []; //Store the disocunts in array 
    foreach ( $cart->get_cart() as $cart_item_key => $values ) {
        $_product = $values['data'];
        if ( $values['quantity'] == 2 ) {
            // Calculate the discount amount  
            $product_price = ( $_product->get_sale_price() ) ? $_product->get_sale_price() : $_product->get_regular_price();
            $price         = $product_price * 2;
            array_push( $discounts, $price * 0.05 ); //Push the discount 

        }

        if ( $values['quantity'] > 2 ) {
            // Calculate the discount amount  
            $product_price = ( $_product->get_sale_price() ) ? $_product->get_sale_price() : $_product->get_regular_price();
            $price         = $product_price * $values['quantity'];
            $discount      = $price * 0.1;
            array_push( $discounts, $price * 0.1 );//Push the discount 
        }
    }

    $cart->add_fee( 'Your Disocunt', -array_sum( $discounts ) );

}

Updated:

To Change the add_to_cart text when using Ajax actually it's already answered in here By @LoicTheAztec at this link and with little modification you can achieve your target goal as follow:

add_action( 'wp_footer', 'change_add_to_cart_jquery' );
function change_add_to_cart_jquery() {
    if ( is_shop() || is_product_category() || is_product_tag() ) : ?>
            <script type="text/javascript">
            (function ($) {
                $('a.add_to_cart_button').click(function () {
                    $this = $(this);
                    if (typeof $($this).attr('data-qty') == 'undefined') {
                        $($this).text('Buy again & Get 5% OFF');
                        $($this).attr('data-qty', 1);
                        return;
                    }
                    if ($($this).attr('data-qty') == 1) {
                        $($this).text('Buy 3 and get 10% OFF');
                        $($this).attr('data-qty', 2);
                        return
                    }
                    $($this).text('Add Again');
                });

            })(jQuery);
            </script>
        <?php
        endif;
}

of course the code above has been tested.

kashalo
  • 3,442
  • 2
  • 11
  • 28
  • Thank you! Works very well. Would be even better if the button changed on the archive without having to update (refresh) the page. But that's a minor thing. Again, thank you. How to make this into a checkbox button so that the functionality can be "activated" per product basis? As I mention in my original question: "I would like for this to be a checkbox option beside "Downloadable" in WooCommerce Admin when creating a new product." –  Oct 01 '18 at 17:54
  • @bjornen you are welcome i will think about easy solution for that give me 10 min – kashalo Oct 01 '18 at 17:57
  • sure thing. I'll wait :) Thank you for taking the time to help me. –  Oct 01 '18 at 19:27
  • @bjornen i have updated my answer sorry for delay :) – kashalo Oct 01 '18 at 20:34
  • Cool! I didn't refresh :) Thank you and thank you LoicTheAztec so much. It works fine :) –  Oct 01 '18 at 20:47
  • Only thing missing, if you know how, is to make it into a checkbox on the WooCommerce admin so that it can be activated on a product level. It's cool as hell, but having it for all products might not be ideal :) –  Oct 01 '18 at 20:48
  • @bjornen yes of course but I left my office I u want post a question and in the morning if no one answer u I will do it – kashalo Oct 01 '18 at 20:51
  • Please have a look at checkbox feature when you can @kashalo. Really appreciate it. –  Oct 02 '18 at 06:05
  • hello again kashalo, I hate to ask again (feel like I'm spamming), but do you want me to make a new question about adding this as a checkbox although it's in the original question? Please let me know. Thanks so much for the help. –  Oct 02 '18 at 11:30
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/181148/discussion-between-kashalo-and-bjornen). – kashalo Oct 02 '18 at 11:31