Keep new variation in sync so actions can modify data Closes #8858

This commit is contained in:
Mike Jolley 2015-08-17 15:11:34 +01:00
parent 9330176865
commit a5a7c71aa7
1 changed files with 62 additions and 28 deletions

View File

@ -753,76 +753,110 @@ class WC_AJAX {
do_action( 'woocommerce_create_product_variation', $variation_id ); do_action( 'woocommerce_create_product_variation', $variation_id );
if ( $variation_id ) { if ( $variation_id ) {
$variation = get_post( $variation_id );
$variation_meta = get_post_meta( $variation_id );
$variation_data = array();
$shipping_classes = get_the_terms( $variation_id, 'product_shipping_class' );
$variation_fields = array(
'_sku' => '',
'_stock' => '',
'_regular_price' => '',
'_sale_price' => '',
'_weight' => '',
'_length' => '',
'_width' => '',
'_height' => '',
'_download_limit' => '',
'_download_expiry' => '',
'_downloadable_files' => '',
'_downloadable' => '',
'_virtual' => '',
'_thumbnail_id' => '',
'_sale_price_dates_from' => '',
'_sale_price_dates_to' => '',
'_manage_stock' => '',
'_stock_status' => '',
'_backorders' => null,
'_tax_class' => null,
'_variation_description' => ''
);
$variation_post_status = 'publish'; foreach ( $variation_fields as $field => $value ) {
$variation_data = get_post_meta( $variation_id ); $variation_data[ $field ] = isset( $variation_meta[ $field ][0] ) ? maybe_unserialize( $variation_meta[ $field ][0] ) : $value;
$variation_data['variation_post_id'] = $variation_id; }
// Get attributes // Add the variation attributes
$attributes = (array) maybe_unserialize( get_post_meta( $post_id, '_product_attributes', true ) ); $variation_data = array_merge( $variation_data, wc_get_product_variation_attributes( $variation_id ) );
// Formatting
$variation_data['_regular_price'] = wc_format_localized_price( $variation_data['_regular_price'] );
$variation_data['_sale_price'] = wc_format_localized_price( $variation_data['_sale_price'] );
$variation_data['_weight'] = wc_format_localized_decimal( $variation_data['_weight'] );
$variation_data['_length'] = wc_format_localized_decimal( $variation_data['_length'] );
$variation_data['_width'] = wc_format_localized_decimal( $variation_data['_width'] );
$variation_data['_height'] = wc_format_localized_decimal( $variation_data['_height'] );
$variation_data['_thumbnail_id'] = absint( $variation_data['_thumbnail_id'] );
$variation_data['image'] = $variation_data['_thumbnail_id'] ? wp_get_attachment_thumb_url( $variation_data['_thumbnail_id'] ) : '';
$variation_data['shipping_class'] = $shipping_classes && ! is_wp_error( $shipping_classes ) ? current( $shipping_classes )->term_id : '';
$variation_data['menu_order'] = $variation->menu_order;
// Get tax classes // Get tax classes
$tax_classes = WC_Tax::get_tax_classes(); $tax_classes = WC_Tax::get_tax_classes();
$tax_class_options = array(); $tax_class_options = array();
$tax_class_options[''] = __( 'Standard', 'woocommerce' ); $tax_class_options[''] = __( 'Standard', 'woocommerce' );
if ( $tax_classes ) { if ( ! empty( $tax_classes ) ) {
foreach ( $tax_classes as $class ) { foreach ( $tax_classes as $class ) {
$tax_class_options[ sanitize_title( $class ) ] = $class; $tax_class_options[ sanitize_title( $class ) ] = esc_attr( $class );
} }
} }
// Set backorder options
$backorder_options = array( $backorder_options = array(
'no' => __( 'Do not allow', 'woocommerce' ), 'no' => __( 'Do not allow', 'woocommerce' ),
'notify' => __( 'Allow, but notify customer', 'woocommerce' ), 'notify' => __( 'Allow, but notify customer', 'woocommerce' ),
'yes' => __( 'Allow', 'woocommerce' ) 'yes' => __( 'Allow', 'woocommerce' )
); );
// set stock status options
$stock_status_options = array( $stock_status_options = array(
'instock' => __( 'In stock', 'woocommerce' ), 'instock' => __( 'In stock', 'woocommerce' ),
'outofstock' => __( 'Out of stock', 'woocommerce' ) 'outofstock' => __( 'Out of stock', 'woocommerce' )
); );
// Get parent data // Get attributes
$attributes = (array) maybe_unserialize( get_post_meta( $post_id, '_product_attributes', true ) );
$parent_data = array( $parent_data = array(
'id' => $post_id, 'id' => $post_id,
'attributes' => $attributes, 'attributes' => $attributes,
'tax_class_options' => $tax_class_options, 'tax_class_options' => $tax_class_options,
'sku' => get_post_meta( $post_id, '_sku', true ), 'sku' => get_post_meta( $post_id, '_sku', true ),
'weight' => get_post_meta( $post_id, '_weight', true ), 'weight' => wc_format_localized_decimal( get_post_meta( $post_id, '_weight', true ) ),
'length' => get_post_meta( $post_id, '_length', true ), 'length' => wc_format_localized_decimal( get_post_meta( $post_id, '_length', true ) ),
'width' => get_post_meta( $post_id, '_width', true ), 'width' => wc_format_localized_decimal( get_post_meta( $post_id, '_width', true ) ),
'height' => get_post_meta( $post_id, '_height', true ), 'height' => wc_format_localized_decimal( get_post_meta( $post_id, '_height', true ) ),
'tax_class' => get_post_meta( $post_id, '_tax_class', 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 'stock_status_options' => $stock_status_options
); );
if ( ! $parent_data['weight'] ) { if ( ! $parent_data['weight'] ) {
$parent_data['weight'] = '0.00'; $parent_data['weight'] = wc_format_localized_decimal( 0 );
} }
if ( ! $parent_data['length'] ) { if ( ! $parent_data['length'] ) {
$parent_data['length'] = '0'; $parent_data['length'] = wc_format_localized_decimal( 0 );
} }
if ( ! $parent_data['width'] ) { if ( ! $parent_data['width'] ) {
$parent_data['width'] = '0'; $parent_data['width'] = wc_format_localized_decimal( 0 );
} }
if ( ! $parent_data['height'] ) { if ( ! $parent_data['height'] ) {
$parent_data['height'] = '0'; $parent_data['height'] = wc_format_localized_decimal( 0 );
} }
$_tax_class = null;
$_downloadable_files = '';
$_stock_status = '';
$_backorders = '';
$image_id = 0;
$_thumbnail_id = '';
$variation = get_post( $variation_id ); // Get the variation object
$menu_order = 0;
include( 'admin/meta-boxes/views/html-variation-admin.php' ); include( 'admin/meta-boxes/views/html-variation-admin.php' );
} }