Javascript 添加';加上'';减去';取代';添加到购物车';在opencart中
我想用OpenCart 2.0.1.1中的两个加号和减号按钮替换“添加到购物车”,现在我无法正确编码减号按钮。 我在Javascript 添加';加上'';减去';取代';添加到购物车';在opencart中,javascript,php,ajax,opencart,opencart2.x,Javascript,Php,Ajax,Opencart,Opencart2.x,我想用OpenCart 2.0.1.1中的两个加号和减号按钮替换“添加到购物车”,现在我无法正确编码减号按钮。 我在catalog/view/theme/*/template/module/featured.tpl中添加了plus和mius按钮,并在catalog/controller/api/cart.php中调用了plus和mius按钮,在common.js中我放置了类似于url:'index.php?route=checkout/cart/minus的url,其余代码如下所示 system
catalog/view/theme/*/template/module/featured.tpl
中添加了plus和mius按钮,并在catalog/controller/api/cart.php
中调用了plus和mius按钮,在common.js
中我放置了类似于url:'index.php?route=checkout/cart/minus
的url,其余代码如下所示
system/library/cart.php
public function minus($product_id, $qty)
{
$this->data = array();
$qnt1 = 1;
$product['product_id'] = (int)$product_id;
$key = base64_encode(serialize($product));
if ((int)$qty && ((int)$qty > 0)) {
if (!isset($this->session->data['cart'][$key])) {
$this->session->data['cart'][$key]-= (int)$qty;
}
else {
$this->remove($key);
}
}
}
[Image for plus minus button in place of "Add to cart" Button] [1]
为了减少产品数量,您需要
产品id
及其数量。要减少数量,我们需要检查数量是否大于1,如果大于1,我们可以减少数量,否则如果数量仅为1,我们需要删除整个产品
您需要更改的是视图页面中的加号、减号图标,然后是控制器,然后是库,然后将ajax结果发送回。我会尽量让这件事变得简单
让我们从查看页面开始,在我的例子中,它是products.tpl
我为获取加减按钮而编写的代码是
<table class="table scroll">
<tbody>
<?php foreach ($products as $product) { ?>
<tr >
<td >
<input type="text" style="width: 20px; height: 20px;font-weight:700 " disabled="true" value="<?php echo $product['quantity']; ?>" class="text-center">
</td>
<td class="text-left">
<a href="<?php echo $product['href']; ?>">
<b><?php echo $product['name']; ?></b>
</a>
</td>
<td style=" text-align:right">
<i class="fa fa-minus-circle fa-2x" style="cursor: pointer;color:red;" onclick="cart.remove('<?php echo $product['key']; ?>');"></i>
</td>
</tr>
<tr>
<td colspan="2" style="border:0 none;">
<div class="btn-group form-inline" role="group">
<button type="button" style="height:25px;width:25px" class="btn btn-default btn-xs " onclick="cart.decrement('<?php echo $product['product_id']; ?>');">
<i class="fa fa-minus"></i>
</button>
<button type="button" style="height:25px;width:25px" class="btn btn-default btn-xs" onclick="cart.add('<?php echo $product['product_id']; ?>');">
<i class="fa fa-plus"></i>
</button>
</div>
</td>
<td style="border:0 none;" class="text-right" >
<b><?php echo $product['total']; ?></b>
</td>
</tr>
<?php } ?>
现在您是否注意到我们正在调用库函数通过传递qty和1来减少产品数量
。这里的键只不过是ajax参数,它是product\u id
现在是库中的最后一个函数
public function decrement_product_quantity($product_id, $qty = 1){
$this->data = array();
$product['product_id'] = (int)$product_id;
$key = base64_encode(serialize($product));
if ((int)$qty && ((int)$qty > 0)) {
if ($this->session->data['cart'][$key]>1) {
$this->session->data['cart'][$key] -= (int)$qty;
} else {
$this->remove($key);
}
}
}
这一个检查车,如果数量大于1,它将减少,否则删除整个产品。
希望您能理解,如果您有任何疑问,请告诉我。也希望你能为增量做些什么。祝你好运这里的问题是标准opencart,因为2.0版本忽略了索引长度超过64个字符的post字段(在某些系统上,至少对我的客户而言) 因此,如果产品没有选项,则post请求会收到字段数量[index],因为索引小于64个字符。但是如果产品有选项,则索引大于64个字符,并且post请求中不会收到字段(无论是否通过ajax) 我的客户的提供商不想在共享服务器上改变这一点,我的解决方案是看看它在较旧版本的opencart中是如何工作的(我认为它只在程序/system/library/cart.php中使用),然后在2.0中接管它,您的问题就解决了。。旧版本中的索引较短。。因为并非所有字段都用于base64_编码 查看购物车的html代码,找到字段quantity[…]并计算索引的字符数。。对于我来说,当长度超过64个字符(例如使用了产品选项)时,post请求中不再接收该字段(通过ajax或不通过ajax)
也许当你有一个自己的服务器时,你就没有这个问题了 你得到答案了吗?还是你解决了?如果没有,那么告诉我我有这个问题的解决方案好吧,朋友,我可以给你负功能,你能对increment做同样的事情吗,n我认为increment就是在你的购物车库中调用add函数。因为代码有点长,所以我如何在加减按钮之间添加特定产品的数量文本框。在显示数量或加减按钮的地方,只需粘贴链接,就可以在产品页面和购物车项目中显示数量,加减。如果我的努力帮助您,请将我的答案标记为@Ankitagarwal抱歉,无法正确获得答案,请使用您编辑的代码,但什么都没有发生@Sudarshan@AnkitAgarwal不要只是复制我的代码,请理解逻辑,并做相应的更改,因为我不知道你在你的项目中做了什么。粘贴相同的代码让我看看
public function decrement() {
$this->load->language('checkout/cart');
$json = array();
// Remove
if (isset($this->request->post['key'])) {
$this->cart->decrement_product_quantity($this->request->post['key'],1);
unset($this->session->data['vouchers'][$this->request->post['key']]);
$this->session->data['success'] = $this->language->get('text_remove');
// rest of the code keep same}
public function decrement_product_quantity($product_id, $qty = 1){
$this->data = array();
$product['product_id'] = (int)$product_id;
$key = base64_encode(serialize($product));
if ((int)$qty && ((int)$qty > 0)) {
if ($this->session->data['cart'][$key]>1) {
$this->session->data['cart'][$key] -= (int)$qty;
} else {
$this->remove($key);
}
}
}