B2BKing Vendor Rejection Reason

PHP 142 lines Shared by KingsPlugins Support

/**
 * Reject a B2BKing vendor application while keeping the account as a customer.
 */

add_action( 'b2bking_registration_approval_after_group', 'b2bking_custom_rejection_reason_field' );
function b2bking_custom_rejection_reason_field( $user_id ) {
	if ( 'no' !== get_user_meta( $user_id, 'b2bking_account_approved', true ) ) {
		return;
	}
	?>
	<div id="b2bking-custom-rejection-reason-wrap" style="display:none; margin:15px 0; width:100%;">
		<label for="b2bking-custom-rejection-reason" style="display:block; font-weight:600; margin-bottom:6px;">
			<?php esc_html_e( 'Vendor application rejection reason', 'b2bking' ); ?>
		</label>
		<textarea id="b2bking-custom-rejection-reason" rows="5" style="width:100%;" placeholder="<?php esc_attr_e( 'Enter the reason that will be emailed to the applicant.', 'b2bking' ); ?>"></textarea>
		<p class="description">
			<?php esc_html_e( 'The account will remain active as a regular customer without B2B or vendor access.', 'b2bking' ); ?>
		</p>
	</div>
	<?php
}

add_action( 'admin_footer-user-edit.php', 'b2bking_custom_rejection_reason_script' );
function b2bking_custom_rejection_reason_script() {
	?>
	<script>
	(function () {
		function initializeB2BKingRejection() {
			const groupSelect = document.querySelector('.b2bking_user_registration_user_data_container_element_select_group');
			const reasonWrap = document.getElementById('b2bking-custom-rejection-reason-wrap');
			const reasonField = document.getElementById('b2bking-custom-rejection-reason');
			const approveButton = document.querySelector('.b2bking_user_registration_user_data_container_element_approval_button_approve');

			if (!groupSelect || !reasonWrap || !reasonField || !approveButton || !window.jQuery || !window.b2bking) {
				return;
			}

			const customerOption = groupSelect.querySelector('option[value="b2c"]');
			if (!customerOption) {
				return;
			}

			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';

			function updateRejectionInterface() {
				const rejectingApplication = groupSelect.value === 'b2c';
				reasonWrap.style.display = rejectingApplication ? 'block' : 'none';
				if (buttonTextNode) {
					buttonTextNode.textContent = rejectingApplication ? ' Reject vendor application' : ' ' + originalButtonLabel;
				}
			}

			groupSelect.addEventListener('change', updateRejectionInterface);
			updateRejectionInterface();

			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: 'b2bkingapproveuser',
					security: window.b2bking.security,
					chosen_group: 'b2c',
					user: document.getElementById('b2bking_user_registration_data_id').value,
					b2bking_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);
		}

		if (document.readyState === 'loading') {
			document.addEventListener('DOMContentLoaded', initializeB2BKingRejection);
		} else {
			initializeB2BKingRejection();
		}
	}());
	</script>
	<?php
}

add_action( 'b2bking_approved_user_as_b2c', 'b2bking_custom_send_rejection_reason', 10, 2 );
function b2bking_custom_send_rejection_reason( $user_id, $email_address ) {
	if ( empty( $_POST['b2bking_rejection_reason'] ) ) {
		return;
	}

	$reason = sanitize_textarea_field( wp_unslash( $_POST['b2bking_rejection_reason'] ) );
	$user   = get_userdata( $user_id );

	if ( ! $user || ! is_email( $email_address ) || '' === $reason ) {
		return;
	}

	update_user_meta( $user_id, 'b2bking_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 B2B and vendor access have not been enabled.</p>';

	if ( function_exists( 'WC' ) && WC()->mailer() ) {
		$message = WC()->mailer()->wrap_message( 'Vendor application update', $message );
	}

	wp_mail(
		$email_address,
		$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