From a5a7c71aa7a4239a22d1f5e5103e8b3da9d744c6 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 17 Aug 2015 15:11:34 +0100 Subject: [PATCH] Keep new variation in sync so actions can modify data Closes #8858 --- includes/class-wc-ajax.php | 90 ++++++++++++++++++++++++++------------ 1 file changed, 62 insertions(+), 28 deletions(-) diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index 3fcd04ee42d..7ea16fb5f82 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -753,76 +753,110 @@ class WC_AJAX { do_action( 'woocommerce_create_product_variation', $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'; - $variation_data = get_post_meta( $variation_id ); - $variation_data['variation_post_id'] = $variation_id; + foreach ( $variation_fields as $field => $value ) { + $variation_data[ $field ] = isset( $variation_meta[ $field ][0] ) ? maybe_unserialize( $variation_meta[ $field ][0] ) : $value; + } - // Get attributes - $attributes = (array) maybe_unserialize( get_post_meta( $post_id, '_product_attributes', true ) ); + // Add the variation attributes + $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 - $tax_classes = WC_Tax::get_tax_classes(); - $tax_class_options = array(); - $tax_class_options[''] = __( 'Standard', 'woocommerce' ); + $tax_classes = WC_Tax::get_tax_classes(); + $tax_class_options = array(); + $tax_class_options[''] = __( 'Standard', 'woocommerce' ); - if ( $tax_classes ) { + if ( ! empty( $tax_classes ) ) { 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( 'no' => __( 'Do not allow', 'woocommerce' ), 'notify' => __( 'Allow, but notify customer', 'woocommerce' ), 'yes' => __( 'Allow', 'woocommerce' ) ); + // set stock status options $stock_status_options = array( 'instock' => __( 'In 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( 'id' => $post_id, 'attributes' => $attributes, 'tax_class_options' => $tax_class_options, 'sku' => get_post_meta( $post_id, '_sku', true ), - 'weight' => get_post_meta( $post_id, '_weight', true ), - 'length' => get_post_meta( $post_id, '_length', true ), - 'width' => get_post_meta( $post_id, '_width', true ), - 'height' => get_post_meta( $post_id, '_height', true ), + 'weight' => wc_format_localized_decimal( get_post_meta( $post_id, '_weight', true ) ), + 'length' => wc_format_localized_decimal( get_post_meta( $post_id, '_length', true ) ), + '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, 'stock_status_options' => $stock_status_options ); if ( ! $parent_data['weight'] ) { - $parent_data['weight'] = '0.00'; + $parent_data['weight'] = wc_format_localized_decimal( 0 ); } if ( ! $parent_data['length'] ) { - $parent_data['length'] = '0'; + $parent_data['length'] = wc_format_localized_decimal( 0 ); } if ( ! $parent_data['width'] ) { - $parent_data['width'] = '0'; + $parent_data['width'] = wc_format_localized_decimal( 0 ); } 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' ); }