Skip to content

Instantly share code, notes, and snippets.

@ChrisFlannagan
Last active April 6, 2018 00:17
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 ChrisFlannagan/8c91ec94489729c7afad47c1587c6359 to your computer and use it in GitHub Desktop.
Save ChrisFlannagan/8c91ec94489729c7afad47c1587c6359 to your computer and use it in GitHub Desktop.
<?php
namespace Project\WooCommerce\Gateways;
class Estimates extends \WC_Payment_Gateway {
const ID = 'estimates-gateway';
const WC_STATUS = 'wc-estimate';
public function __construct() {
$this->id = self::ID;
$this->icon = '';
$this->has_fields = false;
$this->method_title = __( 'Estimates', 'tribe' );
$this->method_description = __( 'Allows generating estimates before paying', 'tribe' );
add_filter( 'woocommerce_available_payment_gateways', [ $this, 'check_access' ], 10, 1 );
}
// Can the current user access this gateway?
public function check_access( $payment_gateways ) {
if ( is_admin() ) {
return $payment_gateways;
}
if ( ! is_user_logged_in() ) {
unset( $payment_gateways[ self::ID ] );
return $payment_gateways;
}
$user = User::factory( get_current_user_id() );
if ( ! $user->can_access_quotes() ) {
unset( $payment_gateways[ self::ID ] );
}
return $payment_gateways;
}
// runs when checking out with this gateway selected
public function process_payment( $order_id ){
$order = new \WC_Order( $order_id );
wc_reduce_stock_levels( $order_id );
$order->update_status( self::WC_STATUS, __( 'Awaiting estimate confirmation', 'tribe' ) );
WC()->cart->empty_cart();
return [
'result' => 'success',
'redirect' => some_custom_thank_you_page_url(),
];
}
// hooked into plugins_loaded
public static function hook(){
add_filter( 'wc_order_statuses', function( $statuses ) {
$statuses[ self::WC_STATUS ] = __( 'Estimate', 'tribe' );
return $statuses;
} );
add_filter( 'woocommerce_payment_gateways', function ( $gateways ){
array_unshift( $gateways, __CLASS__ );
return $gateways;
} );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment