Make downloadable product dropdown ajaxy, as per #4211

This commit is contained in:
Gerhard 2013-11-26 15:54:34 +02:00
parent b4c66db7e2
commit 6300e65582
4 changed files with 123 additions and 83 deletions

View File

@ -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 = '';

View File

@ -53,35 +53,7 @@ class WC_Meta_Box_Order_Downloads {
<div class="toolbar">
<p class="buttons">
<select name="grant_access_id" class="grant_access_id chosen_select_nostd" data-placeholder="<?php _e( 'Choose a downloadable product&hellip;', 'woocommerce' ) ?>">
<?php
echo '<option value=""></option>';
$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'
)
)
);
$products = get_posts( $args );
if ( $products ) foreach ( $products as $product ) {
$product_object = get_product( $product->ID );
echo '<option value="' . esc_attr( $product->ID ) . '">' . esc_html( $product_object->get_formatted_name() ) . '</option>';
}
?>
</select>
<select name="grant_access_id" id="grant_access_id" class="ajax_chosen_select_downloadable_products_and_variations" multiple="multiple" data-placeholder="<?php _e( 'Search for a downloadable product&hellip;', 'woocommerce' ) ?>" style="width: 400px"></select>
<button type="button" class="button grant_access"><?php _e( 'Grant Access', 'woocommerce' ); ?></button>
</p>
<div class="clear"></div>
@ -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(<?php echo WC()->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: '<?php echo $post->ID; ?>',
security: '<?php echo wp_create_nonce("grant-access"); ?>'

View File

@ -6,7 +6,7 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
<button type="button" rel="<?php echo absint( $download->product_id ) . ',' . esc_attr( $download->download_id ); ?>" class="revoke_access button"><?php _e( 'Revoke Access', 'woocommerce' ); ?></button>
<div class="handlediv" title="<?php _e( 'Click to toggle', 'woocommerce' ); ?>"></div>
<strong>
<?php echo '#' . absint( $product->id ) . ' &mdash; ' . apply_filters( 'woocommerce_admin_download_permissions_title', $product->get_title(), $download->product_id, $download->order_id, $download->order_key, $download->download_id ) . ' &mdash; ' . sprintf( __( 'File %d: %s', 'woocommerce' ), $file_count, wc_get_filename_from_url( $product->get_file_download_path( $download->download_id ) ) ) . ' &mdash; ' . sprintf( _n('Downloaded %s time', 'Downloaded %s times', absint( $download->download_count ), 'woocommerce'), absint( $download->download_count ) ); ?>
<?php echo '#' . absint( $product->id ) . ' &mdash; ' . apply_filters( 'woocommerce_admin_download_permissions_title', $product->get_title(), $download->product_id, $download->order_id, $download->order_key, $download->download_id ) . ' &mdash; ' . sprintf( __( '%s: %s', 'woocommerce' ), $file_count, wc_get_filename_from_url( $product->get_file_download_path( $download->download_id ) ) ) . ' &mdash; ' . sprintf( _n('Downloaded %s time', 'Downloaded %s times', absint( $download->download_count ), 'woocommerce'), absint( $download->download_count ) ); ?>
</strong>
</h3>
<table cellpadding="0" cellspacing="0" class="wc-metabox-content">

View File

@ -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