3

I'm trying to override the output HTML from the Woocommerce Stripe plugin. How would I go about doing that? I've read some articles and the docs about action hooks and filters but I believe those are only for the Woocommerce plugin alone and not the Woo/Stripe extension plugin. If a hook or filter is the solution what would that look like? I could modify core files or use JS but those aren't real viable solutions at all...

The file I'm trying to modify is located in:

plugins/woocommerce-gateway-stripe/includes/class-wc-gateway-stripe.php

This is the block of markup I'm looking to modify within that file:

/**
 * Payment form on checkout page
 */
public function payment_fields() {
    $checked = 1;
    ?>
    <fieldset>
        <?php
            if ( $this->description ) {
                echo wpautop( esc_html( $this->description ) );
            }
            if ( $this->saved_cards && is_user_logged_in() && ( $customer_id = get_user_meta( get_current_user_id(), '_stripe_customer_id', true ) ) && is_string( $customer_id ) && ( $cards = $this->get_saved_cards( $customer_id ) ) ) {
                ?>
                <p class="form-row form-row-wide">
                    <a class="button" style="float:right;" href="<?php echo apply_filters( 'wc_stripe_manage_saved_cards_url', get_permalink( get_option( 'woocommerce_myaccount_page_id' ) ) ); ?>#saved-cards"><?php _e( 'Manage cards', 'woocommerce-gateway-stripe' ); ?></a>
                    <?php if ( $cards ) : ?>
                        <?php foreach ( (array) $cards as $card ) : ?>
                            <label for="stripe_card_<?php echo $card->id; ?>">
                                <input type="radio" id="stripe_card_<?php echo $card->id; ?>" name="stripe_card_id" value="<?php echo $card->id; ?>" <?php checked( $checked, 1 ) ?> />
                                <?php printf( __( '%s card ending in %s (Expires %s/%s)', 'woocommerce-gateway-stripe' ), (isset( $card->type ) ? $card->type : $card->brand ), $card->last4, $card->exp_month, $card->exp_year ); ?>
                            </label>
                            <?php $checked = 0; endforeach; ?>
                    <?php endif; ?>
                    <label for="new">
                        <input type="radio" id="new" name="stripe_card_id" <?php checked( $checked, 1 ) ?> value="new" />
                        <?php _e( 'Use a new credit card', 'woocommerce-gateway-stripe' ); ?>
                    </label>
                </p>
                <?php
            }
        ?>
        <div class="stripe_new_card" <?php if ( $checked === 0 ) : ?>style="display:none;"<?php endif; ?>
            data-description=""
            data-amount="<?php echo $this->get_stripe_amount( WC()->cart->total ); ?>"
            data-name="<?php echo sprintf( __( '%s', 'woocommerce-gateway-stripe' ), get_bloginfo( 'name' ) ); ?>"
            data-label="<?php _e( 'Confirm and Pay', 'woocommerce-gateway-stripe' ); ?>"
            data-currency="<?php echo strtolower( get_woocommerce_currency() ); ?>"
            data-image="<?php echo $this->stripe_checkout_image; ?>"
            >
            <?php if ( ! $this->stripe_checkout ) : ?>
                <?php $this->credit_card_form( array( 'fields_have_names' => false ) ); ?>
            <?php endif; ?>
        </div>
    </fieldset>
    <?php
}
Corey Bruyere
  • 786
  • 1
  • 10
  • 21

1 Answers1

6

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

<?php
class 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";
    }
}
doublesharp
  • 26,888
  • 6
  • 52
  • 73
  • Thanks! I believe I'm close. I've activated the plugin but when I get to the page where that function is ran (Checkout page, where stripe plugin is used) I get a blank page. I think the problem is somewhere in the 'my-stripe-gateway-class' file. I can echo out text from that file but once I try extending that class it breaks. Here's the repo -> [Github Repo](https://github.com/coreybruyere/shay-designs/tree/master/_production/app/plugins/gateway) – Corey Bruyere Nov 10 '14 at 03:01
  • You need to extend `WC_Stripe`, I didn't know what the class was called when I answered. Your error log probably says something about this. https://github.com/coreybruyere/shay-designs/blob/master/_production/app/plugins/woocommerce-gateway-stripe/woocommerce-gateway-stripe.php#L36 – doublesharp Nov 10 '14 at 03:08
  • Ah ok I thought we were extending the class that the function was held in. I changed it to WC_Stripe and I'm not getting a blank page anymore but now I'm not seeing anything in that section on the frontend. It's removing the core functionality but not overriding it? Thanks again! – Corey Bruyere Nov 10 '14 at 04:02
  • I updated a few things in the code after testing it. There is still quite a bit of work that is needed before this will be actually functional. – doublesharp Nov 10 '14 at 07:03
  • I'm wondering if any of these [hooks](http://docs.woothemes.com/document/hooks/#section-3) will work. I'll have to try when I get home. But again, I think those are Woocommerce specific.. – Corey Bruyere Nov 11 '14 at 19:37
  • Those are all specific to WooCommerce and will only fire when there is a do_action() call for them. – doublesharp Nov 11 '14 at 19:42
  • Dang.. What other routes were you thinking of? Any other direction or suggestions would be greatly appreciated. – Corey Bruyere Nov 12 '14 at 00:52
  • The code in the example will work fine, you are just going to have to do more work to complete it. – doublesharp Nov 12 '14 at 01:02