• Resolved mindthetrash

    (@mindthetrash)


    Hi guys,
    Thank you for a great plugin! Works beautifully!
    We recently added sweetalert2 to our website to show a little notification after add to cart event. It works on the shop page and on the related products in the single product page. However, it doesn’t work on the single product itself, and the variation products.

    Here’s the code we use to implement sweetalert2.

    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();
    }
    
    add_action('wp_footer', 'item_count_check');
    function item_count_check() {
        if (is_checkout())
            return;
        ?>
        <script src="https://unpkg.com/[email protected]/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: 2000
                            });
                            toast({
                                type: 'success',
                                title: 'Product Added To Cart'
                            })
                        }
                    }
                });
            });
        });
        </script>
        <?php
    }

    Can you guys assist with some pointers to how we can get it to work on the single product page while using your plugin?

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Integration with sweetalert2’ is closed to new replies.