ドラッグアンドドロップを使用してカートに商品を追加したい。ドロップ関数で、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 ;
商品はカートに追加されますが、応答がなく、1つの商品をドロップした後、カートにそれ以上商品をドロップできないため、Ajax呼び出しは完了しません。私を助けてください