function wc_add_special_to_gateways( $gateways ) {
$gateways[] = 'WC_Gateway_Special';
return $gateways;
}
add_filter( 'woocommerce_payment_gateways', 'wc_add_special_to_gateways' );
add_action( 'plugins_loaded', 'wc_special_gateway_init', 11 );
function wc_special_gateway_init() {
class WC_Gateway_Special extends WC_Payment_Gateway {
public $domain;
public function __construct() {
$this->id = 'gateway-special';
$this->domain = 'wcpg-special';
$this->icon = apply_filters('woocommerce_payment_gateway_icon', '');
$this->has_fields = false;
$this->method_title = __( 'Special Gateway', $this->domain );
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option( 'title' );
$this->order_status = $this->get_option( 'order_status' );
$this->status_text = $this->get_option( 'status_text' );
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
add_action( 'woocommerce_checkout_create_order', array( $this, 'save_order_payment_type_meta_data' ), 10, 2 );
add_filter( 'woocommerce_get_order_item_totals', array( $this, 'display_transaction_type_order_item_totals'), 10, 3 );
add_action( 'woocommerce_admin_order_data_after_billing_address', array( $this, 'display_payment_type_order_edit_pages'), 10, 1 );
add_action( 'woocommerce_thankyou_' . $this->id, array( $this, 'thankyou_page' ) );
add_action( 'woocommerce_email_before_order_table', array( $this, 'email_instructions' ), 10, 3 );
}
public function init_form_fields() {
$this->form_fields = apply_filters( 'wc_special_payment_form_fields', array(
'enabled' => array(
'title' => __( 'Enable/Disable', $this->domain ),
'type' => 'checkbox',
'label' => __( 'Enable Special Payment', $this->domain ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', $this->domain ),
'type' => 'text',
'description' => __( 'This controls the title for the payment method the customer sees during checkout.', $this->domain ),
'default' => __( 'Special Payment', $this->domain ),
'desc_tip' => true,
),
'order_status' => array(
'title' => __( 'Order Status', $this->domain ),
'type' => 'select',
'description' => __( 'Choose whether order status you wish after checkout.', $this->domain ),
'default' => 'wc-completed',
'desc_tip' => true,
'class' => 'wc-enhanced-select',
'options' => wc_get_order_statuses()
),
'status_text' => array(
'title' => __( 'Order Status Text', $this->domain ),
'type' => 'text',
'description' => __( 'Set the text for the selected order status.', $this->domain ),
'default' => __( 'Order is completed', $this->domain ),
'desc_tip' => true,
),
) );
}
public function save_order_payment_type_meta_data( $order, $data ) {
if ( $data['payment_method'] === $this->id && isset($_POST['transaction_type']) )
$order->update_meta_data('_transaction_type', esc_attr($_POST['transaction_type']) );
}
public function thankyou_page( $order_id ) {
$order = wc_get_order( $order_id );
if ( $this->instructions ) {
echo wpautop( wptexturize( $this->instructions ) );
}
}
public function display_payment_type_order_edit_pages( $order ){
if( $this->id === $order->get_payment_method() && $order->get_meta('_transaction_type') ) {
$options = $this->options;
echo '<p><strong>'.__('Transaction type').':</strong> ' . $options[$order->get_meta('_transaction_type')] . '</p>';
}
}
public function display_transaction_type_order_item_totals( $total_rows, $order, $tax_display ){
if( is_a( $order, 'WC_Order' ) && $order->get_meta('_transaction_type') ) {
$new_rows = []; // Initializing
$options = $this->options;
foreach( $total_rows as $total_key => $total_values ) {
$new_rows[$total_key] = $total_values;
if( $total_key === 'payment_method' ) {
$new_rows['payment_type'] = [
'label' => __("Transaction type", $this->domain) . ':',
'value' => $options[$order->get_meta('_transaction_type')],
];
}
}
$total_rows = $new_rows;
}
return $total_rows;
}
public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
if ( $this->instructions && ! $sent_to_admin && $this->id === $order->get_payment_method()
&& $order->has_status( $this->order_status ) ) {
echo wpautop( wptexturize( $this->instructions ) ) . PHP_EOL;
}
}
public function process_payment( $order_id ) {
$order = wc_get_order( $order_id );
$order->update_status( $this->order_status, $this->status_text );
wc_reduce_stock_levels( $order->get_id() );
WC()->cart->empty_cart();
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
}
}