MarketKing vendor rejection reason

PHP 133 lines Shared by KingsPlugins Support

/**
 * 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' )
	);
}
How to add this code

Any method works: a code snippets plugin, or your child theme's functions.php file.

Not sure how? We recommend the free WPCode plugin: add a new PHP snippet, paste the code, then activate and save it.

Need a hand? Reply on your ticket and we'll help.

Sign In