Появилась необходимость сделать разные комплектации товара со своими ценами (переключаются во вкладках на фронтэнде). Причем у каждой комплектации свои атрибуты, которые плюсуются к цене.
AJAX cмену цены при клике по вкладке сделать удалось:
В файле шаблона:
Ссылки вкладок:
<a href="#level-2-1" rel="product_price=100000"></a>
<a href="#level-2-1" rel="product_price=200000"></a>
javascript:
function number_format(n, d, s) {
if (arguments.length == 2) { s = " "; }
if (arguments.length == 1) { s = " "; d = "."; }
n = n.toString();
a = n.split(d);
x = a[0];
y = a[1];
z = "";
if (typeof(x)!= "undefined") {
for (i=x.length-1;i>=0;i--)
z += x.charAt(i);
z = z.replace(/(\d{3})/g, "function number_format(n, d, s) {
if (arguments.length == 2) { s = " "; }
if (arguments.length == 1) { s = " "; d = "."; }
n = n.toString();
a = n.split(d);
x = a[0];
y = a[1];
z = "";
if (typeof(x)!= "undefined") {
for (i=x.length-1;i>=0;i--)
z += x.charAt(i);
z = z.replace(/(\d{3})/g, "$1" + s);
if (z.slice(-s.length) == s)
z = z.slice(0, -s.length);
x = "";
for (i=z.length-1;i>=0;i--)
x += z.charAt(i);
if (typeof(y)!= "undefined" && y.length > 0)
x += d + y;
}
return x;
}
$(document).ready(function () {
$('.ajaxPrice').on('click', function () {
$.ajax({
type:'POST',
cache:false,
dataType:'html',
url:'/index.php?option=com_jshopping&controller=product&task=ajax_price&product_id=1',
data: $(this).attr("rel"),
format : 'raw',
success:function (data) {
data = number_format(data);
jQuery("#block_price").html(data + ' <?php print $this->config->currency_code;?>');
}
});
});
});" + s);
if (z.slice(-s.length) == s)
z = z.slice(0, -s.length);
x = "";
for (i=z.length-1;i>=0;i--)
x += z.charAt(i);
if (typeof(y)!= "undefined" && y.length > 0)
x += d + y;
}
return x;
}
$(document).ready(function () {
$('.ajaxPrice').on('click', function () {
$.ajax({
type:'POST',
cache:false,
dataType:'html',
url:'/index.php?option=com_jshopping&controller=product&task=ajax_price&product_id=1',
data: $(this).attr("rel"),
format : 'raw',
success:function (data) {
data = number_format(data);
jQuery("#block_price").html(data + ' <?php print $this->config->currency_code;?>');
}
});
});
});
Контроллер \controllers\product.php:
function display($cachable = false, $urlparams = false){
$session = JFactory::getSession();
$session->clear('product_price');
...
}
function ajax_price()
{
$product_price = JRequest::getInt('product_price');
print $product_price;
$session = JFactory::getSession();
$session->set( 'product_price', $product_price );
die();
}
В файл \tables\product.php переопределил function load для загрузки цены комплектации из сессии, которая выводится при выборе комплектации:
public function load($keys = null, $reset = true)
{
parent::load($keys, $reset);
$session = JFactory::getSession();
if ($session->get('product_price')){
$this->product_price = $session->get('product_price');
}
}
Цена при переключается, всё вроде бы пашет, в корзину заносится. Но вот как только мы выберем атрибут в одной комплектации, потом перейдем на другую, - плюсуются стоимость атрибутов обеих.
Подскажет кто где и как их обнулять? Уже мозги сломал все. Этот кусок кода например не помогает:
$product = $this->product;
$product->load($this->product_id);
$product->setAttributeActive(array());