Last active
May 31, 2019 06:55
-
-
Save wpmudev-sls/389fe8aef8b394790fc0bd396db8375b to your computer and use it in GitHub Desktop.
[MarketPress] - Remove Country Field
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: [MarketPress] - Remove Country Field | |
* Plugin URI: https://premium.wpmudev.org/ | |
* Author: WPMUDEV | |
* Author URI: https://premium.wpmudev.org | |
* License: GPLv2 or later | |
*/ | |
if ( ! defined( 'ABSPATH' ) ) { | |
exit; | |
} | |
add_action( 'plugins_loaded', 'wpmudev_mp_load_remove_country_field_func', 100 ); | |
function wpmudev_mp_load_remove_country_field_func() { | |
if ( defined( 'MP_VERSION' ) && class_exists( 'Marketpress' ) ) { | |
add_filter( 'mp_checkout/required_fields', 'wpmudev_mp_remove_country_from_checkout_required_fields' ); | |
add_filter( 'mp_checkout/address_fields_array', 'wpmudev_mp_remove_country_field', 10, 2 ); | |
add_action( 'admin_init', 'wpmudev_mp_remove_state_and_country_fields' ); | |
function wpmudev_mp_remove_state_and_country_fields(){ | |
if( apply_filters( 'wpmudev_mp_remove_country_and_state_fields_on_admin_order_page', false ) ){ | |
add_filter( 'mp_order/get_address', 'wpmudev_mp_admin_hide_country_field', 10, 3 ); | |
} | |
} | |
/** | |
* Remove country field from list required fields | |
* @param array $required_fields list required fields | |
* @return array custom required fields | |
*/ | |
function wpmudev_mp_remove_country_from_checkout_required_fields($required_fields){ | |
if( $found = array_search('country', $required_fields) ){ | |
unset( $required_fields[ $found ] ); | |
} | |
return $required_fields; | |
} | |
// Remove country field from checkout page | |
function wpmudev_mp_remove_country_field( $address_fields, $type ){ | |
foreach( $address_fields as $k => $field ){ | |
if( isset( $field['name'] ) && $field['name'] === $type .'[country]' ){ | |
unset( $address_fields[ $k ] ); | |
} | |
} | |
return $address_fields; | |
} | |
/** | |
* Add purchase order and not fields in admin | |
* @param strig $html default address html | |
* @param string $type address type | |
* @param object $order MP_Order | |
* @return string custom html | |
*/ | |
function wpmudev_mp_admin_hide_country_field( $html, $type, $order ){ | |
$html .= '<style> | |
.mp_customer_address table tr:nth-child(9), .mp_customer_address table tr:nth-child(7){ | |
display:none; | |
} | |
.wpmudev-hide-country-fields .mp_customer_address table tr:nth-child(9), .wpmudev-hide-country-fields .mp_customer_address table tr:nth-child(7){ | |
display: table-row; | |
} | |
</style> | |
<script> | |
(function($){ | |
$(function(){ | |
var _mpel_order_customer = $("#mp-order-customer-info-metabox"); | |
if( _mpel_order_customer.length ){ | |
var _mpel_country_fields = _mpel_order_customer.find("select[name$=\'[country]\']"); | |
if( _mpel_country_fields.length ){ | |
var _state_fields = _mpel_order_customer.find("select[name$=\'[state]\']");; | |
if( _state_fields.length ){ | |
_state_fields.closest("tr").remove(); | |
} | |
_mpel_country_fields.closest("tr").remove(); | |
_mpel_order_customer.addClass("wpmudev-hide-country-fields"); | |
} | |
} | |
}) | |
}(window.jQuery)); | |
</script> | |
'; | |
return $html; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment