feat(Core): add optimize and complete code
This commit is contained in:
@@ -37,6 +37,9 @@ class AdminController {
|
||||
'cart_contains_category',
|
||||
'customer_order_count_min',
|
||||
'customer_order_count_max',
|
||||
'customer_days_since_last_order_min',
|
||||
'product_total_sales_max',
|
||||
'product_total_sales_min',
|
||||
'day_of_week',
|
||||
];
|
||||
private $allowedRuleConditionOperators = ['is', 'is_not', 'in', 'not_in'];
|
||||
@@ -81,6 +84,15 @@ class AdminController {
|
||||
[$this, 'addRulePage']
|
||||
);
|
||||
|
||||
add_submenu_page(
|
||||
'sodino-rules',
|
||||
__('قالبهای آماده', 'sodino'),
|
||||
__('قالبهای آماده', 'sodino'),
|
||||
'manage_options',
|
||||
'sodino-templates',
|
||||
[$this, 'templatesPage']
|
||||
);
|
||||
|
||||
add_submenu_page(
|
||||
'sodino-rules',
|
||||
__('آپسل (پیشنهاد فروش)', 'sodino'),
|
||||
@@ -199,6 +211,12 @@ class AdminController {
|
||||
$this->listRulesPage();
|
||||
}
|
||||
|
||||
public function templatesPage() {
|
||||
$ruleTemplates = $this->getRuleTemplates();
|
||||
$upsellTemplates = $this->getUpsellTemplates();
|
||||
include SODINO_PLUGIN_DIR . 'admin/views/templates.php';
|
||||
}
|
||||
|
||||
/**
|
||||
* Dashboard page
|
||||
*/
|
||||
@@ -249,6 +267,8 @@ class AdminController {
|
||||
$this->saveRule();
|
||||
} else {
|
||||
$rule = new Rule();
|
||||
$templateKey = isset($_GET['template']) ? sanitize_key(wp_unslash($_GET['template'])) : '';
|
||||
$selectedTemplate = $this->applyRuleTemplate($rule, $templateKey);
|
||||
include SODINO_PLUGIN_DIR . 'admin/views/rule-form.php';
|
||||
}
|
||||
}
|
||||
@@ -284,6 +304,8 @@ class AdminController {
|
||||
$this->saveUpsell();
|
||||
} else {
|
||||
$upsell = new Upsell();
|
||||
$templateKey = isset($_GET['template']) ? sanitize_key(wp_unslash($_GET['template'])) : '';
|
||||
$selectedTemplate = $this->applyUpsellTemplate($upsell, $templateKey);
|
||||
include SODINO_PLUGIN_DIR . 'admin/views/upsell-form.php';
|
||||
}
|
||||
}
|
||||
@@ -371,6 +393,117 @@ class AdminController {
|
||||
include SODINO_PLUGIN_DIR . 'admin/views/banner-list.php';
|
||||
}
|
||||
|
||||
private function getRuleTemplates() {
|
||||
return [
|
||||
'first_purchase' => [
|
||||
'title' => __('تخفیف اولین خرید', 'sodino'),
|
||||
'description' => __('برای کاربران واردشدهای که هنوز سفارشی ثبت نکردهاند تخفیف درصدی اعمال میکند.', 'sodino'),
|
||||
'name' => __('تخفیف اولین خرید', 'sodino'),
|
||||
'priority' => 90,
|
||||
'usage_limit' => 0,
|
||||
'conditions' => [
|
||||
['type' => 'user_type', 'operator' => 'is', 'value' => 'new'],
|
||||
],
|
||||
'actions' => [
|
||||
['type' => 'discount_percent', 'value' => 10],
|
||||
],
|
||||
],
|
||||
'return_after_30_days' => [
|
||||
'title' => __('تخفیف بازگشت مشتری بعد از ۳۰ روز', 'sodino'),
|
||||
'description' => __('برای مشتریانی که حداقل یک سفارش دارند و ۳۰ روز از آخرین سفارششان گذشته است.', 'sodino'),
|
||||
'name' => __('کمپین بازگشت مشتریان قدیمی', 'sodino'),
|
||||
'priority' => 80,
|
||||
'usage_limit' => 0,
|
||||
'conditions' => [
|
||||
['type' => 'customer_order_count_min', 'operator' => 'is', 'value' => '1'],
|
||||
['type' => 'customer_days_since_last_order_min', 'operator' => 'is', 'value' => '30'],
|
||||
],
|
||||
'actions' => [
|
||||
['type' => 'discount_percent', 'value' => 12],
|
||||
],
|
||||
],
|
||||
'cart_total_threshold' => [
|
||||
'title' => __('تخفیف سبد بالای مبلغ مشخص', 'sodino'),
|
||||
'description' => __('وقتی مبلغ سبد از حد مشخصی بالاتر رفت، مشتری تخفیف دریافت میکند.', 'sodino'),
|
||||
'name' => __('تخفیف سبد خرید بالای ۱٬۰۰۰٬۰۰۰', 'sodino'),
|
||||
'priority' => 70,
|
||||
'usage_limit' => 0,
|
||||
'conditions' => [
|
||||
['type' => 'cart_total_min', 'operator' => 'is', 'value' => '1000000'],
|
||||
],
|
||||
'actions' => [
|
||||
['type' => 'discount_percent', 'value' => 5],
|
||||
],
|
||||
],
|
||||
'slow_moving_stock' => [
|
||||
'title' => __('فروش سریع موجودی کمفروش', 'sodino'),
|
||||
'description' => __('برای محصولاتی که فروش کل پایینی دارند تخفیف خودکار میگذارد.', 'sodino'),
|
||||
'name' => __('تخفیف محصولات کمفروش', 'sodino'),
|
||||
'priority' => 60,
|
||||
'usage_limit' => 0,
|
||||
'conditions' => [
|
||||
['type' => 'product_total_sales_max', 'operator' => 'is', 'value' => '5'],
|
||||
],
|
||||
'actions' => [
|
||||
['type' => 'discount_percent', 'value' => 15],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function getUpsellTemplates() {
|
||||
return [
|
||||
'complementary_cart_product' => [
|
||||
'title' => __('پیشنهاد محصول مکمل در سبد خرید', 'sodino'),
|
||||
'description' => __('وقتی محصول یا دسته فعالساز در سبد باشد، محصول مکمل را با تخفیف پیشنهاد میدهد.', 'sodino'),
|
||||
'name' => __('پیشنهاد محصول مکمل', 'sodino'),
|
||||
'trigger_type' => 'product',
|
||||
'trigger_value' => '',
|
||||
'target_product_id' => 0,
|
||||
'discount_type' => 'percentage',
|
||||
'discount_value' => 10,
|
||||
'priority' => 80,
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
private function applyRuleTemplate(Rule $rule, $templateKey) {
|
||||
$templates = $this->getRuleTemplates();
|
||||
if (empty($templateKey) || empty($templates[$templateKey])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$template = $templates[$templateKey];
|
||||
$rule->name = $template['name'];
|
||||
$rule->priority = (int) $template['priority'];
|
||||
$rule->usage_limit = (int) $template['usage_limit'];
|
||||
$rule->conditions = $template['conditions'];
|
||||
$rule->actions = $template['actions'];
|
||||
$rule->enabled = 1;
|
||||
$rule->syncLegacyFields();
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
private function applyUpsellTemplate(Upsell $upsell, $templateKey) {
|
||||
$templates = $this->getUpsellTemplates();
|
||||
if (empty($templateKey) || empty($templates[$templateKey])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$template = $templates[$templateKey];
|
||||
$upsell->title = $template['name'];
|
||||
$upsell->trigger_type = $template['trigger_type'];
|
||||
$upsell->trigger_value = $template['trigger_value'];
|
||||
$upsell->target_product_id = (int) $template['target_product_id'];
|
||||
$upsell->discount_type = $template['discount_type'];
|
||||
$upsell->discount_value = (float) $template['discount_value'];
|
||||
$upsell->priority = (int) $template['priority'];
|
||||
$upsell->status = 1;
|
||||
|
||||
return $template;
|
||||
}
|
||||
|
||||
private function editBannerPage() {
|
||||
$id = isset($_GET['id']) ? (int) $_GET['id'] : 0;
|
||||
$banner = $this->bannerRepository->getById($id);
|
||||
@@ -522,7 +655,10 @@ class AdminController {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($_GET['_wpnonce']) || !in_array($_GET['action'], ['delete_upsell', 'toggle_upsell_status'], true) || !wp_verify_nonce($_GET['_wpnonce'], $_GET['action'])) {
|
||||
$action = isset($_GET['action']) ? sanitize_key(wp_unslash($_GET['action'])) : '';
|
||||
$nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : '';
|
||||
|
||||
if (!in_array($action, ['delete_upsell', 'toggle_upsell_status'], true) || !wp_verify_nonce($nonce, $action)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -531,13 +667,13 @@ class AdminController {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($_GET['action'] === 'delete_upsell') {
|
||||
if ($action === 'delete_upsell') {
|
||||
$this->upsellRepository->delete($id);
|
||||
wp_safe_redirect(admin_url('admin.php?page=sodino-upsells'));
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($_GET['action'] === 'toggle_upsell_status') {
|
||||
if ($action === 'toggle_upsell_status') {
|
||||
$upsell = $this->upsellRepository->getById($id);
|
||||
if ($upsell) {
|
||||
$upsell->status = $upsell->status ? 0 : 1;
|
||||
@@ -553,7 +689,10 @@ class AdminController {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isset($_GET['_wpnonce']) || !in_array($_GET['action'], ['delete_banner', 'toggle_banner_status'], true) || !wp_verify_nonce($_GET['_wpnonce'], $_GET['action'])) {
|
||||
$action = isset($_GET['action']) ? sanitize_key(wp_unslash($_GET['action'])) : '';
|
||||
$nonce = isset($_GET['_wpnonce']) ? sanitize_text_field(wp_unslash($_GET['_wpnonce'])) : '';
|
||||
|
||||
if (!in_array($action, ['delete_banner', 'toggle_banner_status'], true) || !wp_verify_nonce($nonce, $action)) {
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -562,13 +701,13 @@ class AdminController {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($_GET['action'] === 'delete_banner') {
|
||||
if ($action === 'delete_banner') {
|
||||
$this->bannerRepository->delete($id);
|
||||
wp_safe_redirect(admin_url('admin.php?page=sodino-banners'));
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($_GET['action'] === 'toggle_banner_status') {
|
||||
if ($action === 'toggle_banner_status') {
|
||||
$banner = $this->bannerRepository->getById($id);
|
||||
if ($banner) {
|
||||
$banner->status = $banner->status ? 0 : 1;
|
||||
@@ -588,7 +727,7 @@ class AdminController {
|
||||
wp_send_json([]);
|
||||
}
|
||||
|
||||
$term = sanitize_text_field($_POST['term'] ?? '');
|
||||
$term = isset($_POST['term']) ? sanitize_text_field(wp_unslash($_POST['term'])) : '';
|
||||
if (empty($term) || !function_exists('wc_get_products')) {
|
||||
wp_send_json([]);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user