init(); } /** * Load gateways and hook in functions. */ public function init() { $load_gateways = array( 'WC_Gateway_BACS', 'WC_Gateway_Cheque', 'WC_Gateway_COD', 'WC_Gateway_Paypal', ); $simplify_countries = (array) apply_filters( 'woocommerce_gateway_simplify_commerce_supported_countries', array( 'US', 'IE' ) ); if ( in_array( WC()->countries->get_base_country(), $simplify_countries ) ) { if ( class_exists( 'WC_Subscriptions_Order' ) || class_exists( 'WC_Pre_Orders_Order' ) ) { if ( ! function_exists( 'wcs_create_renewal_order' ) ) { // Subscriptions < 2.0 $load_gateways[] = 'WC_Addons_Gateway_Simplify_Commerce_Deprecated'; } else { $load_gateways[] = 'WC_Addons_Gateway_Simplify_Commerce'; } } else { $load_gateways[] = 'WC_Gateway_Simplify_Commerce'; } } // Filter $load_gateways = apply_filters( 'woocommerce_payment_gateways', $load_gateways ); // Get sort order option $ordering = (array) get_option( 'woocommerce_gateway_order' ); $order_end = 999; // Load gateways in order foreach ( $load_gateways as $gateway ) { $load_gateway = is_string( $gateway ) ? new $gateway() : $gateway; if ( isset( $ordering[ $load_gateway->id ] ) && is_numeric( $ordering[ $load_gateway->id ] ) ) { // Add in position $this->payment_gateways[ $ordering[ $load_gateway->id ] ] = $load_gateway; } else { // Add to end of the array $this->payment_gateways[ $order_end ] = $load_gateway; $order_end++; } } ksort( $this->payment_gateways ); } /** * Get gateways. * * @access public * @return array */ public function payment_gateways() { $_available_gateways = array(); if ( sizeof( $this->payment_gateways ) > 0 ) { foreach ( $this->payment_gateways as $gateway ) { $_available_gateways[ $gateway->id ] = $gateway; } } return $_available_gateways; } /** * Get available gateways. * * @return array */ public function get_available_payment_gateways() { $_available_gateways = array(); foreach ( $this->payment_gateways as $gateway ) { if ( $gateway->is_available() ) { if ( ! is_add_payment_method_page() ) { $_available_gateways[ $gateway->id ] = $gateway; } elseif( $gateway->supports( 'add_payment_method' ) ) { $_available_gateways[ $gateway->id ] = $gateway; } } } return apply_filters( 'woocommerce_available_payment_gateways', $_available_gateways ); } /** * Set the current, active gateway. * * @param array $gateway Available payment gateways. */ public function set_current_gateway( $gateways ) { // Be on the defensive if ( ! is_array( $gateways ) || empty( $gateways ) ) { return; } $current = WC()->session->get( 'chosen_payment_method' ); if ( $current && isset( $gateways[ $current ] ) ) { $current_gateway = $gateways[ $current ]; } else { $current_gateway = current( $gateways ); } // Ensure we can make a call to set_current() without triggering an error if ( $current_gateway && is_callable( array( $current_gateway, 'set_current' ) ) ) { $current_gateway->set_current(); } } /** * Save options in admin. */ public function process_admin_options() { $gateway_order = isset( $_POST['gateway_order'] ) ? $_POST['gateway_order'] : ''; $order = array(); if ( is_array( $gateway_order ) && sizeof( $gateway_order ) > 0 ) { $loop = 0; foreach ( $gateway_order as $gateway_id ) { $order[ esc_attr( $gateway_id ) ] = $loop; $loop++; } } update_option( 'woocommerce_gateway_order', $order ); } }