1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Keep a rejected vendor applicant as a customer and email the rejection reason.
*/
add_filter( 'marketking_enable_approve_as_customer', '__return_true' );
add_action( 'marketking_before_registration_approval', 'marketking_custom_rejection_reason_field' );
function marketking_custom_rejection_reason_field() {
?>
<div id="marketking-custom-rejection-reason-wrap" style="display:none; margin-bottom:15px; width:100%;">
<label for="marketking-custom-rejection-reason" style="display:block; font-weight:600; margin-bottom:6px;">
<?php esc_html_e( 'Vendor application rejection reason', 'marketking-multivendor-marketplace-for-woocommerce' ); ?>
</label>
<textarea id="marketking-custom-rejection-reason" rows="5" style="width:100%;" placeholder="<?php esc_attr_e( 'Enter the reason that will be emailed to the applicant.', 'marketking-multivendor-marketplace-for-woocommerce' ); ?>"></textarea>
<p class="description">
<?php esc_html_e( 'After entering the reason, click Reject vendor application. The account will remain active as a regular customer without vendor access.', 'marketking-multivendor-marketplace-for-woocommerce' ); ?>
</p>
</div>
<?php
}
add_action( 'admin_footer-user-edit.php', 'marketking_custom_rejection_reason_script' );
function marketking_custom_rejection_reason_script() {
?>
<script>
document.addEventListener('DOMContentLoaded', function () {
const groupSelect = document.querySelector('.marketking_user_registration_user_data_container_element_select_group');
const reasonWrap = document.getElementById('marketking-custom-rejection-reason-wrap');
const reasonField = document.getElementById('marketking-custom-rejection-reason');
const approveButton = document.querySelector('.marketking_user_registration_user_data_container_element_approval_button_approve');
if (!groupSelect || !reasonWrap || !reasonField || !approveButton || !window.jQuery) {
return;
}
const customerOption = groupSelect.querySelector('option[value="b2c"]');
if (customerOption) {
customerOption.textContent = 'Reject vendor application (keep as customer)';
}
const buttonTextNode = Array.from(approveButton.childNodes).find(function (node) {
return node.nodeType === window.Node.TEXT_NODE && node.textContent.trim();
});
const originalButtonLabel = buttonTextNode ? buttonTextNode.textContent.trim() : 'Approve user';
const updateReasonField = function () {
const rejectingApplication = groupSelect.value === 'b2c';
reasonWrap.style.display = rejectingApplication ? 'block' : 'none';
if (buttonTextNode) {
buttonTextNode.textContent = rejectingApplication ? ' Reject vendor application' : ' ' + originalButtonLabel;
}
};
groupSelect.addEventListener('change', updateReasonField);
updateReasonField();
approveButton.addEventListener('click', function (event) {
if (groupSelect.value !== 'b2c') {
return;
}
event.preventDefault();
event.stopImmediatePropagation();
const reason = reasonField.value.trim();
if (!reason) {
window.alert('Please enter a rejection reason first.');
reasonField.focus();
return;
}
if (!window.confirm('Reject this vendor application and keep the account as a regular customer?')) {
return;
}
window.jQuery.post(window.ajaxurl, {
action: 'marketkingapproveuser',
security: window.marketking.security,
chosen_group: 'b2c',
user: document.getElementById('marketking_user_registration_data_id').value,
marketking_rejection_reason: reason
}, function (response) {
if (String(response).trim() === 'success') {
window.location.reload();
return;
}
window.alert('The application could not be updated. Please reload the page and try again.');
}).fail(function () {
window.alert('The application could not be updated. Please reload the page and try again.');
});
}, true);
});
</script>
<?php
}
add_action( 'marketking_account_approved_as_customer_finish', 'marketking_custom_send_rejection_reason' );
function marketking_custom_send_rejection_reason() {
if ( empty( $_POST['user'] ) || empty( $_POST['marketking_rejection_reason'] ) ) {
return;
}
$user_id = absint( wp_unslash( $_POST['user'] ) );
$reason = sanitize_textarea_field( wp_unslash( $_POST['marketking_rejection_reason'] ) );
$user = get_userdata( $user_id );
if ( ! $user || ! is_email( $user->user_email ) || '' === $reason ) {
return;
}
update_user_meta( $user_id, 'marketking_vendor_application_pending', 'no' );
update_user_meta( $user_id, 'marketking_vendor_rejection_reason', $reason );
$name = $user->first_name ? $user->first_name : $user->display_name;
$site_name = wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES );
$subject = sprintf( '[%s] Vendor application update', $site_name );
$message = '<p>Hi ' . esc_html( $name ) . ',</p>';
$message .= '<p>Thank you for your vendor application. We are unable to approve it at this time.</p>';
$message .= '<p><strong>Reason:</strong><br>' . nl2br( esc_html( $reason ) ) . '</p>';
$message .= '<p>Your account remains active as a regular customer, but vendor access has not been enabled.</p>';
if ( function_exists( 'WC' ) && WC()->mailer() ) {
$message = WC()->mailer()->wrap_message( 'Vendor application update', $message );
}
wp_mail(
$user->user_email,
$subject,
$message,
array( 'Content-Type: text/html; charset=UTF-8' )
);
}