Disable COD for specific customers in Woocommerce

Last edited:
November 30, 2023
Reading time:
6 mins

/

Blog

/

WooCommerce

/

Disable COD for specific ...

đź’ˇWe may earn a commision if you subscribe to a service from a link on this page.

WooCommerce, as a powerful e-commerce platform, provides a plethora of features to streamline your online business. One such feature is Cash on Delivery (COD), allowing customers to pay for their orders upon delivery. However, there are situations where disabling COD for specific customers becomes a necessity. This article will guide you through the process of achieving this customization in WooCommerce.

Use Cases

1. Non-Reception of First Order

Consider a scenario where a customer fails to receive their first COD order. This can result in additional fees for the merchant, including shipping fees for the return trip. By selectively disabling COD for such customers, you can mitigate these potential costs and encourage more responsible purchasing behavior.

2. Incorrect Address Intentionally Provided

Customers may sometimes provide inaccurate addresses intentionally, leading to failed deliveries and additional expenses for the business. Disabling COD for customers who frequently provide incorrect addresses can help reduce the financial burden associated with returned orders.

3. Preventing Fraudulent Orders

In the world of e-commerce, fraudulent activities pose a significant threat. By identifying suspicious patterns or behaviors, you may want to disable COD for specific customers to minimize the risk of processing orders that could be fraudulent.

Adding Code

To implement the functionality of disabling COD for specific customers in WooCommerce, you need to add custom code to your WordPress theme’s functions.php file. Below are the steps involved:

Step 1: Access Your Theme’s functions.php

Navigate to your WordPress dashboard, go to Appearance > Theme Editor, and select your active theme. Locate the functions.php file for your child theme.

Step 2: Add the Custom Code

Create user custom field for payment restrictions

The reasons for payment restrictions may vary and the approach per restriction may be different as well. So, it’s better we choose a way to implement this feature that is going to allow us to specify the restriction. An easy method is to use an ID for each restriction. Let’s use a $restriction_id that is going to be an int number, between 0 and 3. 0 means all good, no restriction. 1 means there is a restriction. 2 and 3 are secondary restrictions which you can use per your liking.

Our first move is to add the custom field to the user profile:



add_action( 'edit_user_profile', 'show_profile_credibility_field',10 );

function show_profile_credibility_field( $user ) {
	$restriction_id = get_user_meta( $user->ID,'payment_restrictions',true );
	?>
	<h3><?php esc_html_e( 'Personal Information', 'crf' ); ?></h3>

	<table class="form-table">
		<tr>
			<th><label for="payment_restrictions"><?php esc_html_e( 'Failure to Collect', 'crf' ); ?></label></th>
			<td>
			    <input type="number"
			       min="0"
			       max="3"
			       step="1"
			       id="payment_restrictions"
			       name="payment_restrictions"
			       value="<?php echo esc_attr( $restriction_id ); ?>"
			       class="regular-text"/>
			</td>
		</tr>
	</table>
	<?php
}

add_action( 'personal_options_update', 'update_profile_fields_wpr' );
add_action( 'edit_user_profile_update', 'update_profile_fields_wpr' );

function update_profile_fields_wpr( $user_id ) {
	if ( ! current_user_can( 'edit_user', $user_id ) ) {
		return false;
	}

	if ( ! empty( $_POST['payment_restrictions'] ) ) {
		update_user_meta( $user_id, 'payment_restrictions', intval( $_POST['payment_restrictions'] ) );
	}
}


Unset Cash on Delivery if $restriction_id is 1

For our use case, we will unset Cash on Delivery if $restriction_id is 1. Below code will hide COD when the user has the restriction ID equal to 1:



add_filter( 'woocommerce_available_payment_gateways', 'disable_cod_for_unlegitimate_users' );
  
function disable_cod_for_unlegitimate_users( $available_gateways ) {
     global $current_user;
	$restriction_id = get_user_meta( $current_user->ID,'payment_restrictions',true );
	 
  

   if ( ! is_admin() && !current_user_can('administrator') ) {
        
      $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
        
      $chosen_shipping = $chosen_methods[0];
        
      if ( isset( $available_gateways['cod'] ) && ($restriction_id === '1') ){
         unset( $available_gateways['cod'] );
      }
	  }
   return $available_gateways;
}

Messages for the visitor

By all means, we should inform the visitor about the situation. After all, communication is everything, and that way you make sure false positives don’t have bad experiences. Use the code below to display messages according to the restriction ID of the current user:

add_action( 'woocommerce_before_checkout_form', 'check_customer_credibility', 10 );
function check_customer_credibility( ) {
global $current_user;
	$year = get_user_meta( $current_user->ID,'year_of_birth',true );
   if ($year === '1') {
echo '<p class="lead" style="color:red;">Attention! Because you failed to receive your last Cash on Delivery order, Cash on Delivery is not available for you until you fulfill an order with different type of payment.</p>';
   }
	else if ($year === '2') {
echo '<p class="lead" style="color:red;">Attention! Your account\'s ability to complete an order has been temporarily suspended because we noticed suspicious behaviour. Please call us for further details.</p>';
   }
}

Restrict COD in WooCommerce for single customers

Disabling COD for specific customers in WooCommerce is a valuable customization to enhance your e-commerce operations. By carefully implementing the provided code snippets and tailoring them to your specific requirements, you can address various use cases, from preventing additional costs due to undelivered COD orders to minimizing the risks associated with fraudulent activities. Remember to test the code thoroughly on a staging site before deploying it to your live store. Customizing WooCommerce in this manner empowers you to create a more secure and efficient online shopping experience for both you and your customers.

Leave a Reply

Your email address will not be published. Required fields are marked *