// داخل متد settings_init فقط فیلد دستهبندی مشمول رو ثبت میکنیم:
public function settings_init() {
register_setting('parlaLoyaltyPage', 'parla_loyalty_settings');
add_settings_section(
'parla_loyalty_section',
__('تنظیمات تخفیف وفاداری پارلا', 'parla'),
[$this, 'settings_section_callback'],
'parlaLoyaltyPage'
);
add_settings_field(
'included_categories',
__('دستهبندیهای مشمول', 'parla'),
[$this, 'render_categories_checkbox'],
'parlaLoyaltyPage',
'parla_loyalty_section',
['field_key' => 'included_categories']
);
}
// متن توضیح تنظیمات رو تغییر میدیم به:
public function settings_section_callback() {
echo '
در این صفحه میتوانید دستهبندیهایی که شامل تخفیف وفاداری میشوند را انتخاب کنید.
عدم انتخاب هر دسته، به معنی غیرمشمول بودن آن است.
';
}
// رندر چکباکس دستهبندیها بدون دستهبندی غیرمشمول:
public function render_categories_checkbox($args) {
$option = get_option('parla_loyalty_settings');
$selected = isset($option[$args['field_key']]) ? array_keys($option[$args['field_key']]) : [];
$selected = array_map('intval', $selected);
$categories = get_terms(['taxonomy' => 'product_cat', 'hide_empty' => false]);
echo '';
foreach ($categories as $category) {
$checked = in_array($category->term_id, $selected) ? 'checked' : '';
echo "";
}
echo '
';
}
// متد اعمال تخفیف فقط با چک روی دستهبندیهای مشمول:
public function apply_loyalty_discount($cart) {
if (is_admin() && !defined('DOING_AJAX')) return;
$user_id = get_current_user_id();
if (!$user_id) return;
$orders = wc_get_orders(['customer_id' => $user_id, 'status' => 'completed', 'limit' => -1]);
$order_count = count($orders);
$discount_percentage = 0;
if ($order_count >= 2) {
$discount_percentage = 1.4 + ($order_count - 2) * 0.7;
if ($discount_percentage > 14) $discount_percentage = 14;
}
if ($discount_percentage > 0) {
$option = get_option('parla_loyalty_settings');
$included = isset($option['included_categories']) ? array_map('intval', array_keys($option['included_categories'])) : [];
$eligible_total = 0;
foreach ($cart->get_cart() as $cart_item) {
$product_id = $cart_item['product_id'];
$terms = wp_get_post_terms($product_id, 'product_cat', ['fields' => 'ids']);
// اگر دسته بندی محصول توی مشمولها هست، تخفیف اعمال میشود
if (empty($included) || array_intersect($terms, $included)) {
$eligible_total += $cart_item['line_total'];
}
}
if ($eligible_total > 0) {
$discount_amount = ($eligible_total * $discount_percentage) / 100;
$cart->add_fee(__('تخفیف وفاداری', 'parla'), -$discount_amount);
}
}
}
هدست گردنی ریلمی - فروشگاه لوازم دیجیتال پارلاتل
تمام سفارشات تا حداکثر 3 (روز کاری) ارسال خواهد شد. رد کردن
نقد و بررسیها
هنوز بررسیای ثبت نشده است.