我想使用拖放将产品添加到购物车。在 drop 函数中,我调用 Ajax 函数将产品添加到购物车:
该url
变量包含基于产品的结帐购物车 URL。
jQuery(function() {
jQuery(".category-products" ).accordion();
jQuery(".product-name" ).draggable({
appendTo: "body",
helper: "clone"
});
jQuery(".block-content ol" ).droppable({
activeClass: "ui-state-default",
hoverClass: "ui-state-hover",
accept: ":not(.ui-sortable-helper)",
drop: function( event, ui ) {
jQuery( this ).find( ".placeholder" ).remove();
var url = ui.draggable.attr('id');
jQuery.get(url, function(data) {
// data is the response from the server..
if(data.status == 'ERROR'){
alert(data.message);
}
console.log(data);
});
url += 'isAjax/1';
url = url.replace("checkout/cart","draggableproduct/index");
try {
jQuery.ajax( {
url : url,
dataType : 'json',
success : function(data) {
if(jQuery('.block-cart')){
jQuery('.block-cart').replaceWith(data.sidebar);
}
if(jQuery('.header .links')){
jQuery('.header .links').replaceWith(data.toplink);
}
}
});
} catch (e) {
alert(e);
}
jQuery( "<li></li>" ).text( ui.draggable.text() ).appendTo( this );
}
}).sortable({
items: "li:not(.placeholder)",
sort: function() {
// gets added unintentionally by droppable interacting with sortable
// using connectWithSortable fixes this, but doesn't allow you to customize active/hoverClass options
jQuery( this ).removeClass( "ui-state-default" );
}
});
});
并且在 indexcontroller 代码上包含:-
$this->loadLayout();
$toplink = $this->getLayout()->getBlock('top.links')->toHtml();
$sidebar_block = $this->getLayout()->getBlock('cart_sidebar');
Mage::register('referrer_url', $this->_getRefererUrl());
$sidebar = $sidebar_block->toHtml();
$response['toplink'] = $toplink;
$response['sidebar'] = $sidebar;
$this->getResponse()->setBody(Mage::helper('core')->jsonEncode($response));
return ;
该产品已添加到购物车,但 Ajax 调用未完成,因为我没有得到任何响应,并且在丢弃 1 个产品后,我无法在购物车中放入更多产品。请帮我