Untitled
webwizards
plain_text
10 hours ago
5.9 kB
1
Indexable
// 1. Add MOV fields to subaccount creation/edit form
add_action('b2bking_custom_new_subaccount_fields', 'b2bking_mov_custom_fields', 10, 1);
function b2bking_mov_custom_fields($user_id = 0) {
?>
<br>
<div class="b2bking_subaccounts_new_account_container_content_element">
<div class="b2bking_subaccounts_new_account_container_content_element_label">
<?php esc_html_e('Spending Limit', 'b2bking'); ?>
</div>
<input type="number" step="0.01" class="b2bking_subaccounts_new_account_container_content_element_text"
name="b2bking_mov_limit"
placeholder="<?php esc_attr_e('e.g., 5000', 'b2bking'); ?>"
<?php if($user_id !== 0){echo 'value="'.esc_attr(get_user_meta($user_id,'b2bking_mov_limit', true)).'"';}?>>
</div>
<div class="b2bking_subaccounts_new_account_container_content_element b2bking_subaccount_horizontal_line">
<div class="b2bking_subaccounts_new_account_container_content_element_label">
<?php esc_html_e('Period', 'b2bking'); ?>
</div>
<select class="b2bking_subaccounts_new_account_container_content_element_text" name="b2bking_mov_period">
<option value="weekly" <?php if($user_id !== 0){selected(get_user_meta($user_id,'b2bking_mov_period', true), 'weekly');}?>>
<?php esc_html_e('Weekly', 'b2bking'); ?>
</option>
<option value="monthly" <?php if($user_id !== 0){selected(get_user_meta($user_id,'b2bking_mov_period', true), 'monthly');}?>>
<?php esc_html_e('Monthly', 'b2bking'); ?>
</option>
<option value="quarterly" <?php if($user_id !== 0){selected(get_user_meta($user_id,'b2bking_mov_period', true), 'quarterly');}?>>
<?php esc_html_e('Quarterly', 'b2bking'); ?>
</option>
</select>
</div>
<?php
}
// 2. Register custom field names
add_filter('b2bking_custom_new_subaccount_field_names', 'b2bking_mov_field_names', 10, 1);
function b2bking_mov_field_names($fields) {
$fields[] = 'b2bking_mov_limit';
$fields[] = 'b2bking_mov_period';
return $fields;
}
// 3. Set custom meta keys
add_filter('b2bking_custom_field_meta', 'b2bking_mov_field_meta', 10, 1);
function b2bking_mov_field_meta($field_name) {
// Keep the same meta keys
return $field_name;
}
// 4. Check MOV restriction at cart/checkout
add_action('woocommerce_checkout_process', 'b2bking_check_mov_restriction');
add_action('woocommerce_before_cart', 'b2bking_check_mov_restriction');
add_action('woocommerce_before_checkout_form', 'b2bking_check_mov_restriction');
function b2bking_check_mov_restriction() {
$currentuser = wp_get_current_user();
$account_type = get_user_meta($currentuser->ID, 'b2bking_account_type', true);
if ($account_type !== 'subaccount') {
return;
}
$mov_limit = get_user_meta($currentuser->ID, 'b2bking_mov_limit', true);
if (empty($mov_limit) || $mov_limit <= 0) {
return;
}
$mov_period = get_user_meta($currentuser->ID, 'b2bking_mov_period', true) ?: 'monthly';
$period_start = b2bking_get_period_start($mov_period);
$spent = b2bking_get_spent_in_period($currentuser->ID, $period_start);
$cart_total = WC()->cart->total;
$remaining = $mov_limit - $spent;
if (($spent + $cart_total) > $mov_limit) {
$notice_text = sprintf(
__('Your spending limit is %s %s. You have %s remaining. Current cart total: %s', 'b2bking'),
wc_price($mov_limit),
$mov_period,
wc_price(max(0, $remaining)),
wc_price($cart_total)
);
if (is_cart() && !b2bking()->has_blocks_cart()) {
wc_print_notice($notice_text, 'error');
} else {
wc_add_notice($notice_text, 'error');
}
}
}
// 5. Helper: Get period start date
function b2bking_get_period_start($period) {
$now = current_time('timestamp');
switch ($period) {
case 'weekly':
return strtotime('last Monday', $now);
case 'quarterly':
$month = date('n', $now);
$quarter_start = floor(($month - 1) / 3) * 3 + 1;
return strtotime(date('Y', $now) . '-' . $quarter_start . '-01');
case 'monthly':
default:
return strtotime(date('Y-m-01', $now));
}
}
// 6. Helper: Calculate spent in period
function b2bking_get_spent_in_period($user_id, $period_start) {
$orders = wc_get_orders(array(
'customer_id' => $user_id,
'status' => array('wc-completed', 'wc-processing', 'wc-on-hold'),
'date_created' => '>=' . $period_start,
'limit' => -1,
));
$total = 0;
foreach ($orders as $order) {
$total += $order->get_total();
}
return $total;
}
// Display spending info after each subaccount's edit button
add_action('b2bking_subaccount_tab_right', 'b2bking_display_subaccount_spending', 10, 1);
function b2bking_display_subaccount_spending($subaccount) {
$subaccount_id = $subaccount->ID;
$limit = get_user_meta($subaccount_id, 'b2bking_mov_limit', true);
if (empty($limit) || $limit <= 0) return;
$period = get_user_meta($subaccount_id, 'b2bking_mov_period', true) ?: 'monthly';
$period_start = b2bking_get_period_start($period);
$spent = b2bking_get_spent_in_period($subaccount_id, $period_start);
$remaining = max(0, $limit - $spent);
echo '<div style="margin-top:10px; padding:10px; background:#f8f9fa; border-left:3px solid #0073aa;">';
echo '<strong>' . __('Spending:', 'b2bking') . '</strong> ';
echo wc_price($spent) . ' / ' . wc_price($limit) . ' (' . $period . ') ';
echo '<span style="color:' . ($remaining > 0 ? '#28a745' : '#dc3545') . ';">— ' . wc_price($remaining) . ' ' . __('remaining', 'b2bking') . '</span>';
echo '</div>';
}Editor is loading...
Leave a Comment