Skip to content

Instantly share code, notes, and snippets.

@gitSambhal
Created September 25, 2018 19:24
Show Gist options
  • Save gitSambhal/9a4defd5a826e6a2e70c9d312e82c778 to your computer and use it in GitHub Desktop.
Save gitSambhal/9a4defd5a826e6a2e70c9d312e82c778 to your computer and use it in GitHub Desktop.
Updated JS file for Multiple Add to Cart Woocommerce plugin.
//eecom-multiple-products-add-to-cart\templates\js/eecom_mpatc.js
//Function to open loader
function open_loader()
{
jQuery('.maskloader').show();
}
//Function to close loader
function close_loader()
{
jQuery('.maskloader').hide();
}
//-------------------------------//
//function for mouse event
function simulateClick(elId) {
var evt;
var el = document.getElementById(elId);
if (document.createEvent) {
evt = document.createEvent("MouseEvents");
evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
}
(evt) ? el.dispatchEvent(evt) : (el.click && el.click());
}
jQuery(document).ready(function() {
jQuery('.checkall').click(function() {
if(jQuery(this).is(":checked")) {
var len = document.getElementsByClassName('check').length;
var i;
for(i=0;i<(len);i++) {
document.getElementsByClassName('check')[i].checked = true;
}
} else {
var len = document.getElementsByClassName('check').length;
var i;
for(i=0;i<(len);i++) {
document.getElementsByClassName('check')[i].checked = false;
}
}
});
jQuery('li input.qty').on("click", function() {
jQuery(this).keyup(function(){
var value = jQuery(this).val();
if(value == 0 || value == '')
{
jQuery(this).next().next().prop("checked", false);
alert("Please enter atleast 1 quantity.");
}
else
{
jQuery(this).next().next().prop("checked", true);
}
});
jQuery(this).next().next().prop("checked", true);
});
jQuery('.btn-cartall').click(function() {
var error = false;
var error1 = true;
var allVals = [];
var allValsQuantity = [];
var allValsProductType = [];
jQuery('.check').each(function() {
if(jQuery(this).is(':checked')) {
var setval = jQuery(this).val();
allVals.push(jQuery(this).val());
allValsQuantity.push(jQuery('#quantity-'+setval).val());
allValsProductType.push(jQuery('#product_type-'+setval).val());
error1 = false;
}
});
if(error1 == true) {
alert("No checkbox selected !");
}
if(error == false && error1 == false) {
var homeurl = document.getElementById('home_url').value;
/*-------Check product type in array-----*/
var checktype = "variable";
if(jQuery.inArray(checktype,allValsProductType) != -1){
// the element is not in the array
var mapForm = document.createElement("form");
mapForm.target = "Selected Products List";
mapForm.method = "POST"; // or "post" if appropriate
mapForm.action = multiple_variable_ajax_url;
var mapInput = document.createElement("input");
mapInput.type = "hidden";
mapInput.name = "products_ids";
mapInput.value = allVals;
mapForm.appendChild(mapInput);
var eecomQuantityInput = document.createElement("input");
eecomQuantityInput.type = "hidden";
eecomQuantityInput.name = "eecom_products_qty";
eecomQuantityInput.value = allValsQuantity;
mapForm.appendChild(eecomQuantityInput);
document.body.appendChild(mapForm);
map = window.open("", "Selected Products List", "status=0,title=0,height=auto,width=880px,scrollbars=1,resizeable=1");
if (map) {
mapForm.submit();
} else {
alert('You must allow popups for this Selected Products List to work.');
}
setTimeout(function(){
document.getElementById('all-added-success').innerHTML = '<div class="woocommerce-message"><a class="button wc-forward" href="'+homeurl+'/cart/">View Cart</a></div>';
},10000);
} else {
// Direct add if variable product no found
var allsimprod = [];
for (var i = 0; i < allVals.length; i++ ) {
var simprod = allVals[i]+'_'+allValsQuantity[i] ;
allsimprod.push(simprod);
}
jQuery.ajax({
type: "POST",
beforeSend: function() {
jQuery('body')
.prepend('<div class="maskloader" style="display:block"></div>');
},
url: homeurl+"/wp-admin/admin-ajax.php",
data: { 'action': 'woocommerce_add_to_cart_simple','prodcontent':allsimprod},
success:function(data){
console.log(data)
jQuery('body .maskloader').css('display','none');
document.getElementById('all-added-success').innerHTML = '<div class="woocommerce-message"><a class="button wc-forward" href="'+homeurl+'/cart/">View Cart</a>Items was successfully added to your cart.</div>';
//eecom_mpatc.js
$('.cart-price').html(data['fragments']['.cart-price']);
$('.header .cart-icon').html(data['fragments']['.header .cart-icon']);
$('div.widget_shopping_cart_content').html(data['fragments']['div.widget_shopping_cart_content']);
}
});
}}
});
});
/**************** Variation Products MPATC ***********/
jQuery(document).ready(function(){
jQuery('.eecom-add-all').click(function(){
jQuery('.maskloader').attr('style','display:block;');
var eecom_error = false;
var eecom_error1 = true;
var varierror = false;
var eecom_allVals = [];
var eecom_allValsQuantity = [];
var eecom_allValsProductType = [];
var eecom_item = {};
jQuery('.eecom_check').each(function() {
if(jQuery(this).is(':checked')) {
var eecom_setval = jQuery(this).val();
eecom_allVals.push(jQuery(this).val());
eecom_allValsQuantity.push(jQuery('#eecom_quantity-'+eecom_setval).val());
eecom_allValsProductType.push(jQuery('#eecom_product_type-'+eecom_setval).val());
eecom_error1 = false;
}
});
if(eecom_error1 == true) {
jQuery('.hidingbut').trigger('click');
setTimeout(function(){ jQuery('.close').trigger('click');
close_loader();
}, 3000);
}
else{
if(eecom_error == false && eecom_error1 == false) {
var eecom_homeurl = document.getElementById('eecom_home_url').value;
var eecom_checktype = "variable";
if(jQuery.inArray(eecom_checktype,eecom_allValsProductType) != -1){
//alert('We have some variable products.');
var p = 0;
jQuery( eecom_allValsProductType ).each(function() {
//alert(eecom_allValsProductType[p]);
if(eecom_allValsProductType[p] == eecom_checktype){
var variationIdvalidation = jQuery('.item_p-'+eecom_allVals[p]+' .variation_id').val();
if(variationIdvalidation == '' || variationIdvalidation == 0){
jQuery('.hidingvari').trigger('click');
setTimeout(function(){ jQuery('.close').trigger('click');
close_loader();
}, 3000);
varierror = true ;
exit();
}
}
p++;
});
}
if(varierror == false){
var myproductlist = [];
for (var i = 0; i < eecom_allVals.length; i++ ) {
if(eecom_allValsProductType[i] == eecom_checktype){
$variation_form = jQuery( '.item_p-'+eecom_allVals[i]+' form' );
var item = '',
check = true,
variations = $variation_form.find( '[name^=attribute]' );
if($variation_form.find('select[name^=attribute]').length) {
$variation_form.find('select[name^=attribute]').each(function() {
var attribute = jQuery(this).attr("name");
var attributevalue = jQuery(this).val();
item += attribute;
item += '#'
item += attributevalue;
item += ',';
});
} else {
$variation_form.find('.select').each(function() {
var attribute = jQuery(this).attr("data-attribute-name");
var attributevalue = jQuery(this).find('.selected').attr('data-name');
item += attribute;
item += '#'
item += attributevalue;
item += ',';
});
}
var variety = item.substring(0, item.length-1);
var variationId = jQuery('.item_p-'+eecom_allVals[i]+' .variation_id').val();
var productId = eecom_allVals[i];
var productquant = eecom_allValsQuantity[i];
var producttype = "var";
var mynewproduct = producttype+'*'+productId+'*'+variationId+'*'+productquant+','+variety ;
myproductlist.push(mynewproduct);
}
else{
var productId = eecom_allVals[i];
var productquant = eecom_allValsQuantity[i];
var producttype = "sim";
var mynewproduct = producttype+'*'+productId+'*'+productquant;
myproductlist.push(mynewproduct);
}
//document.getElementById('all-added-success').innerHTML = '<div class="woocommerce-message"><a class="button wc-forward" href="'+homeurl+'/cart/">View Cart</a>Items was successfully added to your cart.</div>';
}//forloop ends
jQuery.ajax({
type: "POST",
url: eecom_homeurl+"/wp-admin/admin-ajax.php",
data: { 'action': "woocommerce_add_to_cart_variable_rc", 'mycontent': myproductlist },
success: function(data) {
close_loader();
jQuery('.hidingsuccess').trigger('click');
setTimeout(function(){ jQuery('.close').trigger('click');
window.close();
}, 2000);
}
});
}
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment