Product searches select2

This commit is contained in:
Mike Jolley 2015-01-12 11:56:44 +00:00
parent 4b7f97c1de
commit 1fa26f1f6e
10 changed files with 105 additions and 3681 deletions

View File

@ -701,4 +701,4 @@ html[dir="rtl"] .select2-container-multi .select2-search-choice-close {
.select2-search input {
background-position: 100% -21px !important;
}
}
}

View File

@ -48,25 +48,6 @@ jQuery( function ( $ ) {
});
$('ul.wc-tabs li:visible').eq(0).find('a').click();
// Ajax Chosen Product Selectors
jQuery("select.ajax_chosen_select_products").ajaxChosen({
method: 'GET',
url: woocommerce_admin_meta_boxes.ajax_url,
dataType: 'json',
afterTypeDelay: 100,
data: {
action: 'woocommerce_json_search_products',
security: woocommerce_admin_meta_boxes.search_products_nonce
}
}, function (data) {
var terms = {};
$.each(data, function (i, val) {
terms[i] = val;
});
return terms;
});
/**
* Load Chosen for select products and variations
@ -74,7 +55,7 @@ jQuery( function ( $ ) {
* @return {void}
*/
function loadSelectProductAndVariation() {
$( 'select.ajax_chosen_select_products_and_variations' ).ajaxChosen({
/*$( 'select.ajax_chosen_select_products_and_variations' ).ajaxChosen({
method: 'GET',
url: woocommerce_admin_meta_boxes.ajax_url,
dataType: 'json',
@ -92,11 +73,11 @@ jQuery( function ( $ ) {
});
return terms;
});
});*/
}
// Run on document load
loadSelectProductAndVariation();
//loadSelectProductAndVariation();
// Load chosen inside WC Backbone Modal
$( 'body' ).on( 'wc_backbone_modal_loaded', function ( e, target ) {
@ -105,7 +86,7 @@ jQuery( function ( $ ) {
}
});
jQuery("select.ajax_chosen_select_downloadable_products_and_variations").ajaxChosen({
/*jQuery("select.ajax_chosen_select_downloadable_products_and_variations").ajaxChosen({
method: 'GET',
url: woocommerce_admin_meta_boxes.ajax_url,
dataType: 'json',
@ -123,7 +104,7 @@ jQuery( function ( $ ) {
});
return terms;
});
});*/
$( ".date-picker" ).datepicker({
dateFormat: "yy-mm-dd",

View File

@ -1 +1 @@
jQuery(function(a){function b(){a("#tiptip_holder").removeAttr("style"),a("#tiptip_arrow").removeAttr("style"),a(".tips").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:200})}function c(){a("select.ajax_chosen_select_products_and_variations").ajaxChosen({method:"GET",url:woocommerce_admin_meta_boxes.ajax_url,dataType:"json",afterTypeDelay:100,data:{action:"woocommerce_json_search_products_and_variations",security:woocommerce_admin_meta_boxes.search_products_nonce}},function(b){var c={};return a.each(b,function(a,b){c[a]=b}),c})}b(),a("#titlediv #title").keyup(function(b){var c=b.keyCode||b.which;return"9"==c&&a("#woocommerce-coupon-description").size()>0?(b.stopPropagation(),a("#woocommerce-coupon-description").focus(),!1):void 0}),a(function(){jQuery(".wc-metabox > h3").click(function(){a(this).parent(".wc-metabox").toggleClass("closed").toggleClass("open")})}),a("ul.wc-tabs").show(),a("div.panel-wrap").each(function(){a(this).find("div.panel:not(:first)").hide()}),a("ul.wc-tabs a").click(function(){var b=a(this).closest("div.panel-wrap");return a("ul.wc-tabs li",b).removeClass("active"),a(this).parent().addClass("active"),a("div.panel",b).hide(),a(a(this).attr("href")).show(),!1}),a("ul.wc-tabs li:visible").eq(0).find("a").click(),jQuery("select.ajax_chosen_select_products").ajaxChosen({method:"GET",url:woocommerce_admin_meta_boxes.ajax_url,dataType:"json",afterTypeDelay:100,data:{action:"woocommerce_json_search_products",security:woocommerce_admin_meta_boxes.search_products_nonce}},function(b){var c={};return a.each(b,function(a,b){c[a]=b}),c}),c(),a("body").on("wc_backbone_modal_loaded",function(a,b){"#wc-modal-add-products"===b&&c()}),jQuery("select.ajax_chosen_select_downloadable_products_and_variations").ajaxChosen({method:"GET",url:woocommerce_admin_meta_boxes.ajax_url,dataType:"json",afterTypeDelay:100,data:{action:"woocommerce_json_search_downloadable_products_and_variations",security:woocommerce_admin_meta_boxes.search_products_nonce}},function(b){var c={};return a.each(b,function(a,b){c[a]=b}),c}),a(".date-picker").datepicker({dateFormat:"yy-mm-dd",numberOfMonths:1,showButtonPanel:!0,showOn:"button",buttonImage:woocommerce_admin_meta_boxes.calendar_image,buttonImageOnly:!0}),a(".date-picker-field").datepicker({dateFormat:"yy-mm-dd",numberOfMonths:1,showButtonPanel:!0}),jQuery(".wc-metaboxes-wrapper").on("click",".wc-metabox h3",function(b){a(b.target).filter(":input, option").length||jQuery(this).next(".wc-metabox-content").stop().slideToggle()}).on("click",".expand_all",function(){return jQuery(this).closest(".wc-metaboxes-wrapper").find(".wc-metabox > .wc-metabox-content").show(),!1}).on("click",".close_all",function(){return jQuery(this).closest(".wc-metaboxes-wrapper").find(".wc-metabox > .wc-metabox-content").hide(),!1}),jQuery(".wc-metabox.closed").each(function(){jQuery(this).find(".wc-metabox-content").hide()})});
jQuery(function(a){function b(){a("#tiptip_holder").removeAttr("style"),a("#tiptip_arrow").removeAttr("style"),a(".tips").tipTip({attribute:"data-tip",fadeIn:50,fadeOut:50,delay:200})}function c(){}b(),a("#titlediv #title").keyup(function(b){var c=b.keyCode||b.which;return"9"==c&&a("#woocommerce-coupon-description").size()>0?(b.stopPropagation(),a("#woocommerce-coupon-description").focus(),!1):void 0}),a(function(){jQuery(".wc-metabox > h3").click(function(){a(this).parent(".wc-metabox").toggleClass("closed").toggleClass("open")})}),a("ul.wc-tabs").show(),a("div.panel-wrap").each(function(){a(this).find("div.panel:not(:first)").hide()}),a("ul.wc-tabs a").click(function(){var b=a(this).closest("div.panel-wrap");return a("ul.wc-tabs li",b).removeClass("active"),a(this).parent().addClass("active"),a("div.panel",b).hide(),a(a(this).attr("href")).show(),!1}),a("ul.wc-tabs li:visible").eq(0).find("a").click(),a("body").on("wc_backbone_modal_loaded",function(a,b){"#wc-modal-add-products"===b&&c()}),a(".date-picker").datepicker({dateFormat:"yy-mm-dd",numberOfMonths:1,showButtonPanel:!0,showOn:"button",buttonImage:woocommerce_admin_meta_boxes.calendar_image,buttonImageOnly:!0}),a(".date-picker-field").datepicker({dateFormat:"yy-mm-dd",numberOfMonths:1,showButtonPanel:!0}),jQuery(".wc-metaboxes-wrapper").on("click",".wc-metabox h3",function(b){a(b.target).filter(":input, option").length||jQuery(this).next(".wc-metabox-content").stop().slideToggle()}).on("click",".expand_all",function(){return jQuery(this).closest(".wc-metaboxes-wrapper").find(".wc-metabox > .wc-metabox-content").show(),!1}).on("click",".close_all",function(){return jQuery(this).closest(".wc-metaboxes-wrapper").find(".wc-metabox > .wc-metabox-content").hide(),!1}),jQuery(".wc-metabox.closed").each(function(){jQuery(this).find(".wc-metabox-content").hide()})});

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,13 +0,0 @@
jQuery( function( $ ) {
$('body')
.on( 'wc-enhanced-select-init', function() {
jQuery(".wc-enhanced-select, select.chosen_select").select2({
minimumResultsForSearch: jQuery( this ).data( 'minimum_results_for_search' ) ? jQuery( this ).data( 'minimum_results_for_search' ) : '5',
allowClear: jQuery( this ).data( 'allow_clear' ) ? true : false,
placeholder: jQuery( this ).data( 'placeholder' )
})
} )
.trigger( 'wc-enhanced-select-init' );
});

View File

@ -0,0 +1,63 @@
jQuery( function( $ ) {
$('body')
.on( 'wc-enhanced-select-init', function() {
// Regular select boxes
$(".wc-enhanced-select, select.chosen_select").each(function() {
$( this ).select2({
minimumResultsForSearch: $( this ).data( 'minimum_results_for_search' ) ? $( this ).data( 'minimum_results_for_search' ) : '5',
allowClear: $( this ).data( 'allow_clear' ) ? true : false,
placeholder: $( this ).data( 'placeholder' )
});
});
// Ajax product search box
$(".wc-product-search").each(function() {
$( this ).select2({
multiple: true,
placeholder: $( this ).data( 'placeholder' ),
ajax: {
url: wc_enhanced_select_params.ajax_url,
dataType: 'json',
quietMillis: 250,
data: function( term, page ) {
return {
term: term,
action: $(this).data( 'action' ) || 'woocommerce_json_search_products_and_variations',
security: wc_enhanced_select_params.search_products_nonce
};
},
results: function( data, page ) {
var terms = [];
if ( data ) {
$.each( data, function( id, text ) {
terms.push( { id: id, text: text } );
});
}
return { results: terms };
},
cache: true
},
initSelection: function( element, callback ) {
var data = $.parseJSON( element.attr( 'data-selected' ) );
var selected = [];
$( element.val().split( "," ) ).each( function( i, val ) {
selected.push( { id: val, text: data[ val ] } );
});
return callback( selected );
},
formatSelection: function( data ) {
return '<div class="selected-option" data-id="' + data.id + '">' + data.text + '</div>';
},
escapeMarkup: function(m) { return m; }
});
});
} )
.trigger( 'wc-enhanced-select-init' );
});

View File

@ -95,15 +95,17 @@ class WC_Admin_Assets {
wp_register_script( 'chosen', WC()->plugin_url() . '/assets/js/chosen/chosen.jquery' . $suffix . '.js', array( 'jquery' ), WC_VERSION );
// Select2 is the replacement for chosen
wp_register_script( 'wc-enhanced-select', WC()->plugin_url() . '/assets/js/select2/wc-enhanced-select' . $suffix . '.js', array( 'jquery', 'select2' ), WC_VERSION );
wp_register_script( 'select2', WC()->plugin_url() . '/assets/js/select2/select2' . $suffix . '.js', array( 'jquery' ), '3.5.2' );
wp_register_script( 'select2', '//cdnjs.cloudflare.com/ajax/libs/select2/3.5.2/select2.min.js', array( 'jquery' ), '3.5.2' );
wp_register_script( 'wc-enhanced-select', WC()->plugin_url() . '/assets/js/wc-enhanced-select' . $suffix . '.js', array( 'jquery', 'select2' ), WC_VERSION );
wp_localize_script( 'wc-enhanced-select', 'wc_enhanced_select_params', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'search_products_nonce' => wp_create_nonce( 'search-products' )
) );
// Accounting
$params = array(
wp_localize_script( 'accounting', 'accounting_params', array(
'mon_decimal_point' => get_option( 'woocommerce_price_decimal_sep' )
);
wp_localize_script( 'accounting', 'accounting_params', $params );
) );
// WooCommerce admin pages
if ( in_array( $screen->id, wc_get_screen_ids() ) ) {

View File

@ -96,45 +96,34 @@ class WC_Meta_Box_Coupon_Data {
// Product ids
?>
<p class="form-field"><label for="product_ids"><?php _e( 'Products', 'woocommerce' ); ?></label>
<select id="product_ids" name="product_ids[]" class="ajax_chosen_select_products_and_variations" multiple="multiple" data-placeholder="<?php _e( 'Search for a product&hellip;', 'woocommerce' ); ?>">
<?php
$product_ids = get_post_meta( $post->ID, 'product_ids', true );
<p class="form-field"><label><?php _e( 'Products', 'woocommerce' ); ?></label>
<input type="hidden" class="wc-product-search" style="width: 50%;" name="product_ids" data-placeholder="<?php _e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-selected="<?php
$product_ids = array_filter( array_map( 'absint', explode( ',', get_post_meta( $post->ID, 'product_ids', true ) ) ) );
$json_ids = array();
if ( $product_ids ) {
$product_ids = array_map( 'absint', explode( ',', $product_ids ) );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
$json_ids[ $product_id ] = wp_kses_post( $product->get_formatted_name() );
}
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
echo '<option value="' . esc_attr( $product_id ) . '" selected="selected">' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select> <img class="help_tip" data-tip='<?php _e( 'Products which need to be in the cart to use this coupon or, for "Product Discounts", which products are discounted.', 'woocommerce' ); ?>' src="<?php echo WC()->plugin_url(); ?>/assets/images/help.png" height="16" width="16" /></p>
echo esc_attr( json_encode( $json_ids ) );
?>" value="<?php echo implode( ',', array_keys( $json_ids ) ); ?>" /> <img class="help_tip" data-tip='<?php _e( 'Products which need to be in the cart to use this coupon or, for "Product Discounts", which products are discounted.', 'woocommerce' ); ?>' src="<?php echo WC()->plugin_url(); ?>/assets/images/help.png" height="16" width="16" /></p>
<?php
// Exclude Product ids
?>
<p class="form-field"><label for="exclude_product_ids"><?php _e( 'Exclude products', 'woocommerce' ); ?></label>
<select id="exclude_product_ids" name="exclude_product_ids[]" class="ajax_chosen_select_products_and_variations" multiple="multiple" data-placeholder="<?php _e( 'Search for a product…', 'woocommerce' ); ?>">
<?php
$product_ids = get_post_meta( $post->ID, 'exclude_product_ids', true );
<p class="form-field"><label><?php _e( 'Exclude products', 'woocommerce' ); ?></label>
<input type="hidden" class="wc-product-search" style="width: 50%;" name="exclude_product_ids" data-placeholder="<?php _e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" data-selected="<?php
$product_ids = array_filter( array_map( 'absint', explode( ',', get_post_meta( $post->ID, 'exclude_product_ids', true ) ) ) );
$json_ids = array();
if ( $product_ids ) {
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
$json_ids[ $product_id ] = wp_kses_post( $product->get_formatted_name() );
}
$product_ids = array_map( 'absint', explode( ',', $product_ids ) );
foreach ( $product_ids as $product_id ) {
$product = wc_get_product( $product_id );
echo '<option value="' . esc_attr( $product_id ) . '" selected="selected">' . wp_kses_post( $product->get_formatted_name() ) . '</option>';
}
}
?>
</select> <img class="help_tip" data-tip='<?php _e( 'Products which must not be in the cart to use this coupon or, for "Product Discounts", which products are not discounted.', 'woocommerce' ); ?>' src="<?php echo WC()->plugin_url(); ?>/assets/images/help.png" height="16" width="16" /></p>
echo esc_attr( json_encode( $json_ids ) );
?>" value="<?php echo implode( ',', array_keys( $json_ids ) ); ?>" /> <img class="help_tip" data-tip='<?php _e( 'Products which must not be in the cart to use this coupon or, for "Product Discounts", which products are not discounted.', 'woocommerce' ); ?>' src="<?php echo WC()->plugin_url(); ?>/assets/images/help.png" height="16" width="16" /></p>
<?php
echo '</div><div class="options_group">';
@ -142,11 +131,10 @@ class WC_Meta_Box_Coupon_Data {
// Categories
?>
<p class="form-field"><label for="product_categories"><?php _e( 'Product categories', 'woocommerce' ); ?></label>
<select id="product_categories" name="product_categories[]" class="chosen_select" multiple="multiple" data-placeholder="<?php _e( 'Any category', 'woocommerce' ); ?>">
<select id="product_categories" name="product_categories[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php _e( 'Any category', 'woocommerce' ); ?>">
<?php
$category_ids = (array) get_post_meta( $post->ID, 'product_categories', true );
$categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' );
$categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' );
if ( $categories ) foreach ( $categories as $cat ) {
echo '<option value="' . esc_attr( $cat->term_id ) . '"' . selected( in_array( $cat->term_id, $category_ids ), true, false ) . '>' . esc_html( $cat->name ) . '</option>';
@ -158,11 +146,10 @@ class WC_Meta_Box_Coupon_Data {
// Exclude Categories
?>
<p class="form-field"><label for="exclude_product_categories"><?php _e( 'Exclude categories', 'woocommerce' ); ?></label>
<select id="exclude_product_categories" name="exclude_product_categories[]" class="chosen_select" multiple="multiple" data-placeholder="<?php _e( 'No categories', 'woocommerce' ); ?>">
<select id="exclude_product_categories" name="exclude_product_categories[]" style="width: 50%;" class="wc-enhanced-select" multiple="multiple" data-placeholder="<?php _e( 'No categories', 'woocommerce' ); ?>">
<?php
$category_ids = (array) get_post_meta( $post->ID, 'exclude_product_categories', true );
$categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' );
$categories = get_terms( 'product_cat', 'orderby=name&hide_empty=0' );
if ( $categories ) foreach ( $categories as $cat ) {
echo '<option value="' . esc_attr( $cat->term_id ) . '"' . selected( in_array( $cat->term_id, $category_ids ), true, false ) . '>' . esc_html( $cat->name ) . '</option>';
@ -253,18 +240,8 @@ class WC_Meta_Box_Coupon_Data {
$minimum_amount = wc_format_decimal( $_POST['minimum_amount'] );
$maximum_amount = wc_format_decimal( $_POST['maximum_amount'] );
$customer_email = array_filter( array_map( 'trim', explode( ',', wc_clean( $_POST['customer_email'] ) ) ) );
if ( isset( $_POST['product_ids'] ) ) {
$product_ids = implode( ',', array_filter( array_map( 'intval', (array) $_POST['product_ids'] ) ) );
} else {
$product_ids = '';
}
if ( isset( $_POST['exclude_product_ids'] ) ) {
$exclude_product_ids = implode( ',', array_filter( array_map( 'intval', (array) $_POST['exclude_product_ids'] ) ) );
} else {
$exclude_product_ids = '';
}
$product_ids = implode( ',', array_filter( array_map( 'intval', explode( ',', $_POST['product_ids'] ) ) ) );
$exclude_product_ids = implode( ',', array_filter( array_map( 'intval', explode( ',', $_POST['exclude_product_ids'] ) ) ) );
$product_categories = isset( $_POST['product_categories'] ) ? array_map( 'intval', $_POST['product_categories'] ) : array();
$exclude_product_categories = isset( $_POST['exclude_product_categories'] ) ? array_map( 'intval', $_POST['exclude_product_categories'] ) : array();

View File

@ -178,7 +178,7 @@ class WC_Report_Sales_By_Product extends WC_Admin_Report {
<div class="section">
<form method="GET">
<div>
<select id="product_ids" name="product_ids[]" class="ajax_chosen_select_products" multiple="multiple" data-placeholder="<?php _e( 'Search for a product&hellip;', 'woocommerce' ); ?>" style="width:203px;"></select>
<input type="hidden" class="wc-product-search" style="width:203px;" name="product_ids[]" data-placeholder="<?php _e( 'Search for a product&hellip;', 'woocommerce' ); ?>" data-action="woocommerce_json_search_products_and_variations" />
<input type="submit" class="submit button" value="<?php _e( 'Show', 'woocommerce' ); ?>" />
<input type="hidden" name="range" value="<?php if ( ! empty( $_GET['range'] ) ) echo esc_attr( $_GET['range'] ) ?>" />
<input type="hidden" name="start_date" value="<?php if ( ! empty( $_GET['start_date'] ) ) echo esc_attr( $_GET['start_date'] ) ?>" />
@ -187,28 +187,6 @@ class WC_Report_Sales_By_Product extends WC_Admin_Report {
<input type="hidden" name="tab" value="<?php if ( ! empty( $_GET['tab'] ) ) echo esc_attr( $_GET['tab'] ) ?>" />
<input type="hidden" name="report" value="<?php if ( ! empty( $_GET['report'] ) ) echo esc_attr( $_GET['report'] ) ?>" />
</div>
<script type="text/javascript">
jQuery(function(){
// Ajax Chosen Product Selectors
jQuery("select.ajax_chosen_select_products").ajaxChosen({
method: 'GET',
url: '<?php echo admin_url('admin-ajax.php'); ?>',
dataType: 'json',
afterTypeDelay: 100,
data: {
action: 'woocommerce_json_search_products_and_variations',
security: '<?php echo wp_create_nonce("search-products"); ?>'
}
}, function (data) {
var terms = {};
jQuery.each(data, function (i, val) {
terms[i] = val;
});
return terms;
});
});
</script>
</form>
</div>
<h4 class="section_title"><span><?php _e( 'Top Sellers', 'woocommerce' ); ?></span></h4>