Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

java script

var cVariant = {
    cname: '',
    var: '',
};

$(function(){
	// On any cart field change
	$('body').on('cartchange', cartChange);

	// On ajax response
	$('body').on('cartupdate', cartUpdate);

    $(document).ready(function(){
        var selectedTarif = $('.vv-tarif[data-excluded="0"]');

        if (selectedTarif.length) {
            cVariant.cname = selectedTarif.data('cname');
            cVariant.var = selectedTarif.data('var');
        }

        variantEvents();

        formInit();

        $('.c-checkbox').styler();
    });
});

function hasTarifs() {
    return ($('.vv-tarif').length > 0);
}

function variantEvents()
{
    if (!hasTarifs()) {
        return false;
    }
    $('.vv-tarif').on('click', function() {
        $('.vv-tarif').removeClass('select').data('excluded', 1);
        $(this).addClass('select').data('excluded', 0);

        cVariant.cname = $(this).data('cname');
        cVariant.var = $(this).data('var');

        Utils.cookies(cVariant.cname, cVariant.var);

        window.dataLayer = window.dataLayer || [];
		window.dataLayer.push({
            'event': 'autoEvent',
            'eventParams': {
                'eventCategory': 'Licenses',
                'eventAction': 'Click',
                'eventLabel': $(this).data('countpc') + ' PC',
                'eventValue': null
            }
        });

        $('body').trigger('cartchange');
    });
}

function cartChange() {
	Cart.updateCartFields();
};

function cartUpdate(e, response) {
    if (response.errorRedirect) {
        window.location.href = response.errorRedirect;
    }

    $('#priceTotal').html(response.totalText);
    $('#priceTotalBase').html(response.totalSavedText);

	if (response.productList) {
		$('#j-product-list').html(response.productList);
		Cart.findFields();
		$('.c-checkbox').styler();

		variantEvents();
	}

	checkTaxField();
}

function formInit() {
	var lastData;
    
	Paddle.Setup({
		vendor: Cart.pageData.Store.vendor,
		eventCallback: function(eventData) {
			console.log(eventData);

            if (Cart.logPaddleError) {
                Cart.logPaddleError(eventData);
            }

            focusPocus();
		}
	});

	var data = {
		width: 455,
		height: 450,
		success: function(data) {
			document.location.href = Cart.pageData.routes.last;
		}
	};

	lastData = $.extend({}, Cart.pageData.Store.data, data);
	PaddleCart.go(lastData);

	checkTaxField();

	// On ajax response
	$('body').on('cartupdate', function(e, response) {
        if (response.errorRedirect) {
            window.location.href = response.errorRedirect;
        }

		$('#paymentsystem-container').html('');
		lastData = $.extend({}, response.storeData, data);
		PaddleCart.go(lastData);
	});
}

function checkTaxField() {
	var taxField = $('.paddle-tax-field');
	var taxString = '';
	var priceRegex = /[^0-9.,s]+/i;
	if (taxField.length) {
		Paddle.Product.Prices(taxField.data('product'), function(prices) {
			console.log(prices);
			if (prices.price.tax_included) {
				taxString = decodeURI(taxField.data('sign')) + prices.price.tax.replace(priceRegex, '');
				taxField.html(taxString);
			}
		});
	}
}

/**
 * fix autofocus on iframe fields
 */
function focusPocus() {
	setTimeout(function(){
    	$(window).focus();
    }, 100);
}
Source by store.outbyte.com #
 
PREVIOUS NEXT
Tagged: #java #script
ADD COMMENT
Topic
Name
6+3 =