Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * WooCommerce Gateways By Location
- *
- * @author Store Apps
- * @category Admin
- * @package WooCommerce Gateways By Location
- * @version 1.2.1
- */
- if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
- if ( ! class_exists( 'SA_WC_Gateways_By_Location' ) ) {
- /**
- * Class SA_WC_Gateways_By_Location
- */
- class SA_WC_Gateways_By_Location {
- private $version = '1.2';
- private $plugin_url;
- private $text_domain = 'wc_gateways_by_location';
- function __construct() {
- $this->plugin_url = plugins_url('', dirname(__FILE__));
- add_action( 'init', array( $this, 'localize' ) );
- add_filter('woocommerce_available_payment_gateways',array( $this, 'display_gateway_by_location' ) );
- if (is_admin() && current_user_can( 'manage_options' ) ) {
- add_action( 'admin_enqueue_scripts', array( &$this, 'load_js_css' ) );
- add_action( 'woocommerce_admin_field_payment_gateways', array( $this, 'gateway_by_location_output' ), 20 );
- add_action( 'woocommerce_update_options_checkout', array( $this, 'save' ), 1000 );
- }
- }
- function localize() {
- load_plugin_textdomain( $this->text_domain, false, dirname( plugin_basename( __FILE__ ) ).'/languages/' );
- }
- function load_js_css() {
- global $woocommerce;
- if ( version_compare($woocommerce->version, '2.3.0', '>=') ){
- wp_register_script( 'sa_gateways_by_location_chosen', $this->plugin_url . '/assets/js/chosen.jquery.min.js', array( 'jquery' ), $this->version );
- }
- wp_register_script( 'sa_gateways_by_location_js', $this->plugin_url . '/assets/js/admin.js', array( 'jquery' ), $this->version );
- wp_enqueue_style( 'sa_gateways_by_location_css', $this->plugin_url . '/assets/css/admin.css', array(), $this->version );
- wp_enqueue_style( 'dashicons' );
- }
- function gateway_by_location_output ( $settings ){
- global $woocommerce;
- $woocommerce = function_exists('WC') ? WC() : $GLOBALS['woocommerce']; // Before WC 2.1.x
- // Enqueue chosen when WC2.3 or above
- if (version_compare($woocommerce->version, '2.3.0', '>=')){
- if ( ! wp_script_is( 'sa_gateways_by_location_chosen' ) ) {
- wp_enqueue_script( 'sa_gateways_by_location_chosen' );
- }
- }
- if ( ! wp_script_is( 'sa_gateways_by_location_js' ) ) {
- wp_enqueue_script( 'sa_gateways_by_location_js' );
- }
- $available_gateways = (array) $woocommerce->payment_gateways->payment_gateways();
- $gateway_locations = get_option( 'sa_wc_gateways_by_location', null );
- $gateway_locations_search_in = get_option( 'sa_wc_gateways_by_location_search_in', null );
- if( empty( $gateway_locations_search_in ) ) {
- $gateway_locations_search_in['address'] = 'billing';
- update_option( 'sa_wc_gateways_by_location_search_in', $gateway_locations_search_in );
- }
- ?>
- <tr valign="top">
- <th scope="row" class="titledesc"><?php _e( 'Gateway By Location', $this->text_domain ) ?></th>
- <td class="forminp gateway_by_location">
- <table id="gateway_by_location" class="wc_gateways_by_location widefat" cellspacing="0">
- <thead>
- <tr>
- <td>
- <span class='search_in'><label><?php _e( 'Address to look in', $this->text_domain ) ?></label></span>
- </td>
- <td>
- <label for="billing">
- <input type="radio" name="sa_wc_gateways_by_location_search_in[address]" value="billing" <?php ( !empty( $gateway_locations_search_in['address'] ) ) ? checked( $gateway_locations_search_in['address'], 'billing' ) : ''; ?> />
- <?php _e( 'Billing', $this->text_domain ); ?>
- </label>
- <label for="shipping">
- <input type="radio" name="sa_wc_gateways_by_location_search_in[address]" value="shipping" <?php ( !empty( $gateway_locations_search_in['address'] ) ) ? checked( $gateway_locations_search_in['address'], 'shipping' ) : ''; ?> />
- <?php _e( 'Shipping', $this->text_domain ); ?>
- </label>
- </td>
- </tr>
- <tr>
- <?php
- $columns = array(
- 'gateway_name' => __( 'Gateway', $this->text_domain ),
- 'available' => __( 'Avaliable Only In', $this->text_domain ),
- 'not_available' => __( 'Not Avaliable In', $this->text_domain ),
- 'action' => __('Action', $this->text_domain),
- );
- foreach ( $columns as $key => $column ) {
- if( esc_attr( $key ) == 'action'){
- echo '<th class="' . esc_attr( $key ) . '" colspan="2">' . esc_html( $column ) . '</th>';
- } else {
- echo '<th class="' . esc_attr( $key ) . '">' . esc_html( $column ) . '</th>';
- }
- }
- ?>
- </tr>
- <tbody>
- <?php
- echo '<tr class="location">';
- echo '<td>';
- echo '<select id="gateway_list">';
- echo '<option value="select">'.__( 'Select Gateway', $this->text_domain ).'</option>';
- foreach ($available_gateways as $gateway) {
- if ( $gateway->enabled == 'yes' ) {
- echo '<option value="' . $gateway->id . '">' . esc_html( $gateway->title ) . '</option>';
- }
- }
- echo '</select>';
- echo '</td>';
- echo '<td>';
- echo '<select id="avail_in" name="sa_wc_gateways_by_location['.$gateway->id.'"][available_in]" data-placeholder="' . __( 'Select Countries', $this->text_domain ) .'" multiple="multiple" class="sa_gbl_location" >';
- foreach( $woocommerce->countries->countries as $code => $country ){
- echo '<option value="' . esc_html( $code ) . '">' . $country . '</option>';
- }
- echo '</select></td>';
- echo '<td>';
- echo '<select id="not_avail_in" name="sa_wc_gateways_by_location['.$gateway->id.'"][not_available_in]" data-placeholder="' . __( 'Select Countries', $this->text_domain ) .'" multiple="multiple" class="sa_gbl_location" >';
- foreach( $woocommerce->countries->countries as $code => $country ){
- echo '<option value="' . esc_html( $code ) . '">' . $country . '</option>';
- }
- echo '</select></td>';
- echo '<td colspan="2"><a class="button add_row">'.__( 'Add Rule', $this->text_domain ).'</a></td>';
- echo '</tr>';
- if( !empty( $gateway_locations ) && is_array( $gateway_locations )){
- foreach ($gateway_locations as $key => $value) {
- $avail_list = $not_avail_list = array();
- echo '<tr><td>'. esc_html( $value['title'] ) . '</td>';
- $avail_in = array_filter(explode(',', $value['available_in'] ));
- $not_avail_in = array_filter(explode(',', $value['not_available_in'] ));
- if( !empty( $avail_in ) ){
- for ($i=0; $i < count( $avail_in ); $i++) {
- if (!empty($woocommerce->countries->countries[$avail_in[$i]])) {
- $avail_list[] = $woocommerce->countries->countries[$avail_in[$i]];
- }
- }
- echo '<td>' . implode(', ', $avail_list) . '</td>';
- } else {
- echo '<td>'.__( 'All', $this->text_domain ).'</td>';
- }
- if( !empty( $not_avail_in ) ) {
- for ($j=0; $j < count( $not_avail_in ); $j++) {
- if (!empty($woocommerce->countries->countries[$not_avail_in[$j]])) {
- $not_avail_list[] = $woocommerce->countries->countries[$not_avail_in[$j]];
- }
- }
- echo '<td>' . implode(', ', $not_avail_list) . '</td>';
- } else {
- echo '<td>'.__('-', $this->text_domain) .'</td>';
- }
- echo '<td class="edit_row" title="'.__('Edit Rule', $this->text_domain) .'"></td>' ;
- echo '<td class="delete_row" title="'.__('Delete Rule', $this->text_domain) .'"></td>' ;
- echo '<input type="hidden" class="gateway_id" name="sa_wc_gateways_by_location[' . $key . '][title]" value="' . esc_html( $value['title'] ) . '">';
- echo '<input type="hidden" class="avail_in" name="sa_wc_gateways_by_location[' . $key . '][available_in]" value="' . esc_html( $value['available_in'] ) . '">';
- echo '<input type="hidden" class="not_avail_in" name="sa_wc_gateways_by_location[' . $key . '][not_available_in]" value="' . esc_html( $value['not_available_in'] ) . '">';
- echo '</tr>';
- }
- }
- ?>
- </tbody>
- </table>
- <p><em><?php _e( 'Not adding rule for a gateway will show it for all countries.', $this->text_domain); ?></em></p>
- </td>
- </tr>
- <?php
- }
- function display_gateway_by_location( $payment_gateways ){
- if( isset( $_GET['page'] ) && $_GET['page'] == 'wc-settings') {
- return $payment_gateways;
- }
- $gateway_locations = get_option( 'sa_wc_gateways_by_location', null );
- $gateway_locations_search_in = get_option( 'sa_wc_gateways_by_location_search_in', null );
- $woocommerce = function_exists('WC') ? WC() : $GLOBALS['woocommerce']; //// Before WC 2.1.x
- // Collect Customer country
- if( !empty( $gateway_locations_search_in ) && $gateway_locations_search_in['address'] == 'billing' ){
- $country = ( ! empty( $woocommerce->customer->country ) ) ? $woocommerce->customer->country : $woocommerce->customer->get_country();
- } else {
- $country = ( ! empty( $woocommerce->customer->shipping_country ) ) ? $woocommerce->customer->shipping_country : $woocommerce->customer->get_shipping_country();
- }
- if (! empty($country) && !empty($gateway_locations) && is_array($gateway_locations) ) {
- foreach ( $gateway_locations as $code => $location_rules ) {
- if( !empty ( $location_rules ) ) {
- if(
- ( !empty( $location_rules['available_in'] ) && strpos( ",{$location_rules['available_in']},", ",{$country}," ) === false ) ||
- ( !empty($location_rules['not_available_in']) && strpos( ",{$location_rules['not_available_in']},", ",{$country}," ) !== false )
- ){
- unset( $payment_gateways[ $code ] );
- }
- }
- }
- }
- return $payment_gateways;
- }
- function save(){
- if( empty( $_GET['page'] ) || $_GET['page'] != 'wc-settings' || empty($_GET['tab']) || $_GET['tab'] != 'checkout' || !empty($_GET['section']) ) {
- return;
- }
- if ( array_key_exists('sa_wc_gateways_by_location', $_POST) ) {
- $gateway_by_locations = $_POST['sa_wc_gateways_by_location'];
- if( !empty( $gateway_by_locations ) && is_array( $gateway_by_locations ) ){
- update_option( 'sa_wc_gateways_by_location', $gateway_by_locations );
- }
- } else {
- update_option( 'sa_wc_gateways_by_location', '' );
- }
- if ( array_key_exists('sa_wc_gateways_by_location_search_in', $_POST) ) {
- update_option( 'sa_wc_gateways_by_location_search_in', $_POST['sa_wc_gateways_by_location_search_in'] );
- } else {
- update_option( 'sa_wc_gateways_by_location_search_in', '' );
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement