![](/img/trans.png)
[英]Create a single JS function for all products in magento for add to cart using Ajax method
[英]How to use ajax add to cart for related products magento
我正在嘗試將ajax添加到相關產品的購物車中,但我不知道如何獲得與每個相關產品的鏈接?
單擊“添加到購物車”時,我想使用ajax而不重定向到購物車頁面。 對於主要產品,此方法運行良好。 但是因為相關是添加主要產品或添加相關產品,但是將我重定向到購物車。
<?php endif; ?>
<a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>" class="product-image">
<img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->constrainOnly(FALSE)->keepAspectRatio(TRUE)->keepFrame(FALSE)->resize(350); ?>" alt="<?php echo $this->stripTags($this->getImageLabel($_product, 'small_image'), null, true) ?>"/>
</a>
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($_product) ?>')"><span><span><?php echo $this->__('Add to Cart') ?></span></span></button>
我可以用嗎
productAddToCartForm.submit(this)
但是里面傳遞什么呢?
這是添加到購物車的代碼
<script type="text/javascript">
//<![CDATA[
var productAddToCartForm = new VarienForm('product_addtocart_form');
productAddToCartForm.submit = function(button, url) {
if (this.validator.validate()) {
var form = this.form;
var oldUrl = form.action;
if (url) {
form.action = url;
}
var e = null;
if (!url) {
url = jQuery('#product_addtocart_form').attr('action');
}
url = url.replace("checkout/cart","ajaxcart/index"); // New Code
var data = jQuery('#product_addtocart_form').serialize();
data += '&isAjax=1';
jQuery('#loading-mask').show();
try {
jQuery.ajax({
url : url,
dataType : 'json',
type : 'post',
data : data,
success : function(data) {
jQuery('#loading-mask').hide();
if(data.status == 'ERROR'){
alert(data.message);
}else{
if(jQuery('.top-links .right-links')){
jQuery('.top-links .right-links').replaceWith(data.toplink);
}
jQuery('#after-loading-success-message').show();
}
}
});
} catch (e) {
}
this.form.action = oldUrl;
if (e) {
throw e;
}
}
}.bind(productAddToCartForm);
//]]>
</script>
您可以序列化產品表單中的數據,並通過AJAX請求( 通過Prototype | 通過jQuery | 通過普通JS )將其發送到服務器。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.