public function getRproducts($product_id, $category_id, $limit){
$color_query = $this->db->query("SELECT text FROM " . DB_PREFIX . "product_attribute WHERE product_id = '" .(int)$product_id. "' AND attribute_id = 12 LIMIT 1");
$color = (isset($color_query->row['text']))?$color_query->row['text']:false;
$product_data = array();
if (!$color) {
$query = $this->db->query("
SELECT p.product_id FROM " . DB_PREFIX . "product p
LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id=p2c.product_id)
LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id)
WHERE p.status = '1' AND p.quantity > 0
AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'
AND p2c.category_id = '" . (int)$category_id ."'
ORDER BY RAND() LIMIT " . (int)$limit);
} else {
$color = trim(mb_strtolower($color));
$query = $this->db->query("
SELECT p.product_id FROM " . DB_PREFIX . "product p
LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id=p2c.product_id)
LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id)
LEFT JOIN " . DB_PREFIX . "product_attribute pa ON (p.product_id = pa.product_id)
WHERE p.status = '1'
AND p.quantity > 0
AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'
AND p2c.category_id = '" . (int)$category_id ."'
AND TRIM(LOWER(pa.text)) LIKE '" .$color. "'
ORDER BY RAND() LIMIT " . (int)$limit);
if (!$query->rows){
$query = $this->db->query("
SELECT p.product_id FROM " . DB_PREFIX . "product p
LEFT JOIN " . DB_PREFIX . "product_to_category p2c ON (p.product_id=p2c.product_id)
LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id)
WHERE p.status = '1' AND p.quantity > 0
AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "'
AND p2c.category_id = '" . (int)$category_id ."'
ORDER BY RAND() LIMIT " . (int)$limit);
}
}
$this->load->model('catalog/product');
foreach ($query->rows as $result) {
$product_data[$result['product_id']] = $this->model_catalog_product->getProduct($result['product_id']);
}
return $product_data;
}