Product admin/saving

This commit is contained in:
Mike Jolley 2014-06-19 16:47:04 +01:00
parent 7591e07d81
commit 19adbb9bba
2 changed files with 178 additions and 130 deletions

View File

@ -280,7 +280,7 @@ class WC_Meta_Box_Product_Data {
if ( get_option('woocommerce_manage_stock') == 'yes' ) {
// manage stock
woocommerce_wp_checkbox( array( 'id' => '_manage_stock', 'wrapper_class' => 'show_if_simple show_if_variable', 'label' => __('Manage stock?', 'woocommerce' ), 'description' => __( 'Enable stock management at product level (not needed if managing stock at variation level)', 'woocommerce' ) ) );
woocommerce_wp_checkbox( array( 'id' => '_manage_stock', 'wrapper_class' => 'show_if_simple show_if_variable', 'label' => __('Manage stock?', 'woocommerce' ), 'description' => __( 'Enable stock management at product level', 'woocommerce' ) ) );
do_action('woocommerce_product_options_stock');
@ -291,6 +291,13 @@ class WC_Meta_Box_Product_Data {
'step' => 'any'
) ) );
// Backorders?
woocommerce_wp_select( array( 'id' => '_backorders', 'label' => __( 'Allow Backorders?', 'woocommerce' ), 'options' => array(
'no' => __( 'Do not allow', 'woocommerce' ),
'notify' => __( 'Allow, but notify customer', 'woocommerce' ),
'yes' => __( 'Allow', 'woocommerce' )
), 'desc_tip' => true, 'description' => __( 'If managing stock, this controls whether or not backorders are allowed. If enabled, stock quantity can go below 0.', 'woocommerce' ) ) );
do_action('woocommerce_product_options_stock_fields');
echo '</div>';
@ -303,21 +310,6 @@ class WC_Meta_Box_Product_Data {
'outofstock' => __( 'Out of stock', 'woocommerce' )
), 'desc_tip' => true, 'description' => __( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ) ) );
if (get_option('woocommerce_manage_stock')=='yes') {
echo '<div class="show_if_simple show_if_variable">';
// Backorders?
woocommerce_wp_select( array( 'id' => '_backorders', 'label' => __( 'Allow Backorders?', 'woocommerce' ), 'options' => array(
'no' => __( 'Do not allow', 'woocommerce' ),
'notify' => __( 'Allow, but notify customer', 'woocommerce' ),
'yes' => __( 'Allow', 'woocommerce' )
), 'desc_tip' => true, 'description' => __( 'If managing stock, this controls whether or not backorders are allowed for this product and variations. If enabled, stock quantity can go below 0.', 'woocommerce' ) ) );
echo '</div>';
}
do_action('woocommerce_product_options_stock_status');
echo '</div>';
@ -719,7 +711,7 @@ class WC_Meta_Box_Product_Data {
* Show options for the variable product type
*/
public static function output_variations() {
global $post, $woocommerce;
global $post;
$attributes = maybe_unserialize( get_post_meta( $post->ID, '_product_attributes', true ) );
@ -736,9 +728,11 @@ class WC_Meta_Box_Product_Data {
$tax_classes = array_filter( array_map('trim', explode( "\n", get_option( 'woocommerce_tax_classes' ) ) ) );
$tax_class_options = array();
$tax_class_options[''] = __( 'Standard', 'woocommerce' );
if ( $tax_classes )
foreach ( $tax_classes as $class )
if ( $tax_classes ) {
foreach ( $tax_classes as $class ) {
$tax_class_options[ sanitize_title( $class ) ] = esc_attr( $class );
}
}
$backorder_options = array(
'no' => __( 'Do not allow', 'woocommerce' ),
@ -746,6 +740,11 @@ class WC_Meta_Box_Product_Data {
'yes' => __( 'Allow', 'woocommerce' )
);
$stock_status_options = array(
'instock' => __( 'In stock', 'woocommerce' ),
'outofstock' => __( 'Out of stock', 'woocommerce' )
);
?>
<div id="variable_product_options" class="panel wc-metaboxes-wrapper"><div id="variable_product_options_inner">
@ -778,6 +777,7 @@ class WC_Meta_Box_Product_Data {
<option value="variable_sale_price_decrease"><?php _e( 'Sale prices decrease by (fixed amount or %)', 'woocommerce' ); ?></option>
</optgroup>
<optgroup label="<?php esc_attr_e( 'Inventory', 'woocommerce' ); ?>">
<option value="toggle_manage_stock"><?php _e( 'Toggle &quot;Manage stock&quot;', 'woocommerce' ); ?></option>
<option value="variable_stock"><?php _e( 'Stock', 'woocommerce' ); ?></option>
</optgroup>
<optgroup label="<?php esc_attr_e( 'Shipping', 'woocommerce' ); ?>">
@ -808,7 +808,8 @@ class WC_Meta_Box_Product_Data {
'width' => wc_format_localized_decimal( get_post_meta( $post->ID, '_width', true ) ),
'height' => wc_format_localized_decimal( get_post_meta( $post->ID, '_height', true ) ),
'tax_class' => get_post_meta( $post->ID, '_tax_class', true ),
'backorder_options' => $backorder_options
'backorder_options' => $backorder_options,
'stock_status_options' => $stock_status_options
);
if ( ! $parent_data['weight'] )
@ -861,11 +862,13 @@ class WC_Meta_Box_Product_Data {
'_virtual',
'_thumbnail_id',
'_sale_price_dates_from',
'_sale_price_dates_to'
'_sale_price_dates_to',
'_manage_stock'
);
foreach ( $variation_fields as $field )
foreach ( $variation_fields as $field ) {
$$field = isset( $variation_data[ $field ][0] ) ? maybe_unserialize( $variation_data[ $field ][0] ) : '';
}
$_backorders = isset( $variation_data['_backorders'][0] ) ? $variation_data['_backorders'][0] : null;
$_tax_class = isset( $variation_data['_tax_class'][0] ) ? $variation_data['_tax_class'][0] : null;
@ -880,6 +883,11 @@ class WC_Meta_Box_Product_Data {
$_width = wc_format_localized_decimal( $_width );
$_height = wc_format_localized_decimal( $_height );
// Stock BW compat
if ( $_stock !== '' ) {
$_manage_stock = 'yes';
}
include( 'views/html-variation-admin.php' );
$loop++;
@ -1204,40 +1212,55 @@ class WC_Meta_Box_Product_Data {
}
// Stock Data
if ( get_option('woocommerce_manage_stock') == 'yes' ) {
if ( 'yes' === get_option( 'woocommerce_manage_stock' ) ) {
if ( $product_type == 'grouped' ) {
$manage_stock = 'no';
$backorders = 'no';
$stock = '';
$stock_status = wc_clean( $_POST['_stock_status'] );
update_post_meta( $post_id, '_manage_stock', 'no' );
update_post_meta( $post_id, '_backorders', 'no' );
update_post_meta( $post_id, '_stock', '' );
if ( 'external' === $product_type ) {
wc_update_product_stock_status( $post_id, wc_clean( $_POST['_stock_status'] ) );
$stock_status = 'instock';
} elseif ( $product_type == 'external' ) {
update_post_meta( $post_id, '_manage_stock', 'no' );
update_post_meta( $post_id, '_backorders', 'no' );
update_post_meta( $post_id, '_stock', '' );
wc_update_product_stock_status( $post_id, 'instock' );
} elseif ( ! empty( $_POST['_manage_stock'] ) ) {
update_post_meta( $post_id, '_manage_stock', 'yes' );
update_post_meta( $post_id, '_backorders', wc_clean( $_POST['_backorders'] ) );
wc_update_product_stock_status( $post_id, wc_clean( $_POST['_stock_status'] ) );
wc_update_product_stock( $post_id, intval( $_POST['_stock'] ) );
} elseif ( 'variable' === $product_type ) {
// Stock status determined by children unless managing stock at product level
if ( empty( $_POST['_manage_stock'] ) ) {
$stock_status = 'outofstock';
$children = get_posts( array(
'post_parent' => $post_id,
'posts_per_page'=> -1,
'post_type' => 'product_variation',
'fields' => 'ids',
'post_status' => 'publish'
) );
foreach ( $children as $child_id ) {
$child_stock_status = get_post_meta( $child_id, '_stock_status', true );
$child_stock_status = $child_stock_status ? $child_stock_status : 'instock';
if ( 'instock' === $child_stock_status ) {
$stock_status = 'instock';
}
}
} else {
$manage_stock = 'yes';
$backorders = wc_clean( $_POST['_backorders'] );
}
// Don't manage stock
update_post_meta( $post_id, '_manage_stock', 'no' );
update_post_meta( $post_id, '_backorders', wc_clean( $_POST['_backorders'] ) );
} elseif ( 'grouped' !== $product_type && ! empty( $_POST['_manage_stock'] ) ) {
$manage_stock = 'yes';
$backorders = wc_clean( $_POST['_backorders'] );
}
update_post_meta( $post_id, '_manage_stock', $manage_stock );
update_post_meta( $post_id, '_backorders', $backorders );
wc_update_product_stock_status( $post_id, $stock_status );
if ( ! empty( $_POST['_manage_stock'] ) ) {
wc_update_product_stock( $post_id, intval( $_POST['_stock'] ) );
} else {
update_post_meta( $post_id, '_stock', '' );
wc_update_product_stock_status( $post_id, wc_clean( $_POST['_stock_status'] ) );
}
} else {
@ -1248,10 +1271,11 @@ class WC_Meta_Box_Product_Data {
if ( isset( $_POST['upsell_ids'] ) ) {
$upsells = array();
$ids = $_POST['upsell_ids'];
foreach ( $ids as $id )
if ( $id && $id > 0 )
foreach ( $ids as $id ) {
if ( $id && $id > 0 ) {
$upsells[] = $id;
}
}
update_post_meta( $post_id, '_upsell_ids', $upsells );
} else {
delete_post_meta( $post_id, '_upsell_ids' );
@ -1336,7 +1360,7 @@ class WC_Meta_Box_Product_Data {
* Save meta box data
*/
public static function save_variations( $post_id, $post ) {
global $woocommerce, $wpdb;
global $wpdb;
$attributes = (array) maybe_unserialize( get_post_meta( $post_id, '_product_attributes', true ) );
@ -1359,27 +1383,30 @@ class WC_Meta_Box_Product_Data {
$variable_length = isset( $_POST['variable_length'] ) ? $_POST['variable_length'] : array();
$variable_width = isset( $_POST['variable_width'] ) ? $_POST['variable_width'] : array();
$variable_height = isset( $_POST['variable_height'] ) ? $_POST['variable_height'] : array();
$variable_stock = isset( $_POST['variable_stock'] ) ? $_POST['variable_stock'] : array();
$variable_backorders = isset( $_POST['variable_backorders'] ) ? $_POST['variable_backorders'] : array();
$variable_enabled = isset( $_POST['variable_enabled'] ) ? $_POST['variable_enabled'] : array();
$variable_is_virtual = isset( $_POST['variable_is_virtual'] ) ? $_POST['variable_is_virtual'] : array();
$variable_is_downloadable = isset( $_POST['variable_is_downloadable'] ) ? $_POST['variable_is_downloadable'] : array();
$variable_manage_stock = isset( $_POST['variable_manage_stock'] ) ? $_POST['variable_manage_stock'] : array();
$variable_stock = isset( $_POST['variable_stock'] ) ? $_POST['variable_stock'] : array();
$variable_backorders = isset( $_POST['variable_backorders'] ) ? $_POST['variable_backorders'] : array();
$variable_stock_status = isset( $_POST['variable_stock_status'] ) ? $_POST['variable_stock_status'] : array();
$max_loop = max( array_keys( $_POST['variable_post_id'] ) );
for ( $i = 0; $i <= $max_loop; $i ++ ) {
if ( ! isset( $variable_post_id[ $i ] ) )
if ( ! isset( $variable_post_id[ $i ] ) ) {
continue;
}
$variation_id = absint( $variable_post_id[ $i ] );
// Virtal/Downloadable
// Checkboxes
$is_virtual = isset( $variable_is_virtual[ $i ] ) ? 'yes' : 'no';
$is_downloadable = isset( $variable_is_downloadable[ $i ] ) ? 'yes' : 'no';
// Enabled or disabled
$post_status = isset( $variable_enabled[ $i ] ) ? 'publish' : 'private';
$manage_stock = isset( $variable_manage_stock[ $i ] ) ? 'yes' : 'no';
// Generate a useful post title
$variation_post_title = sprintf( __( 'Variation #%s of %s', 'woocommerce' ), absint( $variation_id ), esc_html( get_the_title( $post_id ) ) );
@ -1430,20 +1457,25 @@ class WC_Meta_Box_Product_Data {
update_post_meta( $variation_id, '_height', ( $variable_height[ $i ] === '' ) ? '' : wc_format_decimal( $variable_height[ $i ] ) );
// Stock handling
if ( isset( $variable_stock[$i] ) ) {
wc_update_product_stock( $variation_id, wc_clean( $variable_stock[ $i ] ) );
}
update_post_meta( $variation_id, '_manage_stock', $manage_stock );
wc_update_product_stock_status( $variation_id, $variable_stock_status[ $i ] );
if ( 'yes' === $manage_stock ) {
wc_update_product_stock( $variation_id, wc_clean( $variable_stock[ $i ] ) );
// Backorders
if ( isset( $variable_backorders[ $i ] ) && $variable_backorders[ $i ] !== 'parent' ) {
update_post_meta( $variation_id, '_backorders', wc_clean( $variable_backorders[ $i ] ) );
} else {
delete_post_meta( $variation_id, '_backorders' );
}
} else {
update_post_meta( $variation_id, '_stock', '' );
delete_post_meta( $variation_id, '_backorders' );
}
// Price handling
$regular_price = wc_format_decimal( $variable_regular_price[ $i ] );
$sale_price = ( $variable_sale_price[ $i ] === '' ? '' : wc_format_decimal( $variable_sale_price[ $i ] ) );
$sale_price = $variable_sale_price[ $i ] === '' ? '' : wc_format_decimal( $variable_sale_price[ $i ] );
$date_from = wc_clean( $variable_sale_price_dates_from[ $i ] );
$date_to = wc_clean( $variable_sale_price_dates_to[ $i ] );
@ -1451,27 +1483,23 @@ class WC_Meta_Box_Product_Data {
update_post_meta( $variation_id, '_sale_price', $sale_price );
// Save Dates
if ( $date_from )
update_post_meta( $variation_id, '_sale_price_dates_from', strtotime( $date_from ) );
else
update_post_meta( $variation_id, '_sale_price_dates_from', '' );
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 ) : '' );
if ( $date_to )
update_post_meta( $variation_id, '_sale_price_dates_to', strtotime( $date_to ) );
else
update_post_meta( $variation_id, '_sale_price_dates_to', '' );
if ( $date_to && ! $date_from )
if ( $date_to && ! $date_from ) {
update_post_meta( $variation_id, '_sale_price_dates_from', strtotime( 'NOW', current_time( 'timestamp' ) ) );
}
// Update price if on sale
if ( $sale_price != '' && $date_to == '' && $date_from == '' )
if ( '' !== $sale_price && '' === $date_to && '' === $date_from ) {
update_post_meta( $variation_id, '_price', $sale_price );
else
} else {
update_post_meta( $variation_id, '_price', $regular_price );
}
if ( $sale_price != '' && $date_from && strtotime( $date_from ) < strtotime( 'NOW', current_time( 'timestamp' ) ) )
if ( '' !== $sale_price && $date_from && strtotime( $date_from ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
update_post_meta( $variation_id, '_price', $sale_price );
}
if ( $date_to && strtotime( $date_to ) < strtotime( 'NOW', current_time( 'timestamp' ) ) ) {
update_post_meta( $variation_id, '_price', $regular_price );
@ -1479,10 +1507,11 @@ class WC_Meta_Box_Product_Data {
update_post_meta( $variation_id, '_sale_price_dates_to', '' );
}
if ( isset( $variable_tax_class[ $i ] ) && $variable_tax_class[ $i ] !== 'parent' )
if ( isset( $variable_tax_class[ $i ] ) && $variable_tax_class[ $i ] !== 'parent' ) {
update_post_meta( $variation_id, '_tax_class', wc_clean( $variable_tax_class[ $i ] ) );
else
} else {
delete_post_meta( $variation_id, '_tax_class' );
}
if ( $is_downloadable == 'yes' ) {
update_post_meta( $variation_id, '_download_limit', wc_clean( $variable_download_limit[ $i ] ) );
@ -1494,12 +1523,13 @@ class WC_Meta_Box_Product_Data {
$file_url_size = sizeof( $file_urls );
for ( $ii = 0; $ii < $file_url_size; $ii ++ ) {
if ( ! empty( $file_urls[ $ii ] ) )
if ( ! empty( $file_urls[ $ii ] ) ) {
$files[ md5( $file_urls[ $ii ] ) ] = array(
'name' => $file_names[ $ii ],
'file' => $file_urls[ $ii ]
);
}
}
// grant permission to any newly added files on any existing orders for this product prior to saving
do_action( 'woocommerce_process_product_file_download_paths', $post_id, $variation_id, $files );

View File

@ -58,24 +58,6 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
</td>
<td class="data" rowspan="2">
<table cellspacing="0" cellpadding="0" class="data_table">
<?php if ( get_option( 'woocommerce_manage_stock' ) == 'yes' ) : ?>
<tr>
<td>
<label><?php _e( 'Stock Qty:', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e( 'Enter a quantity to enable stock management at variation level, or leave blank to use the parent product\'s options.', 'woocommerce' ); ?>" href="#">[?]</a></label>
<input type="number" size="5" name="variable_stock[<?php echo $loop; ?>]" value="<?php if ( isset( $_stock ) ) echo esc_attr( $_stock ); ?>" step="any" />
</td>
<td>
<label><?php _e( 'Allow Backorders?', 'woocommerce' ); ?></label>
<select name="variable_backorders[<?php echo $loop; ?>]">
<option value="parent" <?php selected( is_null( $_backorders ), true ); ?>><?php _e( 'Same as parent', 'woocommerce' ); ?></option>
<?php
foreach ( $parent_data['backorder_options'] as $key => $value )
echo '<option value="' . esc_attr( $key ) . '" ' . selected( $key === $_backorders, true, false ) . '>' . esc_html( $value ) . '</option>';
?></select>
</td>
</tr>
<?php endif; ?>
<tr class="variable_pricing">
<td>
<label><?php echo __( 'Regular Price:', 'woocommerce' ) . ' (' . get_woocommerce_currency_symbol() . ')'; ?></label>
@ -98,6 +80,36 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
</td>
</tr>
<?php if ( get_option( 'woocommerce_manage_stock' ) == 'yes' ) : ?>
<tr class="show_if_variation_manage_stock">
<td>
<label><?php _e( 'Stock Qty:', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e( 'Enter a quantity to enable stock management at variation level, or leave blank to use the parent product\'s options.', 'woocommerce' ); ?>" href="#">[?]</a></label>
<input type="number" size="5" name="variable_stock[<?php echo $loop; ?>]" value="<?php if ( isset( $_stock ) ) echo esc_attr( $_stock ); ?>" step="any" />
</td>
<td>
<label><?php _e( 'Allow Backorders?', 'woocommerce' ); ?></label>
<select name="variable_backorders[<?php echo $loop; ?>]">
<option value="parent" <?php selected( is_null( $_backorders ), true ); ?>><?php _e( 'Same as parent', 'woocommerce' ); ?></option>
<?php
foreach ( $parent_data['backorder_options'] as $key => $value )
echo '<option value="' . esc_attr( $key ) . '" ' . selected( $key === $_backorders, true, false ) . '>' . esc_html( $value ) . '</option>';
?></select>
</td>
</tr>
<tr>
<td colspan="2">
<label><?php _e( 'Stock status', 'woocommerce' ); ?> <a class="tips" data-tip="<?php esc_attr_e( 'Controls whether or not the product is listed as "in stock" or "out of stock" on the frontend.', 'woocommerce' ); ?>" href="#">[?]</a></label>
<select name="variable_stock_status[<?php echo $loop; ?>]">
<?php
foreach ( $parent_data['stock_status_options'] as $key => $value ) {
echo '<option value="' . esc_attr( $key ) . '" ' . selected( $key === $_backorders, true, false ) . '>' . esc_html( $value ) . '</option>';
}
?>
</select>
</td>
</tr>
<?php endif; ?>
<?php if ( wc_product_weight_enabled() || wc_product_dimensions_enabled() ) : ?>
<tr>
<?php if ( wc_product_weight_enabled() ) : ?>
@ -221,6 +233,12 @@ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
<label><input type="checkbox" class="checkbox variable_is_virtual" name="variable_is_virtual[<?php echo $loop; ?>]" <?php checked( isset( $_virtual ) ? $_virtual : '', 'yes' ); ?> /> <?php _e( 'Virtual', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e( 'Enable this option if a product is not shipped or there is no shipping cost', 'woocommerce' ); ?>" href="#">[?]</a></label>
<?php if ( get_option( 'woocommerce_manage_stock' ) == 'yes' ) : ?>
<label><input type="checkbox" class="checkbox variable_manage_stock" name="variable_manage_stock[<?php echo $loop; ?>]" <?php checked( isset( $_manage_stock ) ? $_manage_stock : '', 'yes' ); ?> /> <?php _e( 'Manage stock?', 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e( 'Enable this option to enable stock management at variation level', 'woocommerce' ); ?>" href="#">[?]</a></label>
<?php endif; ?>
<?php do_action( 'woocommerce_variation_options', $loop, $variation_data, $variation ); ?>
</td>
</tr>