feat(admin): plate moderator roles, advanced limits, coupon scope edit/batch
This commit is contained in:
@@ -250,6 +250,88 @@ class AKSRT_Coupon {
|
||||
return $wpdb->update($this->table(), ['status' => 'active'], ['id' => $id], ['%s'], ['%d']) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get one coupon by id.
|
||||
*/
|
||||
public function get(int $id): ?array {
|
||||
global $wpdb;
|
||||
if ($id <= 0) {
|
||||
return null;
|
||||
}
|
||||
$row = $wpdb->get_row($wpdb->prepare(
|
||||
"SELECT * FROM {$this->table()} WHERE id = %d",
|
||||
$id
|
||||
), ARRAY_A);
|
||||
if (!$row) {
|
||||
return null;
|
||||
}
|
||||
return $this->normalize_row($row);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update mutable coupon fields (not code / used_count / created_at).
|
||||
*/
|
||||
public function update(int $id, array $data): bool {
|
||||
global $wpdb;
|
||||
if ($id <= 0 || !$this->get($id)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$fields = [];
|
||||
$format = [];
|
||||
|
||||
if (array_key_exists('type', $data)) {
|
||||
$type = sanitize_key((string) $data['type']);
|
||||
if (!in_array($type, [self::TYPE_PERCENT, self::TYPE_FIXED], true)) {
|
||||
$type = self::TYPE_FIXED;
|
||||
}
|
||||
$fields['type'] = $type;
|
||||
$format[] = '%s';
|
||||
}
|
||||
if (array_key_exists('value', $data)) {
|
||||
$fields['value'] = (float) $data['value'];
|
||||
$format[] = '%f';
|
||||
}
|
||||
if (array_key_exists('min_amount', $data)) {
|
||||
$fields['min_amount'] = (float) $data['min_amount'];
|
||||
$format[] = '%f';
|
||||
}
|
||||
if (array_key_exists('max_discount', $data)) {
|
||||
$fields['max_discount'] = (float) $data['max_discount'];
|
||||
$format[] = '%f';
|
||||
}
|
||||
if (array_key_exists('usage_limit', $data)) {
|
||||
$fields['usage_limit'] = (int) $data['usage_limit'];
|
||||
$format[] = '%d';
|
||||
}
|
||||
if (array_key_exists('per_user_limit', $data)) {
|
||||
$fields['per_user_limit'] = (int) $data['per_user_limit'];
|
||||
$format[] = '%d';
|
||||
}
|
||||
if (array_key_exists('expires_at', $data)) {
|
||||
$fields['expires_at'] = sanitize_text_field((string) $data['expires_at']);
|
||||
$format[] = '%s';
|
||||
}
|
||||
if (array_key_exists('product_scope', $data)) {
|
||||
$scope = is_array($data['product_scope']) ? $data['product_scope'] : [];
|
||||
$fields['product_scope'] = wp_json_encode($scope, JSON_UNESCAPED_UNICODE);
|
||||
$format[] = '%s';
|
||||
}
|
||||
if (array_key_exists('status', $data)) {
|
||||
$status = sanitize_key((string) $data['status']);
|
||||
if (in_array($status, ['active', 'disabled'], true)) {
|
||||
$fields['status'] = $status;
|
||||
$format[] = '%s';
|
||||
}
|
||||
}
|
||||
|
||||
if (!$fields) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $wpdb->update($this->table(), $fields, ['id' => $id], $format, ['%d']) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Batch-create coupons with auto-generated codes.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user