public function recalculate () {
//$post = JRequest::get('request');
// echo '<pre>'.print_r($post,1).'</pre>';
jimport ('joomla.utilities.arrayhelper');
$virtuemart_product_idArray = JRequest::getVar ('virtuemart_product_id', array()); //is sanitized then
// -->> 06.02.2018 Хак Картинки дочек
$virtuemart_product_parentProduct = JRequest::getVar('virtuemart_product_id','');
$virtuemart_product_childProduct = JRequest::getVar('child_product_id','');
// --<< 06.02.2018 Хак Картинки дочек
if(is_array($virtuemart_product_idArray)){
JArrayHelper::toInteger ($virtuemart_product_idArray);
$virtuemart_product_id = $virtuemart_product_idArray[0];
} else {
$virtuemart_product_id = $virtuemart_product_idArray;
}
$customPrices = array();
$customVariants = JRequest::getVar ('customPrice', array()); //is sanitized then
//echo '<pre>'.print_r($customVariants,1).'</pre>';
//MarkerVarMods
foreach ($customVariants as $customVariant) {
//foreach ($customVariant as $selected => $priceVariant) {
//In this case it is NOT $selected => $variant, because we get it that way from the form
foreach ($customVariant as $priceVariant => $selected) {
//Important! sanitize array to int
$selected = (int)$selected;
$customPrices[$selected] = $priceVariant;
}
}
$quantityArray = JRequest::getVar ('quantity', array()); //is sanitized then
JArrayHelper::toInteger ($quantityArray);
$quantity = 1;
if (!empty($quantityArray[0])) {
$quantity = $quantityArray[0];
}
$product_model = VmModel::getModel ('product');
//VmConfig::$echoDebug = TRUE;
$prices = $product_model->getPrice ($virtuemart_product_id, $customPrices, $quantity);
$priceFormated = array();
if (!class_exists ('CurrencyDisplay')) {
require(JPATH_VM_ADMINISTRATOR . DS . 'helpers' . DS . 'currencydisplay.php');
}
$currency = CurrencyDisplay::getInstance ();
foreach ($prices as $name => $product_price) {
// echo 'Price is '.print_r($name,1).'<br />';
if ($name != 'costPrice') {
$priceFormated[$name] = $currency->createPriceDiv ($name, '', $prices, TRUE);
}
}
// -->> 06.02.2018 Хак Картинки дочек
if($virtuemart_product_childProduct != '') {
$db = JFactory::getDBO();
$q = 'SELECT M.`virtuemart_media_id` FROM `#__virtuemart_product_medias` as M '
. ' WHERE M.`virtuemart_product_id` ='.(int)$virtuemart_product_childProduct . ' LIMIT 1';
$db->setQuery($q);
$resultImgChild = $db->loadObjectList();
$childProduct_media_id =$resultImgChild[0]->virtuemart_media_id;
if($childProduct_media_id != null) {
// получить картинку по id медиа файле (изображение)
$mediaModel = VmModel::getModel ('media'); // Подключаете модель медиа (Если не подключен)
$image = $mediaModel->createMediaByIds($childProduct_media_id);
$priceFormated['childProductMediaId'] = $image[0]->file_url;
} else {
$db = JFactory::getDBO();
$q = 'SELECT M.`virtuemart_media_id` FROM `#__virtuemart_product_medias` as M '
. ' WHERE M.`virtuemart_product_id` ='.(int)$virtuemart_product_parentProduct[0] . ' LIMIT 1';
$db->setQuery($q);
$resultImgChild = $db->loadObjectList();
$childProduct_media_id =$resultImgChild[0]->virtuemart_media_id;
$mediaModel = VmModel::getModel ('media'); // Подключаете модель медиа (Если не подключен)
$image = $mediaModel->createMediaByIds($childProduct_media_id);
$priceFormated['childProductMediaId'] = $image[0]->file_url;
}
}
// --<< 06.02.2018 Хак Картинки дочек
// Get the document object.
$document = JFactory::getDocument ();
// stAn: setName works in JDocumentHTML and not JDocumentRAW
if (method_exists($document, 'setName')){
$document->setName ('recalculate');
}
JResponse::setHeader ('Cache-Control', 'no-cache, must-revalidate');
JResponse::setHeader ('Expires', 'Mon, 6 Jul 2000 10:00:00 GMT');
// Set the MIME type for JSON output.
$document->setMimeEncoding ('application/json');
JResponse::setHeader ('Content-Disposition', 'attachment;filename="recalculate.json"', TRUE);
JResponse::sendHeaders ();
echo json_encode ($priceFormated);
jexit ();
}