-
-
Save AaronBowie/42785f985d4b965a806436dfdb572505 to your computer and use it in GitHub Desktop.
admin / team payment plugin
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: Admin Order Gateway | |
* Plugin URI: https://weareag.co.uk | |
* Description: Take admin/team payments | |
* Author: Aaron Bowie | |
* Author URI: http://weareag.co.uk | |
* Version: 0.1 | |
*/ | |
if( ! defined( 'ABSPATH' ) ) { | |
exit; // Exit if accessed directly. | |
} | |
add_filter( 'woocommerce_payment_gateways', 'ag_add_gateway_class' ); | |
function ag_add_gateway_class( $gateways ) { | |
$gateways[] = 'AG_admin_payment'; | |
return $gateways; | |
} | |
/** | |
* Credit Gateway. | |
* | |
* @class AG_admin_payment | |
* @extends WC_Payment_Gateway | |
* @version 0.1 | |
* @package WooCommerce\Classes\Payment | |
*/ | |
add_action( 'plugins_loaded', 'ag_init_gateway_class' ); | |
function ag_init_gateway_class() { | |
class AG_admin_payment extends WC_Payment_Gateway { | |
/** | |
* Constructor for the gateway. | |
*/ | |
public function __construct() { | |
// Setup general properties. | |
$this->setup_properties(); | |
// Load the settings. | |
$this->init_form_fields(); | |
$this->init_settings(); | |
// Get settings. | |
$this->title = $this->get_option( 'title' ); | |
$this->description = $this->get_option( 'description' ); | |
$this->instructions = $this->get_option( 'instructions' ); | |
$this->enable_for_methods = $this->get_option( 'enable_for_methods', array() ); | |
// Actions. | |
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) ); | |
add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) ); | |
add_filter( 'woocommerce_available_payment_gateways', array( $this, 'display_for_role_only' ) ); | |
} | |
function display_for_role_only( $available_gateways ) { | |
$user = wp_get_current_user(); | |
$allowed_roles = array( 'shop_manager', 'administrator', 'credit', 'trade' ); | |
if( isset( $available_gateways['AG_admin_payment'] ) && ! array_intersect( $allowed_roles, $user->roles ) ) { | |
unset( $available_gateways['AG_admin_payment'] ); | |
} | |
return $available_gateways; | |
} | |
/** | |
* Setup general properties for the gateway. | |
*/ | |
protected function setup_properties() { | |
$this->id = 'AG_admin_payment'; | |
$this->icon = apply_filters( 'woocommerce_cod_icon', '' ); | |
$this->method_title = __( 'Admin Payment', 'woocommerce' ); | |
$this->method_description = __( 'Allow admin / team members to pay on account.', 'woocommerce' ); | |
$this->has_fields = FALSE; | |
} | |
/** | |
* Initialise Gateway Settings Form Fields. | |
*/ | |
public function init_form_fields() { | |
$this->form_fields = array( | |
'enabled' => array( | |
'title' => __( 'Enable/Disable', 'woocommerce' ), | |
'label' => __( 'Enable AG Account Payment', 'woocommerce' ), | |
'type' => 'checkbox', | |
'description' => '', | |
'default' => 'no', | |
), | |
'title' => array( | |
'title' => __( 'Title', 'woocommerce' ), | |
'type' => 'safe_text', | |
'description' => __( 'Payment method description that the customer will see on your checkout.', 'woocommerce' ), | |
'default' => __( 'Team Payment', 'woocommerce' ), | |
'desc_tip' => TRUE, | |
), | |
'description' => array( | |
'title' => __( 'Description', 'woocommerce' ), | |
'type' => 'textarea', | |
'description' => __( 'Payment method description that the customer will see on your website.', 'woocommerce' ), | |
'default' => __( 'Team payment', 'woocommerce' ), | |
'desc_tip' => TRUE, | |
), | |
'instructions' => array( | |
'title' => __( 'Instructions', 'woocommerce' ), | |
'type' => 'textarea', | |
'description' => __( 'Instructions that will be added to the thank you page.', 'woocommerce' ), | |
'default' => __( 'team payment', 'woocommerce' ), | |
'desc_tip' => TRUE, | |
), | |
); | |
} | |
/** | |
* Process the payment and return the result. | |
* | |
* @param int $order_id Order ID. | |
* | |
* @return array | |
*/ | |
public function process_payment( $order_id ) { | |
$order = wc_get_order( $order_id ); | |
$order->update_status( 'pending' ); | |
$order->add_order_note( __( 'This order was generated by the shop team to process a MOTO order.', 'woocommerce' ) ); | |
// Remove cart. | |
WC()->cart->empty_cart(); | |
// Return thankyou redirect. | |
return array( | |
'result' => 'success', | |
'redirect' => $this->get_return_url( $order ), | |
); | |
} | |
/** | |
* Output for the order received page. | |
*/ | |
public function thankyou_page() { | |
if( $this->instructions ) { | |
echo wp_kses_post( wpautop( wptexturize( $this->instructions ) ) ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment