public static function renderAjaxAddToCart()
{
$paramsC = PhocacartUtils::getComponentParameters();
$add_cart_method = $paramsC->get('add_cart_method', 0);
// We need to refresh checkout site when AJAX used for removing or adding products to cart
$task = 'checkout.add';
$class = '.phItemCartBox';
$cView = PhocacartUtils::isView('checkout') ? 1 : 0;
// POS
$isPOS = false;
if (PhocacartUtils::isView('pos')) {
$task = 'pos.add';
$add_cart_method = 1;
// POS has always 1 (ajax and no popup)
$cView = 0;
$class = '.phPosCartBox';
$isPOS = true;
}
if ($add_cart_method == 0) {
return false;
}
if ($add_cart_method > 0) {
$urlAjax = Uri::base(true) . '/index.php?option=com_phocacart&task=' . $task . '&format=json&' . Session::getFormToken() . '=1&checkoutview=' . (int) $cView;
$s = array();
$s[] = 'function phDoSubmitFormAddToCart(sFormData) {';
$s[] = ' var phUrl = "' . $urlAjax . '";';
$s[] = ' var phOptions = [];';
$s[] = ' phOptions["view"] = ' . (int) $cView . ';';
$s[] = ' phOptions["method"] = ' . (int) $add_cart_method . ';';
$s[] = ' phOptions["task"] = "add";';
$s[] = ' phOptions["type"] = "cart";';
$s[] = ' phOptions["class"] = "' . $class . '";';
$s[] = $isPOS === true ? ' phOptions["pos"] = 1;' : ' phOptions["pos"] = 0;';
$s[] = ' phDoRequestMethods(phUrl, sFormData, phOptions);';
$s[] = '}';
$s[] = ' ';
Factory::getDocument()->addScriptDeclaration(implode("\n", $s));
/*$s[] = ' phRequest = jQuery.ajax({';
$s[] = ' type: "POST",';
$s[] = ' url: phUrl,';
$s[] = ' async: "false",';
$s[] = ' cache: "false",';
$s[] = ' data: sFormData,';
$s[] = ' dataType:"JSON",';
$s[] = ' success: function(data){';
$s[] = ' if (data.status == 1){';
$s[] = ' jQuery("'.$class.'").html(data.item);';
$s[] = ' jQuery("'.$class.'Count").html(data.count);';
$s[] = ' jQuery("'.$class.'Total").html(data.total); ';
// POS update message box (clean) and input box (when product added or changed - shipping and payment method must be cleaned)
if (PhocacartUtils::isView('pos')) {
$s[] = ' var phUrlPos = phAddSuffixToUrl(window.location.href, \'format=raw\');';
$s[] = ' var phDataInput = phPosCurrentData("main.input");';
$s[] = ' phDoSubmitFormUpdateInputBox(phDataInput, phUrlPos);';// refresh input box
$s[] = ' jQuery(".ph-pos-message-box").html(data.message);';// clear message box
$s[] = ' phPosManagePage();';
}
if ($add_cart_method == 2) {
$s[] = ' jQuery("body").append(jQuery("#phContainer"));';
$s[] = ' jQuery("#phContainer").html(data.popup);';
$s[] = ' jQuery("#phAddToCartPopup").modal();';
}
if ($add_cart_method == 1) {
// If no popup is displayed we can relaod the page when we are in comparison page
// If popup, this will be done when clicking continue or comparison list
$s[] = ' if (phCheckoutView == 1) {';
$s[] = ' setTimeout(function() {location.reload();}, 0001);';
$s[] = ' }';
}
$s[] = ' } else if (data.status == 0){';
if ($add_cart_method != 2) {
$s[] = ' jQuery(".phItemCartBox").html(data.error);';
}
if ($add_cart_method == 2) {
$s[] = ' jQuery("body").append(jQuery("#phContainer"));';
$s[] = ' jQuery("#phContainer").html(data.popup);';
$s[] = ' jQuery("#phAddToCartPopup").modal();';
}
if ($add_cart_method == 1) {
// If no popup is displayed we can relaod the page when we are in comparison page
// If popup, this will be done when clicking continue or comparison list
$s[] = ' if (phCheckoutView == 1) {';
$s[] = ' setTimeout(function() {location.reload();}, 0001);';
$s[] = ' }';
}
// POS update message box (clean) and input box (when product added or changed - shipping and payment method must be cleaned)
if (PhocacartUtils::isView('pos')) {
//$s[] = ' var phUrlPos = phAddSuffixToUrl(window.location.href, \'format=raw\');';
//$s[] = ' var phDataInput = phPosCurrentData("main.input");';
//$s[] = ' phDoSubmitFormUpdateInputBox(phDataInput, phUrlPos);';// refresh input box
$s[] = ' jQuery(".ph-pos-message-box").html(data.error);';// clear message box
$s[] = ' phPosManagePage();';
}
$s[] = ' } else {';
//$s[] = ' // Don\'t change the price box';
$s[] = ' }';
$s[] = ' },';
//$s[] = ' error: function(data){}';
$s[] = ' })';
$s[] = ' return false;';
$s[] = '}';
$s[] = ' ';
// :: EVENT (CLICK) Category/Items View (icon/button - ajax/standard)
//$s[] = 'function phEventClickFormAddToCart(phFormId) {';
//$s[] = ' var phForm = \'#\' + phFormId;';
//$s[] = ' var sForm = jQuery(this).closest("form");';// Find in which form the right button was clicked
//$s[] = ' var sFormData = jQuery(phForm).serialize();';
//$s[] = ' phDoSubmitFormAddToCart(sFormData);';
//$s[] = '}';
// Set it onclick as it is used in even not ajax submitting
//$s[] = 'function phEventClickFormAddToCart(phFormId) {';
//$s[] = ' var phForm = \'#\' + phFormId;';
//$s[] = ' jQuery(\'phFormId\').find(\':submit\').click();"';
//$s[] = ' return false;';
//$s[] = '}';
$s[] = ' ';
// :: EVENT (SUBMIT) Item View
$s[] = 'jQuery(document).ready(function(){';
//$s[] = ' jQuery(".phItemCartBoxForm").on(\'submit\', function (e) {';// Not working when form is added by ajax
$s[] = ' jQuery(document).on("submit", "form.phItemCartBoxForm", function (e) {';// Works with forms added by ajax
$s[] = ' e.preventDefault();';
$s[] = ' var sFormData = jQuery(this).serialize();';
$s[] = ' phDoSubmitFormAddToCart(sFormData);';
$s[] = ' })';
$s[] = '})';
$s[] = ' ';*/
}
}