diff --git a/assets/js/admin/meta-boxes.js b/assets/js/admin/meta-boxes.js index a71efa573dc..a1893118796 100644 --- a/assets/js/admin/meta-boxes.js +++ b/assets/js/admin/meta-boxes.js @@ -196,6 +196,26 @@ jQuery( function($){ return terms; }); + 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 (data) { + + var terms = {}; + + $.each(data, function (i, val) { + terms[i] = val; + }); + + return terms; + }); + // ORDERS jQuery('#woocommerce-order-actions input, #woocommerce-order-actions a').click(function(){ window.onbeforeunload = ''; diff --git a/includes/admin/post-types/meta-boxes/class-wc-meta-box-order-downloads.php b/includes/admin/post-types/meta-boxes/class-wc-meta-box-order-downloads.php index 58e4eb0594f..c72043250f3 100644 --- a/includes/admin/post-types/meta-boxes/class-wc-meta-box-order-downloads.php +++ b/includes/admin/post-types/meta-boxes/class-wc-meta-box-order-downloads.php @@ -53,35 +53,7 @@ class WC_Meta_Box_Order_Downloads {

- - +

@@ -98,15 +70,15 @@ class WC_Meta_Box_Order_Downloads { jQuery('.order_download_permissions').on('click', 'button.grant_access', function(){ - var product = jQuery('select.grant_access_id').val(); + var products = jQuery('select#grant_access_id').val(); - if (!product) return; + if (!products) return; jQuery('.order_download_permissions').block({ message: null, overlayCSS: { background: '#fff url(plugin_url(); ?>/assets/images/ajax-loader.gif) no-repeat center', opacity: 0.6 } }); var data = { action: 'woocommerce_grant_access_to_download', - product_id: product, + product_ids: products, loop: jQuery('.order_download_permissions .wc-metabox').size(), order_id: 'ID; ?>', security: '' diff --git a/includes/admin/post-types/meta-boxes/views/html-order-download-permission.php b/includes/admin/post-types/meta-boxes/views/html-order-download-permission.php index 935c2b0a808..81e86b92195 100644 --- a/includes/admin/post-types/meta-boxes/views/html-order-download-permission.php +++ b/includes/admin/post-types/meta-boxes/views/html-order-download-permission.php @@ -6,7 +6,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
- id ) . ' — ' . apply_filters( 'woocommerce_admin_download_permissions_title', $product->get_title(), $download->product_id, $download->order_id, $download->order_key, $download->download_id ) . ' — ' . sprintf( __( 'File %d: %s', 'woocommerce' ), $file_count, wc_get_filename_from_url( $product->get_file_download_path( $download->download_id ) ) ) . ' — ' . sprintf( _n('Downloaded %s time', 'Downloaded %s times', absint( $download->download_count ), 'woocommerce'), absint( $download->download_count ) ); ?> + id ) . ' — ' . apply_filters( 'woocommerce_admin_download_permissions_title', $product->get_title(), $download->product_id, $download->order_id, $download->order_key, $download->download_id ) . ' — ' . sprintf( __( '%s: %s', 'woocommerce' ), $file_count, wc_get_filename_from_url( $product->get_file_download_path( $download->download_id ) ) ) . ' — ' . sprintf( _n('Downloaded %s time', 'Downloaded %s times', absint( $download->download_count ), 'woocommerce'), absint( $download->download_count ) ); ?> diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index a70cde0d23e..ce2a3d37e1b 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -22,39 +22,40 @@ class WC_AJAX { // woocommerce_EVENT => nopriv $ajax_events = array( - 'get_refreshed_fragments' => true, - 'apply_coupon' => true, - 'update_shipping_method' => true, - 'update_order_review' => true, - 'add_to_cart' => true, - 'checkout' => true, - 'feature_product' => false, - 'mark_order_complete' => false, - 'mark_order_processing' => false, - 'add_new_attribute' => false, - 'remove_variation' => false, - 'remove_variations' => false, - 'save_attributes' => false, - 'add_variation' => false, - 'link_all_variations' => false, - 'revoke_access_to_download' => false, - 'grant_access_to_download' => false, - 'get_customer_details' => false, - 'add_order_item' => false, - 'add_order_fee' => false, - 'remove_order_item' => false, - 'reduce_order_item_stock' => false, - 'increase_order_item_stock' => false, - 'add_order_item_meta' => false, - 'remove_order_item_meta' => false, - 'calc_line_taxes' => false, - 'add_order_note' => false, - 'delete_order_note' => false, - 'json_search_products' => false, - 'json_search_products_and_variations' => false, - 'json_search_customers' => false, - 'term_ordering' => false, - 'product_ordering' => false + 'get_refreshed_fragments' => true, + 'apply_coupon' => true, + 'update_shipping_method' => true, + 'update_order_review' => true, + 'add_to_cart' => true, + 'checkout' => true, + 'feature_product' => false, + 'mark_order_complete' => false, + 'mark_order_processing' => false, + 'add_new_attribute' => false, + 'remove_variation' => false, + 'remove_variations' => false, + 'save_attributes' => false, + 'add_variation' => false, + 'link_all_variations' => false, + 'revoke_access_to_download' => false, + 'grant_access_to_download' => false, + 'get_customer_details' => false, + 'add_order_item' => false, + 'add_order_fee' => false, + 'remove_order_item' => false, + 'reduce_order_item_stock' => false, + 'increase_order_item_stock' => false, + 'add_order_item_meta' => false, + 'remove_order_item_meta' => false, + 'calc_line_taxes' => false, + 'add_order_note' => false, + 'delete_order_note' => false, + 'json_search_products' => false, + 'json_search_products_and_variations' => false, + 'json_search_downloadable_products_and_variations' => false, + 'json_search_customers' => false, + 'term_ordering' => false, + 'product_ordering' => false ); foreach ( $ajax_events as $ajax_event => $nopriv ) { @@ -754,28 +755,40 @@ class WC_AJAX { $wpdb->hide_errors(); - $order_id = intval( $_POST['order_id'] ); - $product_id = intval( $_POST['product_id'] ); - $loop = intval( $_POST['loop'] ); - $file_count = 0; - $order = new WC_Order( $order_id ); - $product = get_product( $product_id ); - $files = $product->get_files(); + $order_id = intval( $_POST['order_id'] ); + $product_ids = $_POST['product_ids']; + $loop = intval( $_POST['loop'] ); + $file_counter = 0; + $order = new WC_Order( $order_id ); - if ( ! $order->billing_email ) - die(); + if ( ! is_array( $product_ids ) ) { + $product_ids = array( $product_ids ); + } - if ( $files ) { - foreach ( $files as $download_id => $file ) { - if ( $inserted_id = wc_downloadable_file_permission( $download_id, $product_id, $order ) ) { + foreach ( $product_ids as $product_id ) { + $product = get_product( $product_id ); + $files = $product->get_files(); - // insert complete - get inserted data - $download = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE permission_id = %d", $inserted_id ) ); + if ( ! $order->billing_email ) + die(); - $loop ++; - $file_count ++; + if ( $files ) { + foreach ( $files as $download_id => $file ) { + if ( $inserted_id = wc_downloadable_file_permission( $download_id, $product_id, $order ) ) { - include( 'admin/post-types/meta-boxes/views/html-order-download-permission.php' ); + // insert complete - get inserted data + $download = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE permission_id = %d", $inserted_id ) ); + + $loop ++; + $file_counter ++; + + if ( isset( $file['name'] ) ) { + $file_count = $file['name']; + } else { + $file_count = sprintf( __( 'File %d', 'woocommerce' ), $file_counter ); + } + include( 'admin/post-types/meta-boxes/views/html-order-download-permission.php' ); + } } } } @@ -1489,6 +1502,41 @@ class WC_AJAX { die(); } + /** + * Search for downloadable product variations and return json + * + * @access public + * @return void + * @see WC_AJAX::json_search_products() + */ + public function json_search_downloadable_products_and_variations() { + $term = (string) wc_clean( urldecode( stripslashes( $_GET['term'] ) ) ); + + $args = array( + 'post_type' => array( 'product', 'product_variation' ), + 'posts_per_page' => -1, + 'post_status' => 'publish', + 'order' => 'ASC', + 'orderby' => 'parent title', + 'meta_query' => array( + array( + 'key' => '_downloadable', + 'value' => 'yes' + ) + ), + 's' => $term + ); + $posts = get_posts( $args ); + $found_products = array(); + if ( $posts ) foreach ( $posts as $post ) { + $product = get_product( $post->ID ); + $found_products[ $post->ID ] = $product->get_formatted_name(); + } + + echo json_encode( $found_products ); + die(); + } + /** * When searching using the WP_User_Query, search names (user meta) too * @param object $query