2016-09-16 65 views
1

我正在使用Opencart 2.0.3.1。我有兩個製造商。每個有兩個產品。我應該可以將第一家制造商的兩種產品添加到購物車中,當我嘗試從第二家制造商添加產品時,不應允許添加。如何將單個製造商產品添加到購物車每個訂單 - Opencart 2.0.3.1

它應該告訴客戶單獨訂購。

我能夠實現使用清晰的功能添加單個項目到購物車。但如果我們能夠爲每個訂單實現單個製造商產品,那將更有意義。

回答

1

您必須修改ControllerCheckoutCart中的add方法。 所以打開文件/catalog/controller/checkout/cart.php,找到下面的行

public function add() 

在此功能中,在大約336行這replce兩行

  $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 

您可以測試這個補丁here

+0

我已經測試過這個,即使我添加來自同一製造商的其他產品,它也會拋出相同的消息「對於此訂單,您只能添加產品」,並且在購物卡預覽中創建了undefined,請參閱此快照http:// prnt.sc/cir99c – Jahir

+0

我使用的是最新版本的OpenCart。請分享$ product的print_r [0] –

+0

好的。我使用的是2.0.3.1版本。我將分享print_r i – Jahir

相關問題