I'm trying to create a custom admin page which has Woocommerce Stripe Payment gateway options. Specifically just the Stripe API keys related fields. Reason for this is I need to limit access to Woocommerce settings pages and only want to expose very few things, so bringing just the few things to a custom page seems like the simplest thing.
However, this has been trickier than I expected. I've been trying to do this through extending WC_Payment_Gateway class, but this now seems like the wrong idea. Since I don't think I can extend a specific Gateway's class? I would basically need to rewrite the Stripe plugin I guess.
Also thinking there must be an easier way. Has anyone done this before?
Is it possible to create a settings forms with Woocommerce Settings API outside of the normal WC settings page?
Using this method, I didn't actually even get the form to print out, just the fields (i.e. no Submit button).
class WoocommerceStripeInt extends WC_Payment_Gateway
{
public function __construct () {
$this->id = 'stripe';
$this->method_title = __( 'Stripe', 'woocommerce-gateway-stripe' );
/* translators: 1) link to Stripe register page 2) link to Stripe api keys page */
$this->method_description = __( 'Stripe works by adding payment fields on the checkout and then sending the details to Stripe for verification.', 'woocommerce-gateway-stripe' );
$this->has_fields = true;
// Load the settings fields.
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// Get setting values.
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->instructions = $this->get_option( 'instructions' );
$this->order_status = $this->get_option( 'testmode' );
add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
}
/**
* Initialise Gateway Settings Form Fields
*/
function init_form_fields() {
$this->form_fields = apply_filters(
'wc_stripe_settings', array(
'testmode' => [
'title' => __( 'Test mode', 'woocommerce-gateway-stripe' ),
'label' => __( 'Enable Test Mode', 'woocommerce-gateway-stripe' ),
'type' => 'checkbox',
'description' => __( 'Place the payment gateway in test mode using test API keys.', 'woocommerce-gateway-stripe' ),
'default' => 'yes',
'desc_tip' => true,
],
'test_publishable_key' => [
'title' => __( 'Test Publishable Key', 'woocommerce-gateway-stripe' ),
'type' => 'text',
'description' => __( 'Get your API keys from your stripe account. Invalid values will be rejected. Only values starting with "pk_test_" will be saved.', 'woocommerce-gateway-stripe' ),
'default' => '',
'desc_tip' => true,
],
'test_secret_key' => [
'title' => __( 'Test Secret Key', 'woocommerce-gateway-stripe' ),
'type' => 'password',
'description' => __( 'Get your API keys from your stripe account. Invalid values will be rejected. Only values starting with "sk_test_" or "rk_test_" will be saved.', 'woocommerce-gateway-stripe' ),
'default' => '',
'desc_tip' => true,
],
'publishable_key' => [
'title' => __( 'Live Publishable Key', 'woocommerce-gateway-stripe' ),
'type' => 'text',
'description' => __( 'Get your API keys from your stripe account. Invalid values will be rejected. Only values starting with "pk_live_" will be saved.', 'woocommerce-gateway-stripe' ),
'default' => '',
'desc_tip' => true,
],
'secret_key' => [
'title' => __( 'Live Secret Key', 'woocommerce-gateway-stripe' ),
'type' => 'password',
'description' => __( 'Get your API keys from your stripe account. Invalid values will be rejected. Only values starting with "sk_live_" or "rk_live_" will be saved.', 'woocommerce-gateway-stripe' ),
'default' => '',
'desc_tip' => true,
],
)
);
}
function admin_options() {
?>
<h2><?php _e('Stripe Settings','woocommerce'); ?></h2>
<table class="form-table">
<?php $this->generate_settings_html(); ?>
</table> <?php
}
}
Any help would be appreciated, thanks.
Related
This is my first WP plugin. And I have been able to get it working perfectly with the settings page and also showing the shipping method on the checkout page. On a requested feature I need to have another field in my settings page that will have multiple checkboxes to allow users to service types they prefer on their site. This is an example of what I am trying to achieve:
The shot above was taken on from the Woocommerce product settings page. Hence I had to look at the code from WC's files. I found the snippet below but I can't get it to work on my settings page:
array(
'title' => __( 'Enable reviews', 'woocommerce' ),
'desc' => __( 'Enable product reviews', 'woocommerce' ),
'id' => 'woocommerce_enable_reviews',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => 'start',
'show_if_checked' => 'option',
),
array(
'desc' => __( 'Show "verified owner" label on customer reviews', 'woocommerce' ),
'id' => 'woocommerce_review_rating_verification_label',
'default' => 'yes',
'type' => 'checkbox',
'checkboxgroup' => '',
'show_if_checked' => 'yes',
'autoload' => false,
),
array(
'desc' => __( 'Reviews can only be left by "verified owners"', 'woocommerce' ),
'id' => 'woocommerce_review_rating_verification_required',
'default' => 'no',
'type' => 'checkbox',
'checkboxgroup' => 'end',
'show_if_checked' => 'yes',
'autoload' => false,
),
All the fields I have currently displayed on my settings page was done in this manner:
$this->form_fields = array(
'is_enabled' => array(
'title' => __('Enable', 'plugin_id'),
'type' => 'checkbox',
'description' => __('Enable this shipping.','plugin_id'),
'default' => 'no'
),
'api_key' => array(
'title' => __('API Key', 'plugin_id'),
'type' => 'text',
'description' => __('Obtain API key from Lastmal DIP','plugin_id'),
'default' => null
),
...
)
How can I add the multiple checkboxes to my fields and save them along with the other settings I have?
I have checked many questions from this forum about this solution, but I could not get it at all. I'm learning how to overwrite some text from existing array in plugin without editing it's core files so I will not loose my changes when the core plugin is updated in wordpress.
What I have is this code right from the plugin. There is one string which I can not translate from po.edit. Please get that I'm just learning this solution so I do not know how to really get it at all so that's why I'm asking for your advice.
Core code
/**
* Initialize integration settings form fields.
*
* #return void
*/
public function init_form_fields() {
$this->form_fields = array(
'authorization' => array(
'title' => __( 'Authorization', 'woocommerce-bookings' ),
'type' => 'google_calendar_authorization',
),
'testing' => array(
'title' => __( 'Connect with a custom Google Calendar App', 'woocommerce-bookings' ),
'type' => 'title',
'description' => 'Enter the credentials below to use a custom Google Calendar API app. Disconnect existing connection to enter credentials.',
),
'client_id' => array(
'title' => __( 'Client ID', 'woocommerce-bookings' ),
'type' => 'text',
'description' => __( 'Enter the Google Client ID associated with your Calendar API app.', 'woocommerce-bookings' ),
'disabled' => $this->is_using_wooconnect_method(),
'desc_tip' => true,
'default' => '',
),
'client_secret' => array(
'title' => __( 'Client Secret', 'woocommerce-bookings' ),
'type' => 'text',
'description' => __( 'Enter the Google Client Secret associated with your Calendar API app.', 'woocommerce-bookings' ),
'disabled' => $this->is_using_wooconnect_method(),
'desc_tip' => true,
'default' => '',
),
'custom_authorization' => array(
'title' => __( 'Authorization', 'woocommerce-bookings' ),
'type' => 'custom_google_calendar_authorization',
),
'calendar_connection_settings' => array(
'title' => __( 'Connected Calendar Settings', 'woocommerce-bookings' ),
'type' => 'title',
'display_check' => array( $this, 'display_connection_settings' ),
),
'calendar_id' => array(
'title' => __( 'Calendar', 'woocommerce-bookings' ),
'type' => 'select',
'description' => __( 'Select your Calendar.', 'woocommerce-bookings' ),
'desc_tip' => true,
'default' => '',
'options' => $this->get_calendar_list_options(),
'display_check' => array( $this, 'display_connection_settings' ),
),
'sync_preference' => array(
'type' => 'select',
'title' => __( 'Sync Preference', 'woocommerce-bookings' ),
'options' => array(
'both_ways' => __( 'Sync both ways - between Store and Google', 'woocommerce-bookings' ),
'one_way' => __( 'Sync one way - from Store to Google', 'woocommerce-bookings' ),
),
'description' => __( 'Manage the sync flow between your Store calendar and Google calendar.', 'woocommerce-bookings' ),
'desc_tip' => true,
'default' => 'one_way',
'display_check' => array( $this, 'display_connection_settings' ),
),
'debug' => array(
'title' => __( 'Debug Log', 'woocommerce-bookings' ),
'type' => 'checkbox',
'label' => __( 'Enable logging', 'woocommerce-bookings' ),
'default' => 'no',
/* translators: 1: log file path */
'description' => sprintf( __( 'Log Google Calendar events, such as API requests, inside %s', 'woocommerce-bookings' ), '<code>woocommerce/logs/' . $this->id . '-' . sanitize_file_name( wp_hash( $this->id ) ) . '.txt</code>' ),
),
);
}
/**
* Generate Settings HTML.
*
* Extends base class html generation to add 'display_check' parameter to each
* field. 'display_check', is a callable that enables/disables the display of
* the field.
*
* #param array $form_fields (default: array()) Array of form fields.
* #param bool $echo Echo or return.
* #return string the html for the settings
* #since 1.15.0
*/
public function generate_settings_html( $form_fields = array(), $echo = true ) {
if ( empty( $form_fields ) ) {
$form_fields = $this->get_form_fields();
}
foreach ( $form_fields as $index => $field ) {
// Delete fields if they have an "enable_check" function that returns false.
if ( isset( $field['display_check'] ) && ! call_user_func( $field['display_check'] ) ) {
unset( $form_fields[ $index ] );
}
}
return parent::generate_settings_html( $form_fields, $echo );
}
Now there is a testing array in the core code where I need to change its description
'testing' => array(
'title' => __( 'Connect with a custom Google Calendar App', 'woocommerce-bookings' ),
'type' => 'title',
'description' => 'Enter the credentials below to use a custom Google Calendar API app. Disconnect existing connection to enter credentials.',
),
I have tried this but I think its not the right solution for my problem because I can not get it work.
add_filter( 'init_form_fields', 'replace_text', 11, 2 );
function replace_text( $form_fields ) {
$form_fields = array(
'testing'=> array(
'title'=> __( 'Connect with a custom Google Calendar App', 'woocommerce-bookings' ),
'type'=> 'title',
'description' => 'New description',
),
);
return $form_fields;
}
Could you please help me to find a right solution. Thank you
I am trying to add another WooCommerce payment method called xxxx Bank Gateway on checkout page and when selected, this payment method will load an iframe like below (the iframe will redirect to the banks site lets call it $url).
How can I declare the content that will be displayed below the Checkout option?
//Frontend:
<input id="payment_method_xxxx" type="radio" class="input-radio" name="payment_method" value="xxxx" data-order_button_text="">
<label for="payment_method_xxx">
xxx Payment </label>
Here is my code so far:
<?php
/*
Plugin Name: xxxxxxx WooCommerce Payment Gateway
Plugin URI: http://www.xxxxxxx.com/
Description: xxxxxxx enables merchants to authorize, settle and manage credit card and electronic check transactions via Web sites, retail stores, mail order/telephone order (MOTO) call centers and mobile devices.
Version: 1.0.0
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/*
* This action hook registers our PHP class as a WooCommerce payment gateway
*/
add_filter( 'woocommerce_payment_gateways', 'xxxxxxx_add_gateway_class' );
function xxxxxxx_add_gateway_class( $gateways ) {
$gateways[] = 'WC_xxxxxxx_Gateway'; // your class name is here
return $gateways;
}
/*
* The class itself, please note that it is inside plugins_loaded action hook
*/
add_action( 'plugins_loaded', 'xxxxxxx_init_gateway_class' );
function xxxxxxx_init_gateway_class() {
class WC_xxxxxxx_Gateway extends WC_Payment_Gateway {
/**
* Constructor
*/
public function __construct() {
// global ID
$this->id = 'xxxxxxx'; // payment gateway plugin ID
$this->icon = ''; // URL of the icon that will be displayed on checkout page near your gateway name
$this->has_fields = true; // in case you need a custom credit card form
// Show Title
$this->method_title = 'xxxxxxx Gateway';
// Show Description
$this->method_description = 'Description of xxxxxxx payment gateway'; // will be displayed on the options page
// setting defines
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option( 'title' );
// further check of SSL if you want
//add_action( 'admin_notices', array( $this, 'do_ssl_check' ) );
// This action hook saves the settings
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ));
}
public function init_form_fields(){
$this->form_fields = array(
'enabled' => array(
'title' => 'Enable/Disable',
'label' => 'Enable Epcipay Gateway',
'type' => 'checkbox',
'description' => '',
'default' => 'no'
),
'title' => array(
'title' => __( 'Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'xxxxxxx Payment', 'woocommerce' ),
'desc_tip' => true,
),
'description' => array(
'title' => __( 'Description', 'woocommerce' ),
'type' => 'textarea',
'default' => ''
),
'terminal_id' => array(
'title' => 'Terminal Id',
'type' => 'text'
),
'api_key' => array(
'title' => 'API Key',
'type' => 'password',
),
'password' => array(
'title' => 'Password',
'type' => 'text'
),
'pageid' => array(
'title' => 'Page Id',
'type' => 'text'
)
);
}
public function payment_fields() {
//how to add script tag to show the hosted checkout
// credit card form loaded from third party server -- Iframe
}
}
Good afternoon, I want to make simple copy of Cash on Delivery (COD) payment method in my store and rename it to Coupon on Delivery.
How to implement it?
Up
I have tried this code, but it shows error 500 on WC settings page:
<?php
/**
* Plugin Name: My New WooCommerce Gateway
* Plugin URI:
* Description: WooCommerce gateway to ....
* Author: .....
* Version: 1.0
* Author URI: https://example.org/
* Text Domain: woocommerce-my-gateway
* Domain Path: /languages/
*/
add_action( 'plugins_loaded', 'init_my_gateway_class' );
function init_my_gateway_class() {
if ( !class_exists( 'WooCommerce' ) ) return;
class WC_Gateway_COD_Renamed extends WC_Payment_Gateway {
}
}
function add_my_gateway_class( $methods ) {
$methods[] = 'WC_Gateway_my_gateway';
return $methods;
}
add_filter( 'woocommerce_payment_gateways', 'add_my_gateway_class' );
function my_load_textdomain(){
load_plugin_textdomain( 'woocommerce-my-gateway', false, dirname( plugin_dir_path( __FILE__ ) . '/languages/' ) );
}
add_action('plugins_loaded', 'my_load_textdomain');
Source of code
In your main plugin file you can include the class and filter the gateways at the same time:
function add_my_gateway_class( $methods ) {
include( 'class-wc-gateway-cod-renamed.php');
$methods[] = 'WC_Gateway_COD_Renamed';
return $methods;
}
add_filter( 'woocommerce_payment_gateways', 'add_my_gateway_class' );
There's no real need to check if WooCommerce is active this way as woocommerce_payment_gateways only exists if WooCommerce is running.
Then in another file called class-wc-gateway-cod-renamed.php you can define your class:
class WC_Gateway_COD_Renamed extends WC_Gateway_COD {
/**
* Setup general properties for the gateway.
*/
protected function setup_properties() {
$this->id = 'coupon-on-delivery';
$this->icon = apply_filters( 'woocommerce_coupon-on-deliver_icon', '' );
$this->method_title = __( 'Coupon on delivery', 'your-plugin' );
$this->method_description = __( 'Have your customers pay with a coupon upon delivery.', 'your-plugin' );
$this->has_fields = false;
}
/**
* Initialise Gateway Settings Form Fields.
*/
public function init_form_fields() {
$shipping_methods = array();
foreach ( WC()->shipping()->load_shipping_methods() as $method ) {
$shipping_methods[ $method->id ] = $method->get_method_title();
}
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'your-plugin' ),
'label' => __( 'Enable coupon on delivery', 'your-plugin' ),
'type' => 'checkbox',
'description' => '',
'default' => 'no',
),
'title' => array(
'title' => __( 'Title', 'your-plugin' ),
'type' => 'text',
'description' => __( 'Payment method description that the customer will see on your checkout.', 'your-plugin' ),
'default' => __( 'coupon on delivery', 'your-plugin' ),
'desc_tip' => true,
),
'description' => array(
'title' => __( 'Description', 'your-plugin' ),
'type' => 'textarea',
'description' => __( 'Payment method description that the customer will see on your website.', 'your-plugin' ),
'default' => __( 'Pay with coupon upon delivery.', 'your-plugin' ),
'desc_tip' => true,
),
'instructions' => array(
'title' => __( 'Instructions', 'your-plugin' ),
'type' => 'textarea',
'description' => __( 'Instructions that will be added to the thank you page.', 'your-plugin' ),
'default' => __( 'Pay with coupon upon delivery.', 'your-plugin' ),
'desc_tip' => true,
),
'enable_for_methods' => array(
'title' => __( 'Enable for shipping methods', 'your-plugin' ),
'type' => 'multiselect',
'class' => 'wc-enhanced-select',
'css' => 'width: 400px;',
'default' => '',
'description' => __( 'If coupon upon delivery is only available for certain methods, set it up here. Leave blank to enable for all methods.', 'your-plugin' ),
'options' => $shipping_methods,
'desc_tip' => true,
'custom_attributes' => array(
'data-placeholder' => __( 'Select shipping methods', 'your-plugin' ),
),
),
'enable_for_virtual' => array(
'title' => __( 'Accept for virtual orders', 'your-plugin' ),
'label' => __( 'Accept coupon if the order is virtual', 'your-plugin' ),
'type' => 'checkbox',
'default' => 'yes',
),
);
}
}
Extend the WC_Gateway_COD class so that you can inherit methods from it and only override the methods that have to do with the naming of things.
In WooCommerce plugin, you can enable payment gateway COD from admin section:
Admin >> WooCommerce >> Settings >> Checkout >> Cash on delivery.
Check option for enable COD.
Please refer below link for create offline payment gateway.
How to Create A WooCommerce Payment gateway
I've got this code here:
$defaults = array(
'id' => 'private_message-'.$user_id,
'component' => 'messages',
'must_be_logged_in' => true,
'block_self' => true,
'wrapper_id' => 'send-private-message-'.$user_id,
'wrapper_class' =>'send-private-message',
'link_href' => hibuddy_get_send_private_message_url(),
'link_title' => __( 'Ask the model a question about this item!', 'buddypress' ),
'link_text' => __( 'Send Seller a Private Message', 'buddypress' ),
'link_class' => 'send-message',
);
And I'm trying to change the line that says:
'link_text' => __( 'Send Seller a Private Message', 'buddypress' ),
...to:
'link_text' => __( 'echo do_shortcode('[button]'.Send Seller a Private Message.'[/button]')', 'buddypress' ),
...but it doesnt like it. In a nutshell, I'm trying to change the "Send Seller a Private Message" text to output [button]Send Seller a Private Message[/button] which requires the WordPress do_shortcode.
Thank you!
You can try this, but it probably won't work unless 'link_text' allows HTML instead of plain text:
'link_text' => __( do_shortcode('[button]Send Seller a Private Message[/button]'), 'buddypress' ),