在对为JoomShopping组件编写的几乎每一行代码进行了一番努力之后,我相信我已经找到了解决我所有问题的答案。
当激活购物清单中的"购买"按钮并点击后,它使用以下链接语法将产品发布到结帐购物车:
index.php/cart/add?category_id=2&product_id=12&quantity=4
其中2为类别ID, 12为产品ID等…这是由V.Vachev解决的,但我认为在它工作时发布所有完成/固定的oced是谨慎的:
$('.checkOut').live('click',function(){
var products= new Array();
$(".jshop_prod_cart").each(function(){
var product = new Object();
product.catid = $(this).find('[name="category_id"]').val();
product.id = $(this).find('input[name="product_id"]').val();
product.qanty = $(this).find('input[name^="quantity"]').val();
products.push(product)
$.ajax({
type: 'GET',
url: "shop-portal/add?category_id="+products[0].catid+"&product_id="+products[0].id+"&quantity="+products[0].qanty,
dataType: 'json',
})
})
})
这回报:
http://www.domain.com/index.php/shop-portal/add?category_id=2& product_id = 48,数量= 4
但是它只返回1,而且我有多个动态条目,它们都需要被捕获。
我正在研究这个,似乎我需要以某种方式缓存这个信息…什么好主意吗?
URL传递错误。应该是这样的:
url: "/shop-portal/add?category_id="+catid+"&product_id="+id+"&quantity="+qanty,
现在我看到你有一个具有相同名称的数组("catid", "quantity"…)。您最终想要发送数组中的值吗?因为这是另一回事。确保"catid"、"id"answers"quantity"是全局变量,并发送所需的值。
Joomla可能不期望JSON数据,尝试使用原生ajax请求
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("Sent!");
}
}
xmlhttp.open("GET","/shop-portal/add?category_id="+yourvariable+"&product_id="+yourvariable+"&quantity="+yourvariable);
xmlhttp.send();
我不确定你想发送什么值。照顾好它们(yourvariable1, yourvariable2…)
现在我看到您想要传递数组中的值。试试
$.ajax({
type: 'GET',
data:products,
url: "/shop-portal/add?category_id="+products[0].catid+"&product_id="+products[0].id+"&quantity="+products[0].qanty,
dataType: 'json',
})
})
此请求将只发送第一个产品的值。Products是一个数组,所以你可能必须循环遍历它并发送多个请求才能发送所有内容。
你可以用console.log(products)检查var "products"包含什么,这将在控制台中显示内容(例如Firebug)。
$('.checkOut').live('click',function(){
var products= new Array();
$(".jshop_prod_cart").each(function(){
var product = new Object();
product.catid = $(this).find('[name="category_id"]').val();
product.id = $(this).find('input[name="product_id"]').val();
product.qanty = $(this).find('input[name^="quantity"]').val();
products.push(product)
$.ajax({
type: 'GET',
data:products,
url: "/shop-portal/add?category_id="+product.catid+"&product_id="+product.id+"&quantity="+product.qanty,
dataType: 'json',
})
})
});