如何在Opencart中硬编码购物车中的一个项目



我有一个项目网站。为了加快流程,当用户点击"购买"按钮时,我想直接将用户导航到购物车。

我创建了功能

function addToCartQuick() {
// quantity = typeof (quantity) != 'undefined' ? quantity : 1;
$.ajax({
url: 'index.php?route=checkout/cart/add',
type: 'post',
data: 'product_id=' + 50 + '&quantity=' + 1,
dataType: 'json',
beforeSend: function() {
console.log('started to add');
$('.js-btn-buy').html('Loading...');
},
success: function(json) {
console.log('added');
$('#cartlink')[0].click();
$('.success, .warning, .attention, .information, .error').remove();
// window.location.href = 'index.php?route=checkout/simplecheckout';
},
error: function(xhr, ajaxOptions, thrownError) {
alert(thrownError + "rn" + xhr.statusText + "rn" + xhr.responseText);
}
});
}

它将项目添加到购物车,并单击购物车的隐藏链接。正如您所看到的,我也尝试过window.location.href,但它比导航慢。当前的解决方案比window.location.href快一点,但仍然很慢(与只点击购物车链接相比(

有没有办法对购物车中的一个默认项目进行硬编码(当然可以提供增加/减少和删除项目的选项(?

添加在function index() {正下方的catalog/controller/checkout/simplecheckout.php中

if (!$this->cart->hasProducts()) $this->cart->add(<your_product_id>);

然后只需将"购买"按钮更改为直接链接到结账/简单结账。

最新更新