如何将单个制造商产品添加到每个订单的购物车- Opencart 2.0.3.1



我使用的是Opencart 2.0.3.1。我有两个制造商。每个都有两个产物。我应该能够从第一个制造商添加两个产品到购物车中,当我试图从第二个制造商添加产品时,它不应该允许添加。

应该告诉客户单独订购。

我能够使用clear功能实现将单个商品添加到购物车中。但是,如果我们能够实现每个订单只有一个制造商的产品,那就更有意义了。

您必须修改ControllerCheckoutCart中的add方法。因此,打开文件/catalog/controller/checkout/cart.php并找到以下行

public function add()

在这个函数中大约在第336行用

替换这两行
            $this->cart->add($this->request->post['product_id'], $quantity, $option, $recurring_id);
            $json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart'));
这些行

            //BEGIN OF THE PATCH
            /*
            OTRIGINAL CODE
            $this->cart->add($this->request->post['product_id'], $quantity, $option, $recurring_id);
            $json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart'));
            */
            //control if the cart isn't empty
            $can_add_product=true;
            if ($this->cart->hasProducts()>0){
                $products = $this->cart->getProducts();
                foreach ($products as $product) {
                    $product_just_in_cart = $this->model_catalog_product->getProduct($product['product_id']);
                    $manufacturer_id_in_cart=$product_just_in_cart['manufacturer_id'];
                    $manufacturer_name_in_cart=$product_just_in_cart['manufacturer'];
                    //we just analyze only the first product
                    break;
                }    
                if ($product_info['manufacturer_id']!=$manufacturer_id_in_cart) {
                    $can_add_product=false;
                }
            }
            if ($can_add_product) { 
                $this->cart->add($this->request->post['product_id'], $quantity, $option, $recurring_id);
                $json['success'] = sprintf($this->language->get('text_success'), $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart'));
            }
            else {
                $json['success'] =sprintf('For this order you can add only products of '.$manufacturer_name_in_cart, $this->url->link('product/product', 'product_id=' . $this->request->post['product_id']), $product_info['name'], $this->url->link('checkout/cart'));
            }
            //END OF THE PATCH  

你可以在这里测试这个补丁

最新更新