1: | <?php
2: | namespace Opencart\Catalog\Model\Catalog;
3: | /**
4: | * Class Review
5: | *
6: | * @package Opencart\Catalog\Model\Catalog
7: | */
8: | class Review extends \Opencart\System\Engine\Model {
9: | /**
10: | * Add Review
11: | *
12: | * @param int $product_id
13: | * @param array<string, mixed> $data
14: | *
15: | * @return int
16: | */
17: | public function addReview(int $product_id, array $data): int {
18: | $this->db->query("INSERT INTO `" . DB_PREFIX . "review` SET `author` = '" . $this->db->escape($data['author']) . "', `customer_id` = '" . (int)$this->customer->getId() . "', `product_id` = '" . (int)$product_id . "', `text` = '" . $this->db->escape($data['text']) . "', `rating` = '" . (int)$data['rating'] . "', `date_added` = NOW()");
19: |
20: | return $this->db->getLastId();
21: | }
22: |
23: | /**
24: | * Get Reviews By Product ID
25: | *
26: | * @param int $product_id
27: | * @param int $start
28: | * @param int $limit
29: | *
30: | * @return array<int, array<string, mixed>>
31: | */
32: | public function getReviewsByProductId(int $product_id, int $start = 0, int $limit = 20): array {
33: | if ($start < 0) {
34: | $start = 0;
35: | }
36: |
37: | if ($limit < 1) {
38: | $limit = 20;
39: | }
40: |
41: | $query = $this->db->query("SELECT `r`.`author`, `r`.`rating`, `r`.`text`, `r`.`date_added` FROM `" . DB_PREFIX . "review` `r` LEFT JOIN `" . DB_PREFIX . "product` `p` ON (`r`.`product_id` = `p`.`product_id`) LEFT JOIN `" . DB_PREFIX . "product_description` `pd` ON (`p`.`product_id` = pd.`product_id`) WHERE `r`.`product_id` = '" . (int)$product_id . "' AND `p`.`date_available` <= NOW() AND `p`.`status` = '1' AND `r`.`status` = '1' AND `pd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "' ORDER BY `r`.`date_added` DESC LIMIT " . (int)$start . "," . (int)$limit);
42: |
43: | return $query->rows;
44: | }
45: |
46: | /**
47: | * Get Total Reviews By Product ID
48: | *
49: | * @param int $product_id
50: | *
51: | * @return int
52: | */
53: | public function getTotalReviewsByProductId(int $product_id): int {
54: | $query = $this->db->query("SELECT COUNT(*) AS `total` FROM `" . DB_PREFIX . "review` `r` LEFT JOIN `" . DB_PREFIX . "product` `p` ON (`r`.`product_id` = `p`.`product_id`) LEFT JOIN `" . DB_PREFIX . "product_description` `pd` ON (`p`.`product_id` = `pd`.`product_id`) WHERE `p`.`product_id` = '" . (int)$product_id . "' AND `p`.`date_available` <= NOW() AND `p`.`status` = '1' AND `r`.`status` = '1' AND `pd`.`language_id` = '" . (int)$this->config->get('config_language_id') . "'");
55: |
56: | return (int)$query->row['total'];
57: | }
58: | }
59: |

OpenCart API API documentation generated by ApiGen dev-master