1

I am learning how to use the 'added_to_cart' javascript event so that I can display added product information through javascript alert (e.g. SweetAlert2).

I am using Get specific WooCommerce product data on "added_to_cart" javascript event answer code (just removed from jQuery the bianoTrack() function) in my WooCommerce test website, setup with 3 products (i.e. 2 simple and 1 variation).

In my Chrome developer JS console, here is what I get:

Image

I don't see any product information.

Does anyone know why the 'added_to_cart' javascript event is not able to pickup the product information?

I have enabled AJAX for Add to Cart button.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
chubby
  • 35
  • 7
  • Hi @LoicTheAztec , thank you for your reply. I have been checking your posts on WooCommerce and it has been wonderful. I wish to know if there is something that I might have missed in my setup. Here's my setup: - WordPress 5.5.1 - WooCommerce 4.5.2 Is there something that I should be checking on? Thank you. – chubby Sep 30 '20 at 07:20
  • Sorry but the jQuery code works perfectly in last WooCommerce version, see [this fresh screenshot](https://i.stack.imgur.com/0ZB8I.png) … There is something else that is making trouble in your case, and I can't guess what it is, sorry. – LoicTheAztec Sep 30 '20 at 07:30
  • Thanks @LoicTheAztec, I will try on another setup to see what is the issue. Appreciate for your help! Thank you. – chubby Sep 30 '20 at 07:35
  • Is there a way to have it work on the Single Product Page instead? – chubby Sep 30 '20 at 07:45
  • As on normal add to cart (on single product pages), the page is reloaded when you add a product to cart, you can't use "added_to_cart" event as it's only for Ajax add to cart. – LoicTheAztec Sep 30 '20 at 07:56
  • I am using Astra theme, and in the Appearance > Customize > WooCommerce > Single Product, I have checked the 'Enable AJAX Add to Cart'. Also, in the WooCommerce Settings, I have checked 'Enable AJAX add to cart buttons on archives', while leaving 'Redirect to the cart page after successful addition' unchecked. As such, in the single product page, it seems to be loading through AJAX. Still wondering why is your script unable to run within Single Product page... – chubby Sep 30 '20 at 08:01
  • I repeat, Ajax add tto cart is only enabled on product loops in WooCommerce. Now for normal add to cart there is another way to make it work. Please ask a new question asking "Get the product data once product is added to cart in single product pages". And I will answer. – LoicTheAztec Sep 30 '20 at 08:05
  • Ok I have posted the question. – chubby Sep 30 '20 at 08:24
  • I don't see it… can you paste the link of your new question? – LoicTheAztec Sep 30 '20 at 08:25
  • The system says I can only post one question every 90 minutes. I will post again once the time is up :) – chubby Sep 30 '20 at 08:26
  • Ok I have posted @ https://stackoverflow.com/questions/64135155/get-the-product-data-once-product-is-added-to-cart-in-single-product-pages-with – chubby Sep 30 '20 at 09:59

1 Answers1

0

This is not for single product page normal add to cart. It's for Ajax add to cart on product loops as shop pages, archive pages, related products…

Note that products added via Ajax add to cart doesn't reload the page.

The code still works perfectly on last WooCommerce version:

enter image description here

Here is an example with a "Sweet Alert 2" displaying the product name on ajax add to cart:

// Add some product data to "add to cart" button for 'added_to_cart' js event
add_action( 'woocommerce_loop_add_to_cart_link', 'filter_wc_loop_add_to_cart_link', 10, 3 );
function filter_wc_loop_add_to_cart_link( $button_html, $product, $args ) {
    if( $product->supports( 'ajax_add_to_cart' ) ) {
        $search_string  = 'data-product_sku';

        // Insert custom product data as data tags
        $replace_string = sprintf(
            'data-product_name="%s" data-product_price="%s" data-currency="%s" %s',
            $product->get_name(), // product name
            wc_get_price_to_display( $product ), // Displayed price
            get_woocommerce_currency(), // currency
            $search_string
        );

        $button_html = str_replace($search_string, $replace_string, $button_html);
    }
    return $button_html;
}

// The jQuery code that will handle the event getting the required  product data
add_action( 'wp_footer', 'added_to_cart_js_event' );
function added_to_cart_js_event(){
    ?>
    <script src="https://unpkg.com/sweetalert2@7.20.1/dist/sweetalert2.all.js"></script>
    <script type="text/javascript">
    (function($){
        $(document.body).on('added_to_cart', function( event, fragments, cart_hash, button ) {
            var product_id    = button.data('product_id'),   // Get the product id
                product_qty   = button.data('quantity'),     // Get the quantity
                product_sku   = button.data('product_sku'),  // Get the product sku
                product_name  = button.data('product_name'), // Get the product name
                product_price = button.data('product_price'), // Get the product price
                currency      = button.data('currency');     // Get the currency

            // For testing: View all product available data on console log (to be removed)
            console.log( button.data() );

            const toast = swal.mixin({
                toast: true,
                showConfirmButton: false,
                timer: 10000
            });
            toast({
                type: 'success',
                title: 'Product "'+product_name+'" added to Cart'
            });
        });
    })(jQuery);
    </script>
    <?php
}

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

enter image description here

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Oh yes, I have just tested at my Product Catalog page, and it works! – chubby Sep 30 '20 at 07:46
  • Yes, I have accepted your answer with your clarification that it works in Product Catalog :) – chubby Sep 30 '20 at 08:03
  • It will be great if you can share how it can work in the Single Product Page which in my theme supports AJAX. – chubby Sep 30 '20 at 08:04
  • @chubby There is no Ajax for normal add to cart button in single product page as this add to cart use $_POST request... So you can get the product information via $_POST once page is reloaded or redirected. – LoicTheAztec Sep 30 '20 at 08:28
  • Thanks @LoicTheAztec, will test further on it. – chubby Sep 30 '20 at 15:52