Instantly share code, notes, and snippets.
Created Nov 7, 2019
WooCommerce - Pay Button sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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