Created bulk edit actions for variable sale schedule

This commit is contained in:
Claudio Sanches 2015-07-07 16:39:59 -03:00
parent e69d172f6c
commit 8b681fddf7
5 changed files with 55 additions and 40 deletions

View File

@ -59,6 +59,15 @@ jQuery( function( $ ) {
*/
variations_loaded: function() {
$( 'input.variable_is_downloadable, input.variable_is_virtual, input.variable_manage_stock', $( this ) ).change();
$( '.woocommerce_variation', $( this ) ).each( function( index, el ) {
var $el = $( el ),
date_from = $( '.sale_price_dates_from', $el ).val(),
date_to = $( '.sale_price_dates_to', $el ).val();
if ( '' !== date_from || '' !== date_to ) {
$( 'a.sale_schedule', $el ).click();
}
});
$( '.woocommerce_variations .variation-needs-update', $( this ) ).removeClass( 'variation-needs-update' );
$( 'button.save-variation-changes', $( this ) ).attr( 'disabled', 'disabled' );
}
@ -522,23 +531,16 @@ jQuery( function( $ ) {
}
break;
case 'variable_sale_schedule' :
// var start = window.prompt( woocommerce_admin_meta_boxes_variations.i18n_scheduled_sale_start );
// var end = window.prompt( woocommerce_admin_meta_boxes_variations.i18n_scheduled_sale_end );
// var set = false;
data.date_from = window.prompt( woocommerce_admin_meta_boxes_variations.i18n_scheduled_sale_start );
data.date_to = window.prompt( woocommerce_admin_meta_boxes_variations.i18n_scheduled_sale_end );
// if ( start != null && start !== '' ) {
// $('.woocommerce_variable_attributes .sale_schedule').click();
// $( ':input[name^="variable_sale_price_dates_from"]').val( start ).change();
// set = true;
// }
// if ( end != null && end !== '' ) {
// $('.woocommerce_variable_attributes .sale_schedule').click();
// $( ':input[name^="variable_sale_price_dates_to"]').val( end ).change();
// set = true;
// }
// if ( ! set ) {
// $('.woocommerce_variable_attributes .cancel_sale_schedule').click();
// }
if ( null === data.date_from ) {
data.date_from = false;
}
if ( null === data.date_to ) {
data.date_to = false;
}
break;
default :
$( 'select#field_to_edit' ).trigger( bulk_edit );

File diff suppressed because one or more lines are too long

View File

@ -103,7 +103,7 @@ extract( $variation_data );
</p>
<p class="form-row form-row-last">
<label><?php _e( 'Sale end date:', 'woocommerce' ); ?></label>
<input type="text" name="variable_sale_price_dates_to[<?php echo $loop; ?>]" value="<?php echo ! empty( $_sale_price_dates_to ) ? date_i18n( 'Y-m-d', $_sale_price_dates_to ) : ''; ?>" placeholder="<?php echo _x('To&hellip;', 'placeholder', 'woocommerce') ?> YYYY-MM-DD" maxlength="10" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" />
<input type="text" class="sale_price_dates_to" name="variable_sale_price_dates_to[<?php echo $loop; ?>]" value="<?php echo ! empty( $_sale_price_dates_to ) ? date_i18n( 'Y-m-d', $_sale_price_dates_to ) : ''; ?>" placeholder="<?php echo _x('To&hellip;', 'placeholder', 'woocommerce') ?> YYYY-MM-DD" maxlength="10" pattern="[0-9]{4}-(0[1-9]|1[012])-(0[1-9]|1[0-9]|2[0-9]|3[01])" />
</p>
</div>
</div>

View File

@ -2800,7 +2800,7 @@ class WC_AJAX {
$data = ! empty( $_POST['data'] ) ? array_map( 'wc_clean', $_POST['data'] ) : array();
$variations = array();
if ( apply_filters( 'woocommerce_bulk_edit_variations_need_children', in_array( $bulk_action, array( 'toggle_enabled', 'toggle_downloadable', 'toggle_virtual', 'toggle_manage_stock', 'variable_stock', 'variable_regular_price', 'variable_sale_price', 'variable_download_limit', 'variable_download_expiry', 'delete_all' ) ) ) ) {
if ( apply_filters( 'woocommerce_bulk_edit_variations_need_children', ! in_array( $bulk_action, array( 'variable_weight', 'variable_length', 'variable_width', 'variable_height' ) ) ) ) {
$variations = get_posts( array(
'post_parent' => $product_id,
'posts_per_page' => -1,
@ -2931,14 +2931,27 @@ class WC_AJAX {
break;
case 'variable_sale_schedule' :
if ( ! isset( $data['date_from'] ) && ! isset( $data['date_to'] ) ) {
break;
}
foreach ( $variations as $variation_id ) {
$regular_price = get_post_meta( $variation_id, '_regular_price', true );
$sale_price = get_post_meta( $variation_id, '_sale_price', true );
$date_from = 'false' === $data['date_from'] ? date( 'Y-m-d', get_post_meta( $variation_id, '_sale_price_dates_from', true ) ) : $data['date_from'];
$date_to = 'false' === $data['date_to'] ? date( 'Y-m-d', get_post_meta( $variation_id, '_sale_price_dates_to', true ) ) : $data['date_to'];
_wc_save_product_price( $variation_id, $regular_price, $sale_price, $date_from, $date_to );
}
break;
default :
do_action( 'woocommerce_bulk_edit_variations', $bulk_action, $data, $product_id, $variations );
do_action( 'woocommerce_bulk_edit_variations_default', $bulk_action, $data, $product_id, $variations );
break;
}
do_action( 'woocommerce_bulk_edit_variations', $bulk_action, $data, $product_id, $variations );
// Sync and update transients
WC_Product_Variable::sync( $product_id );
wc_delete_product_transients( $product_id );

View File

@ -570,47 +570,47 @@ function wc_get_product_id_by_sku( $sku ) {
*
* This is a private function (internal use ONLY) used until a data manipulation api is built
*
* @since 2.4.0
* @todo look into Data manipulation API
* @since 2.4.0
* @todo look into Data manipulation API
*
* @param int $variation_id
* @param float $regular_price
* @param float $sale_price
* @param string $date_from
* @param string $date_to
* @param int $product_id
* @param float $regular_price
* @param float $sale_price
* @param string $date_from
* @param string $date_to
*/
function _wc_save_product_price( $variation_id, $regular_price, $sale_price = '', $date_from = '', $date_to = '' ) {
$variation_id = absint( $variation_id );
function _wc_save_product_price( $product_id, $regular_price, $sale_price = '', $date_from = '', $date_to = '' ) {
$product_id = absint( $product_id );
$regular_price = wc_format_decimal( $regular_price );
$sale_price = $sale_price === '' ? '' : wc_format_decimal( $sale_price );
$date_from = wc_clean( $date_from );
$date_to = wc_clean( $date_to );
update_post_meta( $variation_id, '_regular_price', $regular_price );
update_post_meta( $variation_id, '_sale_price', $sale_price );
update_post_meta( $product_id, '_regular_price', $regular_price );
update_post_meta( $product_id, '_sale_price', $sale_price );
// Save Dates
update_post_meta( $variation_id, '_sale_price_dates_from', $date_from ? strtotime( $date_from ) : '' );
update_post_meta( $variation_id, '_sale_price_dates_to', $date_to ? strtotime( $date_to ) : '' );
update_post_meta( $product_id, '_sale_price_dates_from', $date_from ? strtotime( $date_from ) : '' );
update_post_meta( $product_id, '_sale_price_dates_to', $date_to ? strtotime( $date_to ) : '' );
if ( $date_to && ! $date_from ) {
update_post_meta( $variation_id, '_sale_price_dates_from', strtotime( 'NOW', current_time( 'timestamp' ) ) );
update_post_meta( $product_id, '_sale_price_dates_from', strtotime( 'NOW', current_time( 'timestamp' ) ) );
}
// Update price if on sale
if ( '' !== $sale_price && '' === $date_to && '' === $date_from ) {
update_post_meta( $variation_id, '_price', $sale_price );
update_post_meta( $product_id, '_price', $sale_price );
} else {
update_post_meta( $variation_id, '_price', $regular_price );
update_post_meta( $product_id, '_price', $regular_price );
}
if ( '' !== $sale_price && $date_from && strtotime( $date_from ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
update_post_meta( $variation_id, '_price', $sale_price );
update_post_meta( $product_id, '_price', $sale_price );
}
if ( $date_to && strtotime( $date_to ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
update_post_meta( $variation_id, '_price', $regular_price );
update_post_meta( $variation_id, '_sale_price_dates_from', '' );
update_post_meta( $variation_id, '_sale_price_dates_to', '' );
update_post_meta( $product_id, '_price', $regular_price );
update_post_meta( $product_id, '_sale_price_dates_from', '' );
update_post_meta( $product_id, '_sale_price_dates_to', '' );
}
}