feat: Implement upsell functionality with repository and service layers
This commit is contained in:
98
app/Repositories/EventRepository.php
Normal file
98
app/Repositories/EventRepository.php
Normal file
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
namespace Sodino\Repositories;
|
||||
|
||||
/**
|
||||
* Event Repository
|
||||
*/
|
||||
class EventRepository {
|
||||
private $table_name;
|
||||
|
||||
public function __construct() {
|
||||
global $wpdb;
|
||||
$this->table_name = $wpdb->prefix . 'sodino_events';
|
||||
}
|
||||
|
||||
public function insert(array $data) {
|
||||
global $wpdb;
|
||||
return $wpdb->insert($this->table_name, $data);
|
||||
}
|
||||
|
||||
public function getEvents(array $filters = []) {
|
||||
global $wpdb;
|
||||
$params = [];
|
||||
$where = $this->buildWhereClauses($filters, $params);
|
||||
|
||||
$sql = "SELECT * FROM {$this->table_name} WHERE " . implode(' AND ', $where) . " ORDER BY created_at ASC";
|
||||
return $wpdb->get_results($wpdb->prepare($sql, $params), ARRAY_A);
|
||||
}
|
||||
|
||||
public function getCount(array $filters = []) {
|
||||
global $wpdb;
|
||||
$params = [];
|
||||
$where = $this->buildWhereClauses($filters, $params);
|
||||
|
||||
$sql = "SELECT COUNT(*) FROM {$this->table_name} WHERE " . implode(' AND ', $where);
|
||||
return (int) $wpdb->get_var($wpdb->prepare($sql, $params));
|
||||
}
|
||||
|
||||
public function getSum($field, array $filters = []) {
|
||||
global $wpdb;
|
||||
if (!in_array($field, ['value', 'discount_value'], true)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
$params = [];
|
||||
$where = $this->buildWhereClauses($filters, $params);
|
||||
|
||||
$sql = "SELECT SUM({$field}) FROM {$this->table_name} WHERE " . implode(' AND ', $where);
|
||||
return floatval($wpdb->get_var($wpdb->prepare($sql, $params)));
|
||||
}
|
||||
|
||||
public function getRuleUsageCount($rule_id) {
|
||||
global $wpdb;
|
||||
return (int) $wpdb->get_var($wpdb->prepare(
|
||||
"SELECT COUNT(*) FROM {$this->table_name} WHERE event_type = %s AND rule_id = %d",
|
||||
'discount_applied',
|
||||
$rule_id
|
||||
));
|
||||
}
|
||||
|
||||
private function buildWhereClauses(array $filters, array &$params) {
|
||||
$where = ['1=1'];
|
||||
|
||||
if (!empty($filters['event_type'])) {
|
||||
if (is_array($filters['event_type'])) {
|
||||
$placeholders = implode(', ', array_fill(0, count($filters['event_type']), '%s'));
|
||||
$where[] = "event_type IN ($placeholders)";
|
||||
$params = array_merge($params, $filters['event_type']);
|
||||
} else {
|
||||
$where[] = 'event_type = %s';
|
||||
$params[] = $filters['event_type'];
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($filters['product_ids'])) {
|
||||
$ids = array_map('intval', (array) $filters['product_ids']);
|
||||
$placeholders = implode(', ', array_fill(0, count($ids), '%d'));
|
||||
$where[] = "product_id IN ($placeholders)";
|
||||
$params = array_merge($params, $ids);
|
||||
}
|
||||
|
||||
if (!empty($filters['rule_id'])) {
|
||||
$where[] = 'rule_id = %d';
|
||||
$params[] = intval($filters['rule_id']);
|
||||
}
|
||||
|
||||
if (!empty($filters['from'])) {
|
||||
$where[] = 'created_at >= %s';
|
||||
$params[] = $filters['from'];
|
||||
}
|
||||
|
||||
if (!empty($filters['to'])) {
|
||||
$where[] = 'created_at <= %s';
|
||||
$params[] = $filters['to'];
|
||||
}
|
||||
|
||||
return $where;
|
||||
}
|
||||
}
|
||||
61
app/Repositories/UpsellRepository.php
Normal file
61
app/Repositories/UpsellRepository.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
namespace Sodino\Repositories;
|
||||
|
||||
use Sodino\Models\Upsell;
|
||||
|
||||
/**
|
||||
* Upsell Repository
|
||||
*/
|
||||
class UpsellRepository {
|
||||
private $table_name;
|
||||
|
||||
public function __construct() {
|
||||
global $wpdb;
|
||||
$this->table_name = $wpdb->prefix . 'sodino_upsells';
|
||||
}
|
||||
|
||||
public function getAll() {
|
||||
global $wpdb;
|
||||
$results = $wpdb->get_results("SELECT * FROM {$this->table_name} ORDER BY priority DESC, id ASC", ARRAY_A);
|
||||
$items = [];
|
||||
foreach ($results as $result) {
|
||||
$items[] = new Upsell($result);
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
public function getById($id) {
|
||||
global $wpdb;
|
||||
$result = $wpdb->get_row($wpdb->prepare("SELECT * FROM {$this->table_name} WHERE id = %d", $id), ARRAY_A);
|
||||
return $result ? new Upsell($result) : null;
|
||||
}
|
||||
|
||||
public function getActive() {
|
||||
global $wpdb;
|
||||
$results = $wpdb->get_results("SELECT * FROM {$this->table_name} WHERE status = 1 ORDER BY priority DESC, id ASC", ARRAY_A);
|
||||
$items = [];
|
||||
foreach ($results as $result) {
|
||||
$items[] = new Upsell($result);
|
||||
}
|
||||
return $items;
|
||||
}
|
||||
|
||||
public function save(Upsell $upsell) {
|
||||
global $wpdb;
|
||||
$data = $upsell->toArray();
|
||||
unset($data['id'], $data['created_at'], $data['updated_at']);
|
||||
|
||||
if ($upsell->id) {
|
||||
$wpdb->update($this->table_name, $data, ['id' => $upsell->id]);
|
||||
return $upsell->id;
|
||||
}
|
||||
|
||||
$wpdb->insert($this->table_name, $data);
|
||||
return $wpdb->insert_id;
|
||||
}
|
||||
|
||||
public function delete($id) {
|
||||
global $wpdb;
|
||||
return $wpdb->delete($this->table_name, ['id' => $id]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user