Sort gateways and set a default

This commit is contained in:
Mike Jolley 2011-12-06 16:45:08 +00:00
parent 49d24b09aa
commit 5f5eb20335
7 changed files with 194 additions and 60 deletions

View File

@ -68,6 +68,7 @@ function woocommerce_admin_scripts() {
wp_enqueue_script( 'woocommerce_admin' );
wp_enqueue_script('farbtastic');
wp_enqueue_script('chosen');
wp_enqueue_script('jquery-ui-sortable');
endif;

View File

@ -1071,7 +1071,7 @@ function woocommerce_settings() {
break;
case "payment_gateways" :
$links = array( '<a href="#gateway-order">'.__('Gateways', 'woothemes').'</a>' );
$links = array( '<a href="#gateway-order">'.__('Payment Gateways', 'woothemes').'</a>' );
foreach ($woocommerce->payment_gateways->payment_gateways() as $gateway) :
$title = ( isset( $gateway->method_title ) && $gateway->method_title) ? ucwords($gateway->method_title) : ucwords($gateway->id);
@ -1079,7 +1079,55 @@ function woocommerce_settings() {
endforeach;
echo '<div class="subsubsub_section"><ul class="subsubsub"><li>' . implode(' | </li><li>', $links) . '</li></ul><br class="clear" />';
// Gateway ordering
echo '<div class="section" id="gateway-order">';
?>
<h3><?php _e('Payment Gateways', 'woothemes'); ?></h3>
<p><?php _e('Your activated payment gateways are listed below. Drag and drop rows to re-order them for display on the checkout.', 'woothemes'); ?></p>
<table class="wc_gateways widefat" cellspacing="0">
<thead>
<tr>
<th width="1%"><?php _e('Default', 'woothemes'); ?></th>
<th><?php _e('Gateway', 'woothemes'); ?></th>
<th><?php _e('Status', 'woothemes'); ?></th>
</tr>
</thead>
<tbody>
<?php
foreach ( $woocommerce->payment_gateways->payment_gateways() as $gateway ) :
$default_gateway = get_option('woocommerce_default_gateway');
echo '<tr>
<td width="1%" class="radio">
<input type="radio" name="default_gateway" value="'.$gateway->id.'" '.checked($default_gateway, $gateway->id, false).' />
<input type="hidden" name="gateway_order[]" value="'.$gateway->id.'" />
</td>
<td>
<p><strong>'.$gateway->title.'</strong><br/>
<small>'.__('Gateway ID', 'woothemes').': '.$gateway->id.'</small></p>
</td>
<td>';
if ($gateway->enabled == 'yes')
echo '<img src="'.$woocommerce->plugin_url().'/assets/images/success.gif" alt="yes" />';
else
echo '<img src="'.$woocommerce->plugin_url().'/assets/images/success-off.gif" alt="no" />';
echo '</td>
</tr>';
endforeach;
?>
</tbody>
</table>
<?php
echo '</div>';
// Specific gateway options
foreach ( $woocommerce->payment_gateways->payment_gateways() as $gateway ) :
echo '<div class="section" id="gateway-'.$gateway->id.'">';
$gateway->admin_options();
@ -1101,71 +1149,96 @@ function woocommerce_settings() {
</form>
<script type="text/javascript">
// Subsubsub tabs
jQuery('ul.subsubsub li a:eq(0)').addClass('current');
jQuery('.subsubsub_section .section:gt(0)').hide();
jQuery(window).load(function(){
jQuery('ul.subsubsub li a').click(function(){
jQuery('a', jQuery(this).closest('ul.subsubsub')).removeClass('current');
jQuery(this).addClass('current');
jQuery('.section', jQuery(this).closest('.subsubsub_section')).hide();
jQuery( jQuery(this).attr('href') ).show();
jQuery('#last_tab').val( jQuery(this).attr('href') );
return false;
});
<?php if (isset($_GET['subtab']) && $_GET['subtab']) echo 'jQuery("ul.subsubsub li a[href=#'.$_GET['subtab'].']").click();'; ?>
// Countries
jQuery('select#woocommerce_allowed_countries').change(function(){
if (jQuery(this).val()=="specific") {
jQuery(this).parent().parent().next('tr').show();
} else {
jQuery(this).parent().parent().next('tr').hide();
}
}).change();
// Color picker
jQuery('.colorpick').each(function(){
jQuery('.colorpickdiv', jQuery(this).parent()).farbtastic(this);
jQuery(this).click(function() {
if ( jQuery(this).val() == "" ) jQuery(this).val('#');
jQuery('.colorpickdiv', jQuery(this).parent() ).show();
});
});
jQuery(document).mousedown(function(){
jQuery('.colorpickdiv').hide();
});
// Edit prompt
jQuery(function(){
var changed = false;
// Subsubsub tabs
jQuery('ul.subsubsub li a:eq(0)').addClass('current');
jQuery('.subsubsub_section .section:gt(0)').hide();
jQuery('input, textarea, select, checkbox').change(function(){
changed = true;
jQuery('ul.subsubsub li a').click(function(){
jQuery('a', jQuery(this).closest('ul.subsubsub')).removeClass('current');
jQuery(this).addClass('current');
jQuery('.section', jQuery(this).closest('.subsubsub_section')).hide();
jQuery( jQuery(this).attr('href') ).show();
jQuery('#last_tab').val( jQuery(this).attr('href') );
return false;
});
jQuery('.woo-nav-tab-wrapper a').click(function(){
if (changed) {
window.onbeforeunload = function() {
return '<?php echo __( 'The changes you made will be lost if you navigate away from this page.', 'woothemes' ); ?>';
}
<?php if (isset($_GET['subtab']) && $_GET['subtab']) echo 'jQuery("ul.subsubsub li a[href=#'.$_GET['subtab'].']").click();'; ?>
// Countries
jQuery('select#woocommerce_allowed_countries').change(function(){
if (jQuery(this).val()=="specific") {
jQuery(this).parent().parent().next('tr').show();
} else {
jQuery(this).parent().parent().next('tr').hide();
}
}).change();
// Color picker
jQuery('.colorpick').each(function(){
jQuery('.colorpickdiv', jQuery(this).parent()).farbtastic(this);
jQuery(this).click(function() {
if ( jQuery(this).val() == "" ) jQuery(this).val('#');
jQuery('.colorpickdiv', jQuery(this).parent() ).show();
});
});
jQuery(document).mousedown(function(){
jQuery('.colorpickdiv').hide();
});
// Edit prompt
jQuery(function(){
var changed = false;
jQuery('input, textarea, select, checkbox').change(function(){
changed = true;
});
jQuery('.woo-nav-tab-wrapper a').click(function(){
if (changed) {
window.onbeforeunload = function() {
return '<?php echo __( 'The changes you made will be lost if you navigate away from this page.', 'woothemes' ); ?>';
}
} else {
window.onbeforeunload = '';
}
});
jQuery('.submit input').click(function(){
window.onbeforeunload = '';
});
});
// Sorting
jQuery('table.wc_gateways tbody').sortable({
items:'tr',
cursor:'move',
axis:'y',
handle: 'td',
scrollSensitivity:40,
helper:function(e,ui){
ui.children().each(function(){
jQuery(this).width(jQuery(this).width());
});
ui.css('left', '0');
return ui;
},
start:function(event,ui){
ui.item.css('background-color','#f6f6f6');
},
stop:function(event,ui){
ui.item.removeAttr('style');
}
});
jQuery('.submit input').click(function(){
window.onbeforeunload = '';
// Chosen selects
jQuery("select.chosen_select").chosen();
jQuery("select.chosen_select_nostd").chosen({
allow_single_deselect: 'true'
});
});
// Chosen selects
jQuery("select.chosen_select").chosen();
jQuery("select.chosen_select_nostd").chosen({
allow_single_deselect: 'true'
});
</script>
</div>

View File

@ -111,6 +111,8 @@ ul.recent-orders.stock_list a,ul.stock_list.stock_list a{text-decoration:none;}
.woocommerce table.shippingrows td.country p.edit,.woocommerce table.taxrows td.country p.edit,.woocommerce table.shippingrows td.country .options,.woocommerce table.taxrows td.country .options{min-width:400px;}
.woocommerce table.shippingrows td.country .chzn-container,.woocommerce table.taxrows td.country .chzn-container{width:100% !important;margin:9px 0 5px;}.woocommerce table.shippingrows td.country .chzn-container .chzn-drop,.woocommerce table.taxrows td.country .chzn-container .chzn-drop{width:100% !important;-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;}
.woocommerce table.shippingrows td.country .chzn-container .search-field input,.woocommerce table.taxrows td.country .chzn-container .search-field input{width:150px !important;}
table.wc_gateways td{vertical-align:middle;cursor:move;}table.wc_gateways td p{margin:0 0 2px;}
table.wc_gateways .radio{text-align:center;}
.woocommerce table.widefat table.coupon_rows{border:0 !important;width:100%;}.woocommerce table.widefat table.coupon_rows thead th{padding:1px;border:0;font-weight:normal;font-size:11px;color:#999;}
.woocommerce table.widefat table.coupon_rows tbody td{padding:3px 3px 0 0;border:0;vertical-align:middle;}.woocommerce table.widefat table.coupon_rows tbody td input.text{width:95px;}
.woocommerce table.form-table textarea.input-text{height:100%;min-width:150px;}

View File

@ -647,6 +647,18 @@ ul.recent-orders, ul.stock_list {
}
}
}
table.wc_gateways {
td {
vertical-align: middle;
cursor: move;
p {
margin: 0 0 2px;
}
}
.radio {
text-align: center;
}
}
.woocommerce {

View File

@ -17,11 +17,29 @@ class woocommerce_payment_gateways {
$load_gateways = apply_filters('woocommerce_payment_gateways', array());
foreach ($load_gateways as $gateway) :
// Get order option
$ordering = (array) get_option('woocommerce_gateway_order');
$order_end = 999;
$this->payment_gateways[] = &new $gateway();
// Load gateways in order
foreach ($load_gateways as $gateway) :
$load_gateway = &new $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++;
endif;
endforeach;
ksort($this->payment_gateways);
add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
}
@ -53,4 +71,24 @@ class woocommerce_payment_gateways {
return $_available_gateways;
}
function process_admin_options() {
$default_gateway = (isset($_POST['default_gateway'])) ? esc_attr($_POST['default_gateway']) : '';
$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[$gateway_id] = $loop;
$loop++;
endforeach;
endif;
update_option( 'woocommerce_default_gateway', $default_gateway );
update_option( 'woocommerce_gateway_order', $order );
}
}

View File

@ -84,6 +84,7 @@ Yes you can! Join in on our GitHub repository :) https://github.com/woothemes/wo
= 1.3.1 =
* Many Minor bug fixes
* Ability to re-order payment gateways and choose a default
* Added a 'Shipping class' taxonomy for grouping products
* Enhanced Flat Rate shipping to give a flat rate to each shipping class
* Made jQuery UI optional and improved Javascript

View File

@ -118,7 +118,14 @@
$available_gateways = $woocommerce->payment_gateways->get_available_payment_gateways();
if ($available_gateways) :
// Chosen Method
if (sizeof($available_gateways)) current($available_gateways)->set_current();
if (sizeof($available_gateways)) :
$default_gateway = get_option('woocommerce_default_gateway');
if (isset($available_gateways[$default_gateway])) :
$available_gateways[$default_gateway]->set_current();
else :
current($available_gateways)->set_current();
endif;
endif;
foreach ($available_gateways as $gateway ) :
?>
<li>