Back to Opencart

File catalog\model\marketing\coupon.php

docs/api/source-catalog.model.marketing.coupon.html

4.1.0.311.3 KB
Original Source

Namespaces

Classes

| 1: | <?php | | 2: | namespace Opencart\Catalog\Model\Marketing; | | 3: | /** | | 4: | * Class Coupon | | 5: | * | | 6: | * @package Opencart\Catalog\Model\Marketing | | 7: | */ | | 8: | class Coupon extends \Opencart\System\Engine\Model { | | 9: | /** | | 10: | * Get Coupon | | 11: | * | | 12: | * @param string $code | | 13: | * | | 14: | * @return array<string, mixed> | | 15: | */ | | 16: | public function getCoupon(string $code): array { | | 17: | $status = true; | | 18: | | | 19: | $coupon_info = $this->model_marketing_coupon->getCouponByCode($code); | | 20: | | | 21: | if ($coupon_info && ($coupon_info['date_start'] == '0000-00-00' || strtotime($coupon_info['date_start']) < strtotime(date('Y-m-d H:i:s'))) && ($coupon_info['date_end'] == '0000-00-00' || strtotime($coupon_info['date_end']) > strtotime(date('Y-m-d H:i:s')))) { | | 22: | if ($coupon_info['total'] > $this->cart->getSubTotal()) { | | 23: | $status = false; | | 24: | } | | 25: | | | 26: | $coupon_total = $this->model_marketing_coupon->getTotalHistories($coupon_info['coupon_id']); | | 27: | | | 28: | if ($coupon_info['uses_total'] > 0 && ($coupon_total >= $coupon_info['uses_total'])) { | | 29: | $status = false; | | 30: | } | | 31: | | | 32: | if ($coupon_info['logged'] && !$this->customer->getId()) { | | 33: | $status = false; | | 34: | } | | 35: | | | 36: | if ($this->customer->getId()) { | | 37: | $customer_total = $this->model_marketing_coupon->getTotalHistoriesByCustomerId($coupon_info['coupon_id'], $this->customer->getId()); | | 38: | | | 39: | if ($coupon_info['uses_customer'] > 0 && ($customer_total >= $coupon_info['uses_customer'])) { | | 40: | $status = false; | | 41: | } | | 42: | } | | 43: | | | 44: | // Products | | 45: | $coupon_product_data = $this->getProducts($coupon_info['coupon_id']); | | 46: | | | 47: | // Categories | | 48: | $coupon_category_data = $this->getCategories($coupon_info['coupon_id']); | | 49: | | | 50: | $product_data = []; | | 51: | | | 52: | if ($coupon_product_data || $coupon_category_data) { | | 53: | $this->load->model('catalog/product'); | | 54: | | | 55: | foreach ($this->cart->getProducts() as $product) { | | 56: | if (in_array($product['product_id'], $coupon_product_data)) { | | 57: | $product_data[] = $product['product_id']; | | 58: | | | 59: | continue; | | 60: | } | | 61: | | | 62: | foreach ($coupon_category_data as $category_id) { | | 63: | $product_total = $this->model_catalog_product->getTotalCategoriesByCategoryId($product['product_id'], $category_id); | | 64: | | | 65: | if ($product_total) { | | 66: | $product_data[] = $product['product_id']; | | 67: | | | 68: | continue; | | 69: | } | | 70: | } | | 71: | } | | 72: | | | 73: | if (!$product_data) { | | 74: | $status = false; | | 75: | } | | 76: | } | | 77: | } else { | | 78: | $status = false; | | 79: | } | | 80: | | | 81: | if ($status) { | | 82: | return [ | | 83: | 'coupon_id' => $coupon_info['coupon_id'], | | 84: | 'code' => $coupon_info['code'], | | 85: | 'name' => $coupon_info['name'], | | 86: | 'type' => $coupon_info['type'], | | 87: | 'discount' => $coupon_info['discount'], | | 88: | 'shipping' => $coupon_info['shipping'], | | 89: | 'total' => $coupon_info['total'], | | 90: | 'product' => $product_data, | | 91: | 'date_start' => $coupon_info['date_start'], | | 92: | 'date_end' => $coupon_info['date_end'], | | 93: | 'uses_total' => $coupon_info['uses_total'], | | 94: | 'uses_customer' => $coupon_info['uses_customer'], | | 95: | 'status' => $coupon_info['status'], | | 96: | 'date_added' => $coupon_info['date_added'] | | 97: | ]; | | 98: | } else { | | 99: | return []; | | 100: | } | | 101: | } | | 102: | | | 103: | /** | | 104: | * Get Coupon By Code | | 105: | * | | 106: | * @param string $code | | 107: | * | | 108: | * @return array<string, mixed> | | 109: | */ | | 110: | public function getCouponByCode(string $code): array { | | 111: | $query = $this->db->query("SELECT * FROM " . DB\_PREFIX . "coupon WHERE code = '" . $this->db->escape($code) . "' AND status = '1'"); | | 112: | | | 113: | return $query->row; | | 114: | } | | 115: | | | 116: | /** | | 117: | * Get Products | | 118: | * | | 119: | * @param int $coupon_id | | 120: | * | | 121: | * @return array<int, int> | | 122: | */ | | 123: | public function getProducts(int $coupon_id): array { | | 124: | $product_data = []; | | 125: | | | 126: | $query = $this->db->query("SELECT * FROM " . DB\_PREFIX . "coupon\_product WHERE coupon\_id = '" . (int)$coupon_id . "'"); | | 127: | | | 128: | foreach ($query->rows as $product) { | | 129: | $product_data[] = $product['product_id']; | | 130: | } | | 131: | | | 132: | return $product_data; | | 133: | } | | 134: | | | 135: | /** | | 136: | * Get Categories | | 137: | * | | 138: | * @param int $coupon_id | | 139: | * | | 140: | * @return array<int, int> | | 141: | */ | | 142: | public function getCategories(int $coupon_id): array { | | 143: | $category_data = []; | | 144: | | | 145: | $query = $this->db->query("SELECT * FROM " . DB\_PREFIX . "coupon\_category cc LEFT JOIN " . DB\_PREFIX . "category\_path cp ON (cc.category\_id = cp.path\_id) WHERE cc.coupon\_id = '" . (int)$coupon_id . "'"); | | 146: | | | 147: | foreach ($query->rows as $category) { | | 148: | $category_data[] = $category['category_id']; | | 149: | } | | 150: | | | 151: | return $category_data; | | 152: | } | | 153: | | | 154: | /** | | 155: | * Add History | | 156: | * | | 157: | * @param int $coupon_id | | 158: | * @param int $order_id | | 159: | * @param int $customer_id | | 160: | * @param float $amount | | 161: | * | | 162: | * @return void | | 163: | */ | | 164: | public function addHistory(int $coupon_id, int $order_id, int $customer_id, float $amount = 0.00): void { | | 165: | $this->db->query("INSERT INTO " . DB\_PREFIX . "coupon\_history SET coupon\_id = '" . (int)$coupon_id . "', order_id = '" . (int)$order_id . "', customer_id = '" . (int)$customer_id . "', amount = '" . (float)$amount . "', date_added = NOW()"); | | 166: | } | | 167: | | | 168: | /** | | 169: | * Delete Coupon Histories By Order ID | | 170: | * | | 171: | * @param int $order_id | | 172: | * | | 173: | * @return void | | 174: | */ | | 175: | public function deleteHistoriesByOrderId(int $order_id): void { | | 176: | $this->db->query("DELETE FROM " . DB\_PREFIX . "coupon\_history WHERE order\_id = '" . (int)$order_id . "'"); | | 177: | } | | 178: | | | 179: | /** | | 180: | * Get Total Histories | | 181: | * | | 182: | * @param string $coupon_id | | 183: | * | | 184: | * @return int | | 185: | */ | | 186: | public function getTotalHistories(string $coupon_id): int { | | 187: | $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB\_PREFIX . "coupon\_history ch LEFT JOIN " . DB\_PREFIX . "coupon c ON (ch.coupon\_id = c.coupon\_id) WHERE c.coupon\_id = '" . $this->db->escape($coupon_id) . "'"); | | 188: | | | 189: | return (int)$query->row['total']; | | 190: | } | | 191: | | | 192: | /** | | 193: | * Get Total Histories By Customer ID | | 194: | * | | 195: | * @param int $coupon_id | | 196: | * @param int $customer_id | | 197: | * | | 198: | * @return int | | 199: | */ | | 200: | public function getTotalHistoriesByCustomerId(int $coupon_id, int $customer_id): int { | | 201: | $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB\_PREFIX . "coupon\_history ch LEFT JOIN " . DB\_PREFIX . "coupon c ON (ch.coupon\_id = c.coupon\_id) WHERE c.coupon\_id = '" . (int)$coupon_id . "' AND ch.customer_id = '" . (int)$customer_id . "'"); | | 202: | | | 203: | return (int)$query->row['total']; | | 204: | } | | 205: | } | | 206: | |

OpenCart API API documentation generated by ApiGen dev-master