Last active
December 8, 2021 06:09
-
-
Save AchalJ/995d7fba71eda8d88416bdaa354dde23 to your computer and use it in GitHub Desktop.
Content Grid - Add to Cart with AJAX - WooCommerce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| .pp-add-to-cart a.loading { | |
| opacity: 0.5; | |
| pointer-events: none; | |
| } | |
| .pp-add-to-cart a.added_to_cart { | |
| margin-left: 5px; | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ;(function($) { | |
| $(function() { | |
| $('.pp-add-to-cart a.button').off('click').on('click', function(e) { | |
| if ( -1 == $(this).attr('href').search('add-to-cart') ) { | |
| return; | |
| } | |
| e.preventDefault(); e.stopPropagation(); | |
| var url = wc_add_to_cart_params.wc_ajax_url.toString().replace( '%%endpoint%%', 'add_to_cart' ); | |
| var data = { | |
| quantity: 1, | |
| product_id: $(this).parents('.pp-content-post').data('id') | |
| }; | |
| var $thisbutton = $(this); | |
| $(this).addClass('loading'); | |
| $.post(url, data, function(response) { | |
| if ( ! response ) { | |
| return; | |
| } | |
| if ( response.error && response.product_url ) { | |
| window.location = location.href + '?add-to-cart=' + data.product_id | |
| return; | |
| } | |
| $( document.body ).trigger( 'added_to_cart', [ response.fragments, response.cart_hash, $thisbutton ] ); | |
| }); | |
| }); | |
| $(document).on('added_to_cart', function( e, fragments, cart_hash, $button ) { | |
| $button.removeClass( 'loading' ); | |
| $button.html( 'Toegevoegd aan winkelwagen' ); | |
| }); | |
| }); | |
| })(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment