Files
sodino/app/Repositories/UpsellRepository.php

70 lines
1.8 KiB
PHP

<?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) {
$result = $wpdb->update($this->table_name, $data, ['id' => $upsell->id]);
if ($result === false) {
return false;
}
return $upsell->id;
}
$result = $wpdb->insert($this->table_name, $data);
if ($result === false) {
return false;
}
return $wpdb->insert_id;
}
public function delete($id) {
global $wpdb;
return $wpdb->delete($this->table_name, ['id' => $id]);
}
}