Typek – Blog

How to Remove the Coupon Code in WooCommerce Using PHP Snippet

Introduction

WooCommerce is a popular e-commerce plugin for WordPress that allows you to create an online store and sell products or services. One of the features of WooCommerce is the ability to offer coupon codes to customers for discounts or other promotions. However, there might be instances where you want to remove the coupon code functionality from your store. In this tutorial, we will learn how to remove the coupon code in WooCommerce using a PHP snippet.

Prerequisites

Before proceeding with this tutorial, make sure you have the following:

  • A WordPress website with WooCommerce installed and activated.
  • Access to your website’s files, either through FTP or your hosting control panel’s file manager.
  • Basic understanding of PHP and WordPress hooks.

Understanding WooCommerce Coupon Code Functionality

By default, WooCommerce allows customers to enter a coupon code during the checkout process. This is done through the use of a form field where customers can input the coupon code and apply it to their order. The coupon code form is displayed in the cart and the checkout page. To remove the coupon code functionality, we need to hide the form field and disable the ability to apply a coupon code.

Removing the Coupon Code Form in WooCommerce

To remove the coupon code form from the cart and checkout pages, we can use a PHP snippet that utilizes WooCommerce hooks. We will add this PHP snippet to our theme’s functions.php file or create a custom plugin to implement it.

Step 1: Open your theme’s functions.php file

The first step is to open your theme’s functions.php file. You can find this file in the following path:


/wp-content/themes/your-theme/functions.php

Replace “your-theme” with the name of your active theme. If you’re using a child theme, make sure to edit the functions.php file of the child theme.

Step 2: Add the PHP snippet to remove the coupon code form

Add the following PHP snippet to the end of your functions.php file:


// Remove coupon code form from cart and checkout pages
function typek_remove_coupon_code_form() {
remove_action('woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10);
remove_action('woocommerce_before_cart', 'woocommerce_output_coupon_form', 10);
}
add_action('init', 'typek_remove_coupon_code_form');

This PHP snippet uses the remove_action() function to remove the coupon code form from the cart and checkout pages. The remove_action() function takes three arguments: the hook we want to remove the function from, the function we want to remove, and the priority of the action.

The typek_remove_coupon_code_form() function is hooked to the init action, which is triggered when WordPress initializes. This ensures that the coupon code form is removed before the pages are rendered.

Step 3: Save and test the changes

Save the changes to your functions.php file and visit your website’s cart and checkout pages. You should no longer see the coupon code form.

Disable Coupon Code Functionality in WooCommerce

Hiding the coupon code form only removes the visual aspect of the coupon code functionality. Customers may still be able to apply a coupon code through other means, such as URL parameters. To completely disable the coupon code functionality, we can use another PHP snippet that hooks into the WooCommerce coupon validation process.

Step 1: Add the PHP snippet to disable coupon code validation

Add the following PHP snippet to the end of your functions.php file:


// Disable coupon code validation
function typek_disable_coupon_code_validation($is_valid, $coupon) {
return false;
}
add_filter('woocommerce_coupon_is_valid', 'typek_disable_coupon_code_validation', 10, 2);

This PHP snippet uses the add_filter() function to hook into the woocommerce_coupon_is_valid filter, which is responsible for validating coupon codes. The typek_disable_coupon_code_validation() function takes two arguments: a boolean value indicating whether the coupon code is valid, and the coupon object.

By returning false in the typek_disable_coupon_code_validation() function, we ensure that all coupon codes are treated as invalid, effectively disabling the coupon code functionality.

Step 2: Save and test the changes

Save the changes to your functions.php file and try applying a coupon code using a URL parameter or any other method. You should find that the coupon code is not applied, and the order total remains unchanged.

Conclusion

In this tutorial, we learned how to remove the coupon code functionality in WooCommerce using PHP snippets. We removed the coupon code form from the cart and checkout pages, and disabled the coupon code validation process to ensure that no coupon codes can be applied. By following these steps, you can easily remove the coupon code functionality from your WooCommerce store.

More traffic??

Are you looking for effective solutions to increase your Google search visibility?

Contact us and discover the full potential of online sales.

What our clients are say?

For me, they are a happiness factory. They managed my store’s website in such a way that users became happy, after which it turned out that the search engine was also happy. When the search engine became happy and started bringing me more happy customers, my business became equally happy 🙂

Matt

Great company, I recommend it to everyone. Services of the highest standard. Very good customer approach. I am very satisfied with their work.

Lukasz Szajny

Complete professionalism and an incredible commitment to their work. It’s worth emphasizing their hassle-free communication and full availability. I definitely recommend them.

Alexander