2

我想禁用通过 opencart 2.x 中的 ajax 将产品添加到购物车超出其库存限制,现在 opencart 仅在标题上显示一条消息“标有 *** 的产品没有所需的数量或没有库存!”。但是我希望如果订购的产品多于库存产品,则不要将产品添加到购物车中,现在 openacart 的东西很耗时,而且不会提前让买家一次又一次地进行更改,

我试过但不确定我应该在哪里进行更改,无论是从目录/控制器/api/cart.php还是common.js或system/lirary/cart.php开始,我试试这个代码-

     if ((int)$qty && ((int)$qty > 0)) {
        if( ($this->session->data['cart'][$key])==(int)$product['stock']){

        }
        else{
            if (!isset($this->session->data['cart'][$key])) {
            $this->session->data['cart'][$key] = (int)$qty;
        } else {
            $this->session->data['cart'][$key] += (int)$qty;
        }
        }

    }
4

2 回答 2

2

如果我理解您的问题,您是否想防止客户在购物车中添加的数量超过剩余库存?

编辑 我在签入 OpenCart 2.0.2.0 后更新了代码。

OK 所以首先在你的controller/checkout/cart.php前行

if ($json)

add()函数中。

您需要以下内容:

$quantity_in_cart = 0;

$products = $this->cart->getProducts();
foreach ($products as $product) {
    if ($product['product_id'] == $product_id) {
        $quantity_in_cart = $product['quantity'];
        break;
    }
}

if (($quantity + (int)$quantity_in_cart) > $product_info['quantity']) {
    $json['error']['stock'] = $this->language->get('error_not_enough_stock');
}

然后,product.tpl如果添加失败,您需要将代码添加到 Javascript 以显示错误。它应该放在函数中

$('#button-cart').on('click', function() {

行后...

if (json['error']['recurring']) {
    $('select[name=\'recurring_id\']').after('<div class="text-danger">' + json['error']['recurring'] + '</div>');
}

添加的代码是...

if (json['error']['stock']) {
    $('.breadcrumb').after('<div class="alert alert-danger">' + json['error']['stock'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');
}

当然,您必须将类似的代码添加到将产品添加到购物车的任何其他 tpl,当您拥有带有选项的产品时,事情会变得更加复杂,但这是它的基础。

于 2015-10-12T10:09:51.837 回答
1

Opencart 使用此控制器/功能
目录 > 控制器 > 结帐 > cart.php
功能将产品添加/更新到购物车 - 添加/编辑

所以你必须为这些函数添加条件

在添加功能代码之前添加它

        if (!$json) {

在添加功能

        if($quantity > $product_info['quantity'])
            $json['warning'] = $this->language->get('error_stock');

它将检查客户添加的数量是否不超过可用数量。

然后在目录 > 视图 > 主题 > 你的主题(我的默认) > 产品 > product.tpl 文件中添加错误的 js 代码

else if(json['warning']) {
    $('#content').parent().before('<div class="alert alert-danger"><i class="fa fa-check-circle"></i> ' + json['warning'] + '<button type="button" class="close" data-dismiss="alert">&times;</button></div>');

    $('html, body').animate({ scrollTop: 0 }, 'slow');
  }

在这个 ajax 的最后一行

$('#button-cart').on('click', function() {

Bazinga,你准备好了,产品不会添加到购物车:)

注意 - 请使用 vqmod/ocmod。

于 2015-10-12T12:41:24.930 回答