Skip to content

Instantly share code, notes, and snippets.

@claudiosanches
Created November 7, 2019 20:40
Show Gist options
  • Select an option

  • Save claudiosanches/27918ec8a28fd1650b6a2fbc6dfed767 to your computer and use it in GitHub Desktop.

Select an option

Save claudiosanches/27918ec8a28fd1650b6a2fbc6dfed767 to your computer and use it in GitHub Desktop.

Revisions

  1. claudiosanches created this gist Nov 7, 2019.
    55 changes: 55 additions & 0 deletions woocommerce-pay-button-sample.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    <?php
    /**
    * Plugin Name: Test "pay button" support
    */

    add_action( 'plugins_loaded', function() {
    class My_Custom_Gateway extends WC_Payment_Gateway {
    public function __construct() {
    $this->id = 'custom_gateway';
    $this->has_fields = false;
    $this->method_title = 'Custom Gateway';
    $this->method_description = 'Custom Gateway for tests';
    $this->pay_button_id = 'custom-pay-button'; // Pay button ID.
    $this->supports = array(
    'products',
    'pay_button', // Pay button supporte declared.
    );

    // Load the settings.
    $this->init_form_fields();
    $this->init_settings();
    }

    /**
    * Initialise Gateway Settings Form Fields.
    */
    public function init_form_fields() {
    $this->form_fields = array(
    'enabled' => array(
    'title' => 'Enable/Disable',
    'type' => 'checkbox',
    'label' => '',
    'default' => 'yes',
    ),
    );
    }
    }

    // Register class.
    add_filter(
    'woocommerce_payment_gateways',
    function( $gateways ) {
    $gateways[] = 'My_Custom_Gateway';

    return $gateways;
    }
    );

    /**
    * Sample button implementation.
    */
    add_action( 'wp_footer', function() {
    echo '<script>jQuery( "#custom-pay-button" ).text( "Pay now" ).css( "color", "#fff" ).css( "background", "#222" ).css( "padding", "5px" ).css( "max-width", "150px" );</script>';
    } );
    }, 10 );