Created
          July 5, 2017 20:08 
        
      - 
      
- 
        Save Yelakelly/11436905db057515cd7ba600763a8197 to your computer and use it in GitHub Desktop. 
    Opencart - add related to products without related
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public function demo(){ | |
| $this->load->model('catalog/product'); | |
| $this->load->model('catalog/category'); | |
| $empty = $this->model_catalog_product->setRelated(); | |
| foreach($empty as $item){ | |
| $product_id = $item['product_id']; | |
| $product_categories = $this->model_catalog_product->getCategories($product_id); | |
| $related_products = array(); | |
| foreach ($product_categories as $category_id) { | |
| $category_id = $category_id["category_id"]; | |
| $category_products = $this->model_catalog_product->getProducts(array( | |
| 'filter_category_id' => $category_id | |
| )); | |
| $related_products = array_merge($category_products); | |
| } | |
| $related_products = array_filter($related_products, function ($item) use ($product_id) { | |
| return !(intval($item['product_id']) == intval($product_id)); | |
| }); | |
| shuffle($related_products); | |
| $related_products = array_slice($related_products, 0, 10); | |
| foreach ($related_products as $related_product) { | |
| $related_id = $related_product['product_id']; | |
| $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$product_id . "' AND related_id = '" . (int)$related_id . "'"); | |
| $this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$product_id . "', related_id = '" . (int)$related_id . "'"); | |
| $this->db->query("DELETE FROM " . DB_PREFIX . "product_related WHERE product_id = '" . (int)$related_id . "' AND related_id = '" . (int)$product_id . "'"); | |
| $this->db->query("INSERT INTO " . DB_PREFIX . "product_related SET product_id = '" . (int)$related_id . "', related_id = '" . (int)$product_id . "'"); | |
| } | |
| } | |
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | public function setRelated(){ | |
| $query = $this->db->query("SELECT DISTINCT oc_product.product_id, oc_product_description.name, CONCAT('http://demo.ru/product/?product_id=', oc_product_description.product_id) AS url FROM oc_product | |
| INNER JOIN oc_product_description ON oc_product.product_id = oc_product_description.product_id | |
| WHERE oc_product.product_id NOT IN (SELECT product_id FROM oc_product_related) AND oc_product.manufacturer_id = 0 OR oc_product.manufacturer_id = 11; | |
| "); | |
| foreach ($query->rows as $result) { | |
| $product_data[] = array( | |
| 'product_id' => $result['product_id'], | |
| 'name' => $result['name'], | |
| 'url' => $result['url'] | |
| ); | |
| } | |
| return $product_data; | |
| } | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment