Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cyberwani/c2c5c7fd3a49169f85cddcc640da019b to your computer and use it in GitHub Desktop.
Save cyberwani/c2c5c7fd3a49169f85cddcc640da019b to your computer and use it in GitHub Desktop.
WooCommerce - Pay Button sample
<?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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment