id = $id; $this->init = $init; $this->settings = $settings; $this->load_methods(); } /** * Loads all payment methods from the database * * @since 3.0.0 */ public function load_methods() { global $wpdb; $has_subscriptions = false; if ( class_exists( '\WC_Subscriptions_Cart' ) ) { if ( \WC_Subscriptions_Cart::cart_contains_subscription() ) { $has_subscriptions = true; } } $methods = $wpdb->get_results( "SELECT * FROM " . $wpdb->prefix . MAKECOMMERCE_TABLENAME ); if ( count( $methods ) ) { if ( is_admin() && $this->init == true && get_option( 'mc_banklinks_api_type' ) != get_option( 'mk_api_type', false ) ) { add_action( 'admin_notices', array( $this, 'banklinks_list_type_notice' ) ); } foreach ( $methods as $method ) { if ( $method->type == 'banklink'|| $method->type == 'other' ) { if ( !$has_subscriptions ) { $banklinks[] = $banklinks_grouped[$method->country][] = $method; } } elseif ( $method->type == 'card' ) { $cards[] = $method; } elseif( $method->type == 'payLater' ) { if ( !$has_subscriptions ) { $paylater[] = $paylater_grouped[$method->country][] = $method; } } } if ( isset( $this->settings['ui_chorder'] ) ) { if ( !$has_subscriptions && isset( $banklinks ) && isset( $banklinks_grouped ) ) { usort( $banklinks, array( $this, 'sort_banklinks' ) ); foreach( $banklinks_grouped as &$country ) { usort( $country, array( $this, 'sort_banklinks' ) ); } } } if ( !$has_subscriptions ) { if ( isset( $banklinks ) ) { $this->banklinks = $banklinks; } if ( isset( $banklinks_grouped ) ) { $this->banklinks_grouped = $banklinks_grouped; } if ( isset( $paylater ) ) { $this->paylater = $paylater; } if ( isset( $paylater_grouped ) ) { $this->paylater_grouped = $paylater_grouped; } } if ( isset( $cards ) ) { $this->cards = array_reverse( $cards ); } if ( is_admin() ) { remove_action( 'admin_notices', array( $this, 'empty_banklinks_notice' ), 30 ); } } elseif ( is_admin() && $this->init == true ) { add_action( 'admin_notices', array( $this, 'empty_banklinks_notice' ), 30 ); } } /** * Banklink sorting function * * @since 3.0.0 */ public function sort_banklinks( $a, $b ) { $order = array_map( 'trim', explode( ",", $this->settings['ui_chorder'] ) ); $posA = array_search( $a->name, $order ); $posB = array_search( $b->name, $order ); if ( $posA === $posB ) { return $a->id > $b->id ? 1 : -1; } if ( $posA === FALSE ) { return 1; } if ( $posB === FALSE ) { return -1; } return $posA > $posB ? 1 : -1; } /** * Notice for admin that banklinks are empty * * @since 3.0.0 */ public function empty_banklinks_notice() { ?>

customer ) { $customerCountry = strtolower( $woocommerce->customer->get_shipping_country() ); if ( array_key_exists( $customerCountry, $this->banklinks_grouped ) ) { return $customerCountry; } else { return 'other'; } } $localeToCountry = array( 'et' => 'ee', 'lv' => 'lv', 'lt' => 'lt', 'fi' => 'fi', ); $locale = \MakeCommerce\i18n::get_two_char_locale(); if ( array_key_exists( $local, $localeToCountry ) ) { return $localeToCountry[$locale]; } return key( $this->banklinks_grouped ); } /** * Show available payment methods in checkout * * @since 3.0.0 */ public function show_methods() { if ( !($this->settings['ui_mode'] == 'inline') ) { $this->hidden_select_box_payment_methods(); } $this->method_list(); $this->method_list_js(); if ( $this->settings['ui_open_by_default'] == 'yes' ) { \MakeCommerce::mc_enqueue_script( 'MC_DEFAULT_PM', dirname( __FILE__ ) . '/js/mc_default_pm.js', [ 'id' => $this->id ], [ 'jquery' ] ); } } /** * Display country selector for payment methods * * @since 3.0.1 */ private function method_list_countries() { //sort countries $this->sort_countries_list(); if ( ( empty( $this->settings['ui_widget_groupcountries'] ) || $this->settings['ui_widget_groupcountries'] == 'no' ) && ( !isset( $this->settings['ui_widget_countries_hidden'] ) || $this->settings['ui_widget_countries_hidden'] == 'no' ) ) { ?>
banklinks_grouped ) as $country ): ?> get_default_country() == $country ) echo 'checked="checked" '; ?>/>settings['ui_widget_countryselector'] == 'flag' ) { ?> settings['ui_widget_countryselector'] == 'dropdown' ) : ?>
settings['ui_payment_country_order'] ) ) { $order = explode( ',', $this->settings['ui_payment_country_order'] ); foreach ( $order as $key=>&$country ) { if ( !isset( $this->banklinks_grouped[$country] ) ) { unset( $order[$key] ); continue; } $country = strtolower( trim( $country ) ); } if ( !empty( $order ) ) { $this->banklinks_grouped = array_merge( array_flip( $order ), $this->banklinks_grouped ); } } } /** * Method list js functionality * * @since 3.0.1 */ private function method_list_js() { $country =$this->get_default_country(); $pick = true; if ( WC()->session->get( 'mc_selected_paylater_method' ) !== NULL ) { $country = WC()->session->get( 'mc_selected_paylater_method_country' ); $pick = false; } \MakeCommerce::mc_enqueue_script( 'MC_METHOD_LIST', dirname( __FILE__ ) . '/js/mc_method_list.js', [ 'id' => $this->id, 'country' => $country, 'settings' => $this->settings, 'banklinksgroupedcount' => count( $this->banklinks_grouped ), 'pick' => $pick ], [ 'jquery' ] ); echo '
'; } /** * Hidden selectbox for all payment methods * * @since 3.0.1 */ private function hidden_select_box_payment_methods() { ?> settings['ui_widget_groupcountries'] == 'yes' ) { $checked = ""; if ( $this->get_default_country() == $country ) { $checked = 'checked="checked"'; } if ( empty( $this->settings['ui_widget_groupcountries'] ) || $this->settings['ui_widget_groupcountries'] == 'no' ) { echo ''; } else { } echo ' '; } } /** * Display payment methods as a widget * * @since 3.0.0 */ private function method_list() { echo ''; } /** * List version of a method item * * @since 3.0.1 */ private function list_version_item( $method, $card = false ) { $banklink_id = $method->country.'_'.$method->name; if ( $card ) { $banklink_id = "card_" . $method->name; } $methodName = ""; if ( in_array( $this->settings['ui_inline_uselogo'], array( 'text', 'text_logo' ) ) ) { $methodName = $method->display_name; if ( count( $this->banklinks_grouped ) > 1 && !$card ) { $methodName .= ' (' . \MakeCommerce\i18n::get_country_name( $method->country ) . ')'; } } $logo = ""; if ( in_array( $this->settings['ui_inline_uselogo'], array( 'logo', 'text_logo' ) ) ) { if ( $methodName == "" ) { $logo = ''; } else { $logo = ''; } } echo '
'; } /** * Widget version of a method item * * @since 3.0.1 */ private function widget_version_item( $method, $card = false ) { $banklink_id = $method->country.'_'.$method->name; if ( $card ) { $banklink_id = "card_" . $method->name; } echo ' '; } /** * Show list of banklinks * * @since 3.0.1 */ private function banklink_list( $methods ) { foreach ( $methods as $method ) { if ( $this->settings['ui_mode'] == 'inline' ) { $this->list_version_item( $method ); } else { $this->widget_version_item( $method ); } } } /** * Show list of paylater methodss * * @since 3.0.1 */ private function paylater_list( $cartTotal, $selected_country ) { if ( $this->paylater_grouped ) { echo '
'; } foreach ( $this->paylater_grouped as $country => $methods ) { if ( $country == $selected_country ) { foreach ( $methods as $method ) { if ( ( $method->min_amount < $cartTotal && $method->max_amount == 0 ) || ( $method->min_amount < $cartTotal && $method->max_amount > $cartTotal ) ) { if ( $this->settings['ui_mode'] == 'inline' ) { $this->list_version_item( $method ); } else { $this->widget_version_item( $method ); } } } if ( $country == 'other' ) { echo '

'. _e( 'No payment methods for selected country' ,'wc_makecommerce_domain' ) .'

'; } } } } /** * Show list of credit cards * * @since 3.0.1 */ private function creditcard_list( $country ) { if ( $this->cards ) { echo '
'; foreach ( $this->cards as $method ) { if ( $this->settings['ui_mode'] == 'inline' ) { $this->list_version_item( $method, true ); } else { $this->widget_version_item( $method, true ); } } } else if ( $country == 'other' ) { echo '

'. _e( 'No payment methods for selected country' ,'wc_makecommerce_domain' ) .'

'; } } }