-
-
Save manidip/a402c6ab0b21258f5613f017667aca59 to your computer and use it in GitHub Desktop.
Custom Add to cart WooCommerce, Refresh fragments data, Refresh cart total items, Get product variation
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
| product_add_to_cart : function(){ | |
| var ajx_data = {}; | |
| var ajx_grab = {}; | |
| $(document).on('change','.single-product .variations_form select',function(e){ | |
| var $this = $(this); | |
| var pro_id = $(".single-product .variations_form").attr('data-product_id'); | |
| var attribute_name = $this.attr('data-attribute_name'); | |
| var attribute_value = $this.val(); | |
| var post_ajxurl = window.location+"?wc-ajax=get_variation"; | |
| get_variation_product(pro_id,attribute_name,attribute_value,post_ajxurl); | |
| }); | |
| $(".input-text.qty.text").on("change",function(event){ | |
| ajx_grab['pro_quantity'] = parseInt($(this).val()); | |
| }); | |
| function get_variation_product(pro_id,attribute_name,attribute_value,post_ajxurl) | |
| { | |
| if(ajx_data.product_id == pro_id) | |
| { | |
| ajx_data[attribute_name] = attribute_value; | |
| } | |
| else | |
| { | |
| ajx_data = { | |
| [attribute_name] : attribute_value, | |
| 'product_id' : pro_id | |
| } | |
| } | |
| console.log(ajx_data); | |
| $.ajax({ | |
| type: "POST", | |
| url: post_ajxurl, | |
| data: ajx_data, | |
| success: function(result,status){ | |
| if(status == "success" && result != false) | |
| { | |
| ajx_grab = result; | |
| ajx_grab['product_id'] = pro_id; | |
| ajx_grab['status_msg'] = true; | |
| $(".custom-add-to-cart-btn").removeClass("disabled"); | |
| } | |
| else | |
| { | |
| ajx_grab['status_msg'] = false; | |
| $(".custom-add-to-cart-btn").addClass("disabled"); | |
| } | |
| }, | |
| dataType: 'json' | |
| }); | |
| } | |
| $( document.body ).on('click','.woocommerce-variation-add-to-cart .single_add_to_cart_button , .btn-variation',function(e){ | |
| $form = $(this).parents('form'); | |
| $form.find("[name='variation_id']").val(ajx_grab.variation_id); | |
| $form_serialize = $form.serialize(); | |
| var pro_add = woocommerce_params.wc_ajax_url+"&"+$form_serialize; | |
| pro_add = pro_add.replace("%%endpoint%%", "add-to-cart"); | |
| $.get(pro_add, function(data, status){ | |
| console.log("\nStatus: " + status); | |
| if(status == "success") | |
| { | |
| var url = woocommerce_params.wc_ajax_url; | |
| url = url.replace("%%endpoint%%", "get_refreshed_fragments"); | |
| $.post(url, function(data, status){ | |
| //$('.woocommerce.widget_shopping_cart').html(data.fragments['div.widget_shopping_cart_content']); | |
| if ( data.fragments ) | |
| { | |
| jQuery.each(data.fragments, function(key, value){ | |
| jQuery(key).replaceWith(value); | |
| }); | |
| } | |
| jQuery('body').trigger( 'wc_fragments_refreshed' ); | |
| }); | |
| } | |
| }); | |
| return false; | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment