Skip to content

Commit

Permalink
fix: Product edit page loading issue when long tags list issue fixed (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
alamgircsebd authored Aug 20, 2020
1 parent c58a4f2 commit 079b1eb
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 17 deletions.
35 changes: 35 additions & 0 deletions assets/src/js/product-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -935,4 +935,39 @@

});

$(document).ready(function () {
// Ajax search products tags
$("#product_tag_search").select2({
allowClear: false,
ajax: {
url: dokan.ajaxurl,
dataType: 'json',
delay: 250,
data: function (params) {
return {
q: params.term,
action: 'dokan_json_search_products_tags',
security: dokan.search_products_tags_nonce,
page: params.page || 1
};
},
processResults: function( data ) {
var options = [];
if ( data ) {
$.each( data, function( index, text ) {
options.push( { id: text[0], text: text[1] } );
});
}
return {
results: options,
pagination: {
more: true
}
};
},
cache: true
}
});
});

})(jQuery);
39 changes: 39 additions & 0 deletions includes/Ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use WC_Customer;
use WC_Data_Store;
use WC_Order;
use WP_Query;

/**
* Ajax handler for Dokan
Expand Down Expand Up @@ -48,6 +49,8 @@ public function __construct() {

add_action( 'wp_ajax_custom-header-crop', array( $this, 'crop_store_banner' ) );

add_action( 'wp_ajax_dokan_json_search_products_tags', array( $this, 'dokan_json_search_products_tags' ) );

add_action( 'wp_ajax_dokan_json_search_products_and_variations', array( $this, 'json_search_product' ), 10 );
add_action( 'wp_ajax_nopriv_dokan_json_search_products_and_variations', array( $this, 'json_search_product' ), 10 );
add_action( 'wp_ajax_dokan_json_search_vendor_customers', array( $this, 'dokan_json_search_vendor_customers' ) );
Expand Down Expand Up @@ -660,6 +663,42 @@ public function json_search_product() {
wp_send_json( apply_filters( 'dokan_json_search_found_products', $products ) );
}


/**
* Search product tags
*
* @since 3.0.5
*
* @return array
*/
public function dokan_json_search_products_tags() {
check_ajax_referer( 'search-products-tags', 'security' );

$return = array();
$name = ( $_GET['q'] ) ? sanitize_text_field( wp_unslash( $_GET['q'] ) ) : '';
$page = ( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : 1;
$offset = ( $page-1 ) * 10;

$drop_down_tags = array(
'name__like' => $name,
'hide_empty' => 0,
'orderby' => 'name',
'order' => 'ASC',
'number' => 10,
'offset' => $offset
);

$product_tags = get_terms( 'product_tag', $drop_down_tags );

if ( $product_tags ) :
foreach ( $product_tags as $pro_term ) {
$return[] = array( $pro_term->term_id, $pro_term->name );
}
endif;
echo json_encode( $return );
die;
}

/**
* Search customer
*
Expand Down
1 change: 1 addition & 0 deletions includes/Assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,7 @@ public function conditional_localized_args( $default_args ) {
'product_title_required' => __( 'Product title is required', 'dokan-lite' ),
'product_category_required' => __( 'Product category is required', 'dokan-lite' ),
'search_products_nonce' => wp_create_nonce( 'search-products' ),
'search_products_tags_nonce' => wp_create_nonce( 'search-products-tags' ),
'search_customer_nonce' => wp_create_nonce( 'search-customer' ),
'i18n_matches_1' => __( 'One result is available, press enter to select it.', 'dokan-lite' ),
'i18n_matches_n' => __( '%qty% results are available, use up and down arrow keys to navigate.', 'dokan-lite' ),
Expand Down
29 changes: 12 additions & 17 deletions templates/products/new-product-single.php
Original file line number Diff line number Diff line change
Expand Up @@ -314,25 +314,20 @@ class="vendor-earning simple-product"
<?php
require_once DOKAN_LIB_DIR.'/class.taxonomy-walker.php';
$term = wp_get_post_terms( $post_id, 'product_tag', array( 'fields' => 'ids') );
$selected = ( $term ) ? $term : array();
$drop_down_tags = wp_dropdown_categories( array(
'show_option_none' => __( '', 'dokan-lite' ),
'hierarchical' => 1,
'hide_empty' => 0,
'name' => 'product_tag[]',
'id' => 'product_tag',
'taxonomy' => 'product_tag',
'title_li' => '',
'class' => 'product_tags dokan-form-control dokan-select2',
'exclude' => '',
'selected' => $selected,
'echo' => 0,
'walker' => new TaxonomyDropdown( $post_id )
) );

echo str_replace( '<select', '<select data-placeholder="' . esc_html__( 'Select product tags', 'dokan-lite' ) . '" multiple="multiple" ', $drop_down_tags ); // phpcs:ignore WordPress.XSS.EscapeOutput.OutputNotEscaped

$drop_down_tags = array(
'hide_empty' => 0,
);

$my_tax_terms = get_terms( 'product_tag', $drop_down_tags );
?>
<select multiple="multiple" name="product_tag[]" id="product_tag_search" class="product_tags dokan-form-control dokan-select2">';
<?php foreach ( $my_tax_terms as $tax_term ) : ?>
<?php if ( ! empty( $term ) && in_array( $tax_term->term_id, $term ) ) : ?>
<option value="<?php echo $tax_term->term_id; ?>" selected="selected" ><?php echo $tax_term->name; ?></option>
<?php endif ?>
<?php endforeach ?>
</select>
</div>

<?php do_action( 'dokan_product_edit_after_product_tags', $post, $post_id ); ?>
Expand Down

0 comments on commit 079b1eb

Please sign in to comment.