php - How can i add single Manufacturer products to cart per Order- Opencart 2.0.3.1

601

I'm using Opencart 2.0.3.1. I have two Manufacturers.Each has two products. I should be able to add two products from the first manufacturer in to the Shopping cart and when i try to add product from the second manufacturer it should not allow to add.

It should tell the customer to order it separately.

I was able to achieve to add single item to cart using clear function. But it will be more sense if we are able to achieve single Manufacturer products per order.

207

Answer

Solution:

You have to modify the add method in the ControllerCheckoutCart. So open the file /catalog/controller/checkout/cart.php and find the following line

public function add()

In this function approx at line 336 replce this two lines with

            $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'));

these lines

            //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  

You can test this patch here

People are also looking for solutions to the problem: php - How to test a method which takes a callable as argument with PhpUnit and Prophecy

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.