Make downloadable product dropdown ajaxy, as per #4211
This commit is contained in:
parent
b4c66db7e2
commit
6300e65582
|
@ -196,6 +196,26 @@ jQuery( function($){
|
||||||
return terms;
|
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
|
// ORDERS
|
||||||
jQuery('#woocommerce-order-actions input, #woocommerce-order-actions a').click(function(){
|
jQuery('#woocommerce-order-actions input, #woocommerce-order-actions a').click(function(){
|
||||||
window.onbeforeunload = '';
|
window.onbeforeunload = '';
|
||||||
|
|
|
@ -53,35 +53,7 @@ class WC_Meta_Box_Order_Downloads {
|
||||||
|
|
||||||
<div class="toolbar">
|
<div class="toolbar">
|
||||||
<p class="buttons">
|
<p class="buttons">
|
||||||
<select name="grant_access_id" class="grant_access_id chosen_select_nostd" data-placeholder="<?php _e( 'Choose a downloadable product…', 'woocommerce' ) ?>">
|
<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…', 'woocommerce' ) ?>" style="width: 400px"></select>
|
||||||
<?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>
|
|
||||||
|
|
||||||
<button type="button" class="button grant_access"><?php _e( 'Grant Access', 'woocommerce' ); ?></button>
|
<button type="button" class="button grant_access"><?php _e( 'Grant Access', 'woocommerce' ); ?></button>
|
||||||
</p>
|
</p>
|
||||||
<div class="clear"></div>
|
<div class="clear"></div>
|
||||||
|
@ -98,15 +70,15 @@ class WC_Meta_Box_Order_Downloads {
|
||||||
|
|
||||||
jQuery('.order_download_permissions').on('click', 'button.grant_access', function(){
|
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 } });
|
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 = {
|
var data = {
|
||||||
action: 'woocommerce_grant_access_to_download',
|
action: 'woocommerce_grant_access_to_download',
|
||||||
product_id: product,
|
product_ids: products,
|
||||||
loop: jQuery('.order_download_permissions .wc-metabox').size(),
|
loop: jQuery('.order_download_permissions .wc-metabox').size(),
|
||||||
order_id: '<?php echo $post->ID; ?>',
|
order_id: '<?php echo $post->ID; ?>',
|
||||||
security: '<?php echo wp_create_nonce("grant-access"); ?>'
|
security: '<?php echo wp_create_nonce("grant-access"); ?>'
|
||||||
|
|
|
@ -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>
|
<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>
|
<div class="handlediv" title="<?php _e( 'Click to toggle', 'woocommerce' ); ?>"></div>
|
||||||
<strong>
|
<strong>
|
||||||
<?php echo '#' . absint( $product->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 ) ); ?>
|
<?php echo '#' . absint( $product->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 ) ); ?>
|
||||||
</strong>
|
</strong>
|
||||||
</h3>
|
</h3>
|
||||||
<table cellpadding="0" cellspacing="0" class="wc-metabox-content">
|
<table cellpadding="0" cellspacing="0" class="wc-metabox-content">
|
||||||
|
|
|
@ -22,39 +22,40 @@ class WC_AJAX {
|
||||||
|
|
||||||
// woocommerce_EVENT => nopriv
|
// woocommerce_EVENT => nopriv
|
||||||
$ajax_events = array(
|
$ajax_events = array(
|
||||||
'get_refreshed_fragments' => true,
|
'get_refreshed_fragments' => true,
|
||||||
'apply_coupon' => true,
|
'apply_coupon' => true,
|
||||||
'update_shipping_method' => true,
|
'update_shipping_method' => true,
|
||||||
'update_order_review' => true,
|
'update_order_review' => true,
|
||||||
'add_to_cart' => true,
|
'add_to_cart' => true,
|
||||||
'checkout' => true,
|
'checkout' => true,
|
||||||
'feature_product' => false,
|
'feature_product' => false,
|
||||||
'mark_order_complete' => false,
|
'mark_order_complete' => false,
|
||||||
'mark_order_processing' => false,
|
'mark_order_processing' => false,
|
||||||
'add_new_attribute' => false,
|
'add_new_attribute' => false,
|
||||||
'remove_variation' => false,
|
'remove_variation' => false,
|
||||||
'remove_variations' => false,
|
'remove_variations' => false,
|
||||||
'save_attributes' => false,
|
'save_attributes' => false,
|
||||||
'add_variation' => false,
|
'add_variation' => false,
|
||||||
'link_all_variations' => false,
|
'link_all_variations' => false,
|
||||||
'revoke_access_to_download' => false,
|
'revoke_access_to_download' => false,
|
||||||
'grant_access_to_download' => false,
|
'grant_access_to_download' => false,
|
||||||
'get_customer_details' => false,
|
'get_customer_details' => false,
|
||||||
'add_order_item' => false,
|
'add_order_item' => false,
|
||||||
'add_order_fee' => false,
|
'add_order_fee' => false,
|
||||||
'remove_order_item' => false,
|
'remove_order_item' => false,
|
||||||
'reduce_order_item_stock' => false,
|
'reduce_order_item_stock' => false,
|
||||||
'increase_order_item_stock' => false,
|
'increase_order_item_stock' => false,
|
||||||
'add_order_item_meta' => false,
|
'add_order_item_meta' => false,
|
||||||
'remove_order_item_meta' => false,
|
'remove_order_item_meta' => false,
|
||||||
'calc_line_taxes' => false,
|
'calc_line_taxes' => false,
|
||||||
'add_order_note' => false,
|
'add_order_note' => false,
|
||||||
'delete_order_note' => false,
|
'delete_order_note' => false,
|
||||||
'json_search_products' => false,
|
'json_search_products' => false,
|
||||||
'json_search_products_and_variations' => false,
|
'json_search_products_and_variations' => false,
|
||||||
'json_search_customers' => false,
|
'json_search_downloadable_products_and_variations' => false,
|
||||||
'term_ordering' => false,
|
'json_search_customers' => false,
|
||||||
'product_ordering' => false
|
'term_ordering' => false,
|
||||||
|
'product_ordering' => false
|
||||||
);
|
);
|
||||||
|
|
||||||
foreach ( $ajax_events as $ajax_event => $nopriv ) {
|
foreach ( $ajax_events as $ajax_event => $nopriv ) {
|
||||||
|
@ -754,28 +755,40 @@ class WC_AJAX {
|
||||||
|
|
||||||
$wpdb->hide_errors();
|
$wpdb->hide_errors();
|
||||||
|
|
||||||
$order_id = intval( $_POST['order_id'] );
|
$order_id = intval( $_POST['order_id'] );
|
||||||
$product_id = intval( $_POST['product_id'] );
|
$product_ids = $_POST['product_ids'];
|
||||||
$loop = intval( $_POST['loop'] );
|
$loop = intval( $_POST['loop'] );
|
||||||
$file_count = 0;
|
$file_counter = 0;
|
||||||
$order = new WC_Order( $order_id );
|
$order = new WC_Order( $order_id );
|
||||||
$product = get_product( $product_id );
|
|
||||||
$files = $product->get_files();
|
|
||||||
|
|
||||||
if ( ! $order->billing_email )
|
if ( ! is_array( $product_ids ) ) {
|
||||||
die();
|
$product_ids = array( $product_ids );
|
||||||
|
}
|
||||||
|
|
||||||
if ( $files ) {
|
foreach ( $product_ids as $product_id ) {
|
||||||
foreach ( $files as $download_id => $file ) {
|
$product = get_product( $product_id );
|
||||||
if ( $inserted_id = wc_downloadable_file_permission( $download_id, $product_id, $order ) ) {
|
$files = $product->get_files();
|
||||||
|
|
||||||
// insert complete - get inserted data
|
if ( ! $order->billing_email )
|
||||||
$download = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions WHERE permission_id = %d", $inserted_id ) );
|
die();
|
||||||
|
|
||||||
$loop ++;
|
if ( $files ) {
|
||||||
$file_count ++;
|
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();
|
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
|
* When searching using the WP_User_Query, search names (user meta) too
|
||||||
* @param object $query
|
* @param object $query
|
||||||
|
|
Loading…
Reference in New Issue