Products of the same category in OpenCart 2.3

We have a site on OC 2.3, it is necessary to display products from the same category as the current product on the landing page.

For the solution, we will customize the existing functionality of similar products. We go to catalog/model/catalog/product.php and find the getProductRelated function, approximately line 377. After the line

$product_data = array();

add a request to get the MAIN category of the current product

$same_cata_id = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . (int)$product_id . "' AND main_category = '1' LIMIT 0,1");

Below we add a bypass to get the most digital value of the category ID

foreach ($same_cata_id->rows as $cata){
			$related_cata = $cata['category_id'];
		}

And it remains only to replace the existing request for goods with ours, where we indicate the desired category

$query = $this->db->query("SELECT *, (p.product_id) AS related_id FROM
          " . DB_PREFIX . "product_to_category p2c 
            LEFT JOIN " . DB_PREFIX . "product p ON (p2c.product_id = p.product_id)
            LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id)
          WHERE 
            p2c.category_id = " . (int) $related_cata . "
              AND p.product_id <> " . (int) $product_id . "
              AND p.status = '1'
			  AND p2c.main_category = '1'
              AND p.date_available <= NOW()
              AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'
            ORDER BY RAND() ASC
          LIMIT 0,4");

Products will be displayed on the landing page in the already existing place with the heading "Recommended Products" and 4 blocks will be displayed. To increase, you can change the limit at the end of the request.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
The comment language code.