3

I've removed the standard "Added to cart" message given by WooCommerce. I have then added the code below which is using the Sweet Alert. The idea is to remove all "added to cart" messages and to only use the sweet alert.

Based on "JS alert on ajax add to cart for specific product category count in Woocommerce" answer code by @LoicTheAztec, my code works for the first product added, but not for the following ones. So it works fine when the cart is empty and when adding the first product.

Here's the code I'm using:

// remove add to cart woocommerce message
add_filter('wc_add_to_cart_message_html', '__return_null');

// Wordpress Ajax PHP
add_action('wp_ajax_nopriv_checking_items', 'atc_sweet_message');
add_action('wp_ajax_checking_items', 'atc_sweet_message');

function atc_sweet_message() {
    if (isset($_POST['id']) && $_POST['id'] > 0) {
        $count = 0;
        $product_id = $_POST['id'];
        foreach(WC()-> cart-> get_cart() as $cart_item) {
            $count += $cart_item['quantity'];
        }
    }
    echo $count;
    die();
}

// jQuery Ajax
add_action('wp_footer', 'item_count_check');
function item_count_check() {
    if (is_checkout())
        return; 
    ?> 
    <script src="https://unpkg.com/sweetalert2@7.20.1/dist/sweetalert2.all.js"></script>
    <script type="text/javascript">
    jQuery( function($) {
        if ( typeof wc_add_to_cart_params === 'undefined' )
            return false;

        $(document.body).on( 'added_to_cart', function( event, fragments, cart_hash, $button ) {
            $.ajax({
                type: 'POST',
                url: wc_add_to_cart_params.ajax_url,
                data: {
                    'action': 'checking_items',
                    'id'    : $button.data( 'product_id' )
                },
                success: function (response) {
                    if(response == 1 ){
                        const toast = swal.mixin({
                            toast: true,
                            showConfirmButton: false,
                            timer: 3000
                        });
                        toast({
                            type: 'success',
                            title: 'Product Added To Cart'
                        })
                    }
                }
            });
        });
    });
    </script>
    <?php
}

I tried removing if(response == 1 ){ without success. Any input on this is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • @LoicTheAztec, would you mind please having a look at this since you helped with my other question? –  Feb 26 '19 at 19:57

1 Answers1

4

There was little mistakes in your code on the function for Wordpress Ajax PHP. Try the following:

// remove add to cart woocommerce message
add_filter('wc_add_to_cart_message_html', '__return_null');

// Wordpress Ajax PHP
add_action('wp_ajax_nopriv_item_added', 'addedtocart_sweet_message');
add_action('wp_ajax_item_added', 'addedtocart_sweet_message');
function addedtocart_sweet_message() {
    echo isset($_POST['id']) && $_POST['id'] > 0 ? (int) esc_attr($_POST['id']) : false;
    die();
}

// jQuery Ajax
add_action('wp_footer', 'item_count_check');
function item_count_check() {
    if (is_checkout())
        return;
    ?>
    <script src="https://unpkg.com/sweetalert2@7.20.1/dist/sweetalert2.all.js"></script>
    <script type="text/javascript">
    jQuery( function($) {
        if ( typeof wc_add_to_cart_params === 'undefined' )
            return false;

        $(document.body).on( 'added_to_cart', function( event, fragments, cart_hash, $button ) {
            var $pid = $button.data('product_id');

            $.ajax({
                type: 'POST',
                url: wc_add_to_cart_params.ajax_url,
                data: {
                    'action': 'item_added',
                    'id'    : $pid
                },
                success: function (response) {
                    if(response == $pid){
                        const toast = swal.mixin({
                            toast: true,
                            showConfirmButton: false,
                            timer: 3000
                        });
                        toast({
                            type: 'success',
                            title: 'Product Added To Cart'
                        })
                    }
                }
            });
        });
    });
    </script>
    <?php
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

It works now for all ajax added to cart items (and not just the first one).

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • thank you! only one "problem", if it can be called a "problem", the page "reloads" and "flickers" when adding a product to the cart if on the the product page. Also, if on the product page, the message is not shown. Other than that, it works perfectly. –  Feb 27 '19 at 20:56
  • @FreddieMcCollough The code is made to work with the "Ajax add to cart button" so it can't work on single product pages… When the product is added to cart, the page is reloaded, and there is not `added_to_cart` jQuery event active as this is PHP only. – LoicTheAztec Feb 27 '19 at 21:03
  • okay, I understand. I tried adding a if statement for the `add_filter('wc_add_to_cart_message_html', '__return_null');` filter making it only run on the shop (if ! is_product) but that did not work. –  Feb 27 '19 at 21:12
  • @FreddieMcCollough But remember that in the product single pages there are the related products and the upsells products that can have ajax add to cart buttons (if simple products). – LoicTheAztec Feb 27 '19 at 21:16