Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2017
1,072
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.97 KB | None | 0 0
  1. <?php
  2. /**
  3. * @author Nick M. <webdev.nick@gmail.com>
  4. * @package welldone_opencart
  5. */
  6.  
  7. class ModelWelldoneProduct extends Model {
  8.  
  9. private static $labels = array();
  10. private static $new_products = null;
  11.  
  12. public function __construct($registry) {
  13. parent::__construct($registry);
  14. $this->load->model('catalog/product');
  15. $this->load->model('tool/image');
  16. }
  17.  
  18. public function getLabels($product_id) {
  19.  
  20. //New product
  21. if (self::$new_products === null)
  22. {
  23. self::$new_products = $this->model_catalog_product->getLatestProducts(10);
  24. }
  25.  
  26. if ($this->welldone->get_settings('label_new_status', 'show') == 'show')
  27. {
  28. if (!$this->hasLabel($product_id, 'new') && is_array(self::$new_products)) {
  29. foreach (self::$new_products as $product) {
  30. if ($product_id == $product['product_id']) {
  31. $this->addLabel($product_id, 'new', $this->welldone->get_settings('label_new_text', 'New'));
  32. break;
  33. }
  34. }
  35. }
  36. }
  37.  
  38. if ($this->welldone->get_settings('label_special_status', 'show') == 'show')
  39. {
  40. //Get discount
  41. $product = $this->model_catalog_product->getProduct($product_id);
  42.  
  43. if (!$this->hasLabel($product_id, 'discount')){
  44. if ((float)$product['special']) {
  45. if ($this->welldone->get_settings('label_special_type', 'percent') === 'percent') {
  46. if (($this->config->get('config_customer_price') && $this->customer->isLogged()) || !$this->config->get('config_customer_price')) {
  47. $price = $this->tax->calculate($product['price'], $product['tax_class_id'], $this->config->get('config_tax'));
  48. } else {
  49. $price = false;
  50. }
  51.  
  52. $special_price = $this->tax->calculate($product['special'], $product['tax_class_id'], $this->config->get('config_tax'));
  53. if ($price > 0) {
  54. $discount_price = round(($price - $special_price) / $price * 100);
  55. $this->addLabel($product_id, 'discount', '-' .$discount_price.'%');
  56. }
  57.  
  58. }
  59. else
  60. {
  61. $this->addLabel($product_id, 'discount', $this->welldone->get_settings('label_discount_text', 'Sale'));
  62. }
  63. }
  64. }
  65. }
  66.  
  67. if ($this->welldone->get_settings('label_oos_status', 'show') == 'show')
  68. {
  69. //Get out of stock
  70. if (!$this->hasLabel($product_id, 'outofstock')){
  71. if ($product['quantity'] <= 0) {
  72. $this->addLabel($product_id, 'outofstock', $product['stock_status']);
  73. }
  74. }
  75. }
  76.  
  77. //Get countdown
  78. if (!$this->hasLabel($product_id, 'countdown') && $this->welldone->get_settings('product_count_down_status','show') == 'show'){
  79. $special = $this->getSpecialPriceEnd($product_id);
  80.  
  81. if ($special !== false){
  82. $this->addLabel($product_id, 'countdown', $special['date_end']);
  83. }
  84. }
  85.  
  86. //Get colors
  87. if (!$this->hasLabel($product_id, 'colors')){
  88. $color_options = $this->getColorsOptions($product_id);
  89.  
  90. if (count($color_options))
  91. {
  92. $this->addLabel($product_id, 'colors', $color_options);
  93. }
  94. }
  95.  
  96. //Get size
  97. if (!$this->hasLabel($product_id, 'sizes')){
  98. $size_options = $this->getSizeOptions($product_id);
  99.  
  100. if (count($size_options))
  101. {
  102. $this->addLabel($product_id, 'sizes', $size_options);
  103. }
  104. }
  105.  
  106. if (!isset(self::$labels[$product_id])) {
  107. return array();
  108. }
  109. else
  110. return self::$labels[$product_id];
  111. }
  112.  
  113. private function getColorsOptions($product_id){
  114. $color_option_id = $this->welldone->get_settings('product_color_option_id',array());
  115. $product_options = $this->model_catalog_product->getProductOptions($product_id);
  116. $color_options = array();
  117.  
  118. foreach($product_options as $option)
  119. {
  120. if (in_array($option['option_id'],$color_option_id))
  121. {
  122. foreach($option['product_option_value'] as &$opt)
  123. {
  124. $opt['image'] = $this->model_tool_image->resize($opt['image'], 100, 100);
  125. }
  126. return $option['product_option_value'];
  127. }
  128. }
  129.  
  130. return $color_options;
  131. }
  132.  
  133. private function getSizeOptions($product_id){
  134. $color_option_id = $this->welldone->get_settings('product_size_option_id',array());
  135. $product_options = $this->model_catalog_product->getProductOptions($product_id);
  136. $color_options = array();
  137.  
  138. foreach($product_options as $option)
  139. {
  140. if (in_array($option['option_id'],$color_option_id))
  141. {
  142. return $option['product_option_value'];
  143. }
  144. }
  145.  
  146. return $color_options;
  147. }
  148.  
  149. private function getSpecialPriceEnd($product_id) {
  150. $sql = "select date_start,date_end from " . DB_PREFIX . "product_special where product_id='$product_id'";
  151. $query = $this->db->query($sql);
  152.  
  153. if ($query->num_rows) {
  154. $date_start = (int)strtotime($query->row['date_start']);
  155. $date_end = (int)strtotime($query->row['date_end']);
  156. $now = time();
  157. $valid = true;
  158.  
  159. if ($date_start > 0 && $date_start > $now)
  160. {
  161. $valid = false;
  162. }
  163. elseif ($date_end > 0 && $date_end < $now)
  164. $valid = false;
  165.  
  166. if ($valid)
  167. return $query->row;
  168. else
  169. return false;
  170. }
  171. else
  172. return false;
  173. }
  174.  
  175. private function hasLabel($product_id, $label) {
  176. if (!isset(self::$labels[$product_id])) {
  177. return false;
  178. }
  179. return in_array($label, self::$labels[$product_id]);
  180. }
  181.  
  182. private function addLabel($product_id, $label, $text) {
  183. if (!isset(self::$labels[$product_id])) {
  184. self::$labels[$product_id] = array();
  185. }
  186. self::$labels[$product_id][$label] = $text;
  187. }
  188.  
  189. public function getProductsByID($ids,$limit)
  190. {
  191. if (count($ids) == 0)
  192. return array();
  193.  
  194. $sql_ids = array();
  195. foreach($ids as $k=>$v)
  196. {
  197. if ((int)$v > 0)
  198. $sql_ids[] = $v;
  199. }
  200.  
  201. if (count($sql_ids) == 0)
  202. return array();
  203.  
  204. $this->load->model('catalog/product');
  205.  
  206. $product_data = array();
  207. $query = $this->db->query("SELECT p.product_id FROM " . DB_PREFIX . "product p LEFT JOIN " . DB_PREFIX . "product_to_store p2s ON (p.product_id = p2s.product_id) WHERE p.status = '1' AND p.date_available <= NOW() AND p2s.store_id = '" . (int)$this->config->get('config_store_id') . "' and p.product_id in (".implode(',',$sql_ids).") ORDER BY p.viewed DESC, p.date_added DESC LIMIT " . (int)$limit);
  208.  
  209. foreach ($query->rows as $result) {
  210. $product_data[$result['product_id']] = $this->model_catalog_product->getProduct($result['product_id']);
  211. }
  212.  
  213. $results = array();
  214. foreach($ids as $idx=>$id)
  215. {
  216. if (isset($product_data[$id]))
  217. $results[] = $product_data[$id];
  218. }
  219.  
  220. return $results;
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement