如何在 Opencart 3.0 中按 sku 或按型号将产品添加到购物车

How to add product in to cart by sku or by model in Opencart 3.0

我想通过 SKU 或型号而不是产品 ID 将产品添加到购物车。 这是它如何在 category.twig 文件

中添加的代码
<button type="button" onclick="cart.add('{{ product.product_id }}', '{{ product.minimum }}');"><i class="fa fa-shopping-cart"></i> <span class="hidden-xs hidden-sm hidden-md">{{ button_cart }}</span></button>

这是js

// Cart add remove functions
var cart = {
    'add': function(product_id, quantity) {
        $.ajax({
            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            data: 'product_id=' + product_id + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
            dataType: 'json',
            beforeSend: function() {
                $('#cart > button').button('loading');
            },
            complete: function() {
                $('#cart > button').button('reset');
            },
            success: function(json) {
                $('.alert-dismissible, .text-danger').remove();

                if (json['redirect']) {
                    location = json['redirect'];
                }

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

                    // Need to set timeout otherwise it wont update the total
                    setTimeout(function () {
                        $('#cart > button').html('<span id="cart-total"><i class="fa fa-shopping-cart"></i> ' + json['total'] + '</span>');
                    }, 100);

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

                    $('#cart > ul').load('index.php?route=common/cart/info ul li');
                }
            },
            error: function(xhr, ajaxOptions, thrownError) {
                alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
            }
        });
    },
}

在上面的代码中,opencart 使用 product_id 将产品添加到购物车,但我想通过 sku 或模型[=20= 添加它]

common.js中派生自add的新方法(基本上是product_id 更改为 sku 并调用 addBySku)

    'addBySku': function(sku, quantity) {
        $.ajax({
            url: 'index.php?route=checkout/cart/addBySku',
            type: 'post',
            data: 'sku=' + sku + '&quantity=' + (typeof(quantity) != 'undefined' ? quantity : 1),
            //nothing else changed from 'add' method

catalog/controller/checkout/cart.php中派生自add方法。通过sku得到product_id,放入post,其他不变

    public function addBySku() {
        $this->load->language('checkout/cart');
        $this->load->model('catalog/product');

        $json = array();

        if (isset($this->request->post['sku'])) {
            $product_id = (int)$this->model_catalog_product->productIDBySku($this->request->post['sku']);   
        } else {
            $product_id = 0;
        }
        $this->request->post['product_id'] = $product_id;


        //nothing else changed from 'add' method


catalog/model/catalog/product.php。添加此方法,通过 product_id

检索 sku
    public function productIDBySku($sku) {
        $query = $this->db->query("select product_id from " . DB_PREFIX . "product where sku = '" . $this->db->escape($sku) . "'");
        return $query->row['product_id'];
    }

希望对您有所帮助。我没有测试它。 不要忘记调用正确的方法并传递 sku

<!--<button type="button" onclick="cart.add('{{ product.product_id }}',-->
<button type="button" onclick="cart.addBySku('{{ product.sku }}',