How can I override core Woocommerce plugin function? How can I override core Woocommerce plugin function? wordpress wordpress

How can I override core Woocommerce plugin function?


You are correct that in this case you cannot modify the output due to the lack of action/filter hooks. This is not necessarily the case for all add-ons to WooCommerce (or other plugins for that matter), it just happens to be the case here.

What I would recommend is writing a plugin that extends the class provided by the Stripe gateway plugin. Override just the payment_fields() function to provide your own output - keep in mind you will still need to make sure everything that the base plugin needs is still there. Enable your plugin and select it as the payment type in the WooCommerce configuration.

Plugin, my-stripe-gateway.php

    require_once ABSPATH . '/wp-content/plugins/woocommerce-gateway-stripe/includes/class-wc-gateway-stripe.php';    if ( ! class_exists( 'WC_Gateway_Stripe' ) )        return;    require_once 'my-stripe-gateway-class.php';    /**    * Add the Gateway to WooCommerce    **/    function add_my_stripe_gateway( $methods ) {        $methods[] = 'My_Stripe_Gateway';        return $methods;    }    add_filter( 'woocommerce_payment_gateways', 'add_my_stripe_gateway' );}add_action( 'plugins_loaded', 'my_stripe_gateway_init', 0 );

Custom Payment Gateway, my-stripe-gateway-class.php

<?phpclass My_Stripe_Gateway extends WC_Gateway_Stripe // or whatever the class name is{    // call the parent constructor, doesn't happen by default    public function __construct(){        parent::__construct();        $this->id = 'my_stripe_gateway';        $this->method_title = 'My Stripe Gateway';        $this->method_description = 'Customized Stripe payment gateway.';    }    // override the payment_fields() function    public function payment_fields(){        // reproduce/modify this function in your class        echo "whatever";    }}