feat: Implement upsell functionality with repository and service layers
This commit is contained in:
@@ -10,6 +10,8 @@ class Rule {
|
||||
public $conditions;
|
||||
public $actions;
|
||||
public $priority;
|
||||
public $usage_limit;
|
||||
public $user_roles;
|
||||
public $start_date;
|
||||
public $end_date;
|
||||
public $enabled;
|
||||
@@ -29,6 +31,8 @@ class Rule {
|
||||
$this->conditions = $this->parseJsonField($data['conditions'] ?? '[]');
|
||||
$this->actions = $this->parseJsonField($data['actions'] ?? '[]');
|
||||
$this->priority = isset($data['priority']) ? (int) $data['priority'] : 10;
|
||||
$this->usage_limit = isset($data['usage_limit']) ? (int) $data['usage_limit'] : 0;
|
||||
$this->user_roles = $this->parseRolesField($data['user_roles'] ?? '');
|
||||
$this->start_date = $data['start_date'] ?? null;
|
||||
$this->end_date = $data['end_date'] ?? null;
|
||||
$this->enabled = isset($data['enabled']) ? (int) $data['enabled'] : 1;
|
||||
@@ -61,6 +65,18 @@ class Rule {
|
||||
return is_array($decoded) ? $decoded : [];
|
||||
}
|
||||
|
||||
private function parseRolesField($value) {
|
||||
if (is_array($value)) {
|
||||
return array_filter(array_map('trim', $value));
|
||||
}
|
||||
|
||||
if (!is_string($value)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_filter(array_map('trim', explode(',', $value)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert to array
|
||||
*/
|
||||
@@ -71,6 +87,8 @@ class Rule {
|
||||
'conditions' => wp_json_encode($this->conditions),
|
||||
'actions' => wp_json_encode($this->actions),
|
||||
'priority' => $this->priority,
|
||||
'usage_limit' => $this->usage_limit,
|
||||
'user_roles' => is_array($this->user_roles) ? implode(',', $this->user_roles) : $this->user_roles,
|
||||
'start_date' => $this->start_date,
|
||||
'end_date' => $this->end_date,
|
||||
'enabled' => $this->enabled,
|
||||
|
||||
53
app/Models/Upsell.php
Normal file
53
app/Models/Upsell.php
Normal file
@@ -0,0 +1,53 @@
|
||||
<?php
|
||||
namespace Sodino\Models;
|
||||
|
||||
/**
|
||||
* Upsell Model
|
||||
*/
|
||||
class Upsell {
|
||||
public $id;
|
||||
public $title;
|
||||
public $trigger_type;
|
||||
public $trigger_value;
|
||||
public $target_product_id;
|
||||
public $discount_type;
|
||||
public $discount_value;
|
||||
public $status;
|
||||
public $priority;
|
||||
public $created_at;
|
||||
public $updated_at;
|
||||
|
||||
public function __construct($data = []) {
|
||||
$this->id = isset($data['id']) ? (int) $data['id'] : null;
|
||||
$this->title = $data['title'] ?? '';
|
||||
$this->trigger_type = $data['trigger_type'] ?? 'product';
|
||||
$this->trigger_value = isset($data['trigger_value']) ? (string) $data['trigger_value'] : '';
|
||||
$this->target_product_id = isset($data['target_product_id']) ? (int) $data['target_product_id'] : 0;
|
||||
$this->discount_type = $data['discount_type'] ?? 'percentage';
|
||||
$this->discount_value = isset($data['discount_value']) ? floatval($data['discount_value']) : 0;
|
||||
$this->status = isset($data['status']) ? (int) $data['status'] : 1;
|
||||
$this->priority = isset($data['priority']) ? (int) $data['priority'] : 10;
|
||||
$this->created_at = $data['created_at'] ?? null;
|
||||
$this->updated_at = $data['updated_at'] ?? null;
|
||||
}
|
||||
|
||||
public function isActive() {
|
||||
return (bool) $this->status;
|
||||
}
|
||||
|
||||
public function toArray() {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => sanitize_text_field($this->title),
|
||||
'trigger_type' => sanitize_text_field($this->trigger_type),
|
||||
'trigger_value' => sanitize_text_field($this->trigger_value),
|
||||
'target_product_id' => $this->target_product_id,
|
||||
'discount_type' => sanitize_text_field($this->discount_type),
|
||||
'discount_value' => floatval($this->discount_value),
|
||||
'status' => $this->status,
|
||||
'priority' => $this->priority,
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user