How to override class-wc-gateway-bacs.php file in Woocommerce

I have a problem regarding overriding the file in woocommerce/includes/gateways/bacs/class-wc-gateway-bacs.php

I have edited this file and added some other fields and changed placement order. I have tried the following locations in an attempt to use my version:

Read More
  • mytheme/woocommerce/includes/gateways/bacs/class-wc-gateway-bacs.php
  • mytheme/woocommerce/includes/gateways/class-wc-gateway-bacs.php
  • mytheme/woocommerce/includes/bacs/class-wc-gateway-bacs.php

None of the above locations worked. In my previous job I successfully overrode some of the files in woo-commerce by placing overloads within the /templates/ folder to do the trick but this time it is more complicated.

I didn’t want to replace the original file to prevent my custom edits being overwritten when the plugin is updated.

Is there’s a way to override it?

Related posts

Leave a Reply

1 comment

  1. Hi Mosak i think you are working on the wrong side. Due to extend Woocommerce payment gateways you cannot override the plugin classes working on templates but you should create your own plugin hooking your code to the filter used by woocommerce to recognize the registered payment gateways.

    add_action('plugins_loaded', 'init_my_gateway_class');
    
    function init_my_gateway_class() {
        add_filter('woocommerce_payment_gateways', 'add_this_class_to_gateway_class');
    
        /**
        * @param array $methods
        * @return string
        */
        function add_this_class_to_gateway_class($methods){
            $methods[] = 'WC_My_Gateway';
            return $methods;
        }
    
        class WC_My_Gateway extends WC_Payment_Gateway{
           /**
            * Here you can create your own payment gateway
            * defining the specs on the constructor.
            **/
        }
    }
    

    The class WC_My_Gateway, hypothetically, could be your customization of the original bacs class, mantaining compatibility and leaving the original bacs class active.