Revise how variation attributes are deleted/updated

Prevents issues with WPE caching when you delete and then update right
after.

Conflicts:

	includes/admin/post-types/meta-boxes/class-wc-meta-box-product-data.php
This commit is contained in:
Mike Jolley 2014-05-06 10:47:23 +01:00
parent 121022a5eb
commit 616542b09c
1 changed files with 23 additions and 21 deletions

View File

@ -1387,6 +1387,11 @@ class WC_Meta_Box_Product_Data {
} }
// Only continue if we have a variation ID
if ( ! $variation_id ) {
continue;
}
// Update post meta // Update post meta
update_post_meta( $variation_id, '_sku', wc_clean( $variable_sku[ $i ] ) ); update_post_meta( $variation_id, '_sku', wc_clean( $variable_sku[ $i ] ) );
update_post_meta( $variation_id, '_thumbnail_id', absint( $upload_image_id[ $i ] ) ); update_post_meta( $variation_id, '_thumbnail_id', absint( $upload_image_id[ $i ] ) );
@ -1481,25 +1486,22 @@ class WC_Meta_Box_Product_Data {
$variable_shipping_class[ $i ] = ! empty( $variable_shipping_class[ $i ] ) ? (int) $variable_shipping_class[ $i ] : ''; $variable_shipping_class[ $i ] = ! empty( $variable_shipping_class[ $i ] ) ? (int) $variable_shipping_class[ $i ] : '';
wp_set_object_terms( $variation_id, $variable_shipping_class[ $i ], 'product_shipping_class'); wp_set_object_terms( $variation_id, $variable_shipping_class[ $i ], 'product_shipping_class');
// Remove old taxonomies attributes so data is kept up to date // Update taxonomies - don't use wc_clean as it destroys sanitized characters
if ( $variation_id ) { $updated_attribute_keys = array();
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_key LIKE 'attribute_%%' AND post_id = %d;", $variation_id ) );
wp_cache_delete( $variation_id, 'post_meta');
}
// Update taxonomies
foreach ( $attributes as $attribute ) { foreach ( $attributes as $attribute ) {
if ( $attribute['is_variation'] ) { if ( $attribute['is_variation'] ) {
// Don't use wc_clean as it destroys sanitized characters $attribute_key = 'attribute_' . sanitize_title( $attribute['name'] );
if ( isset( $_POST[ 'attribute_' . sanitize_title( $attribute['name'] ) ][ $i ] ) ) $value = isset( $_POST[ $attribute_key ][ $i ] ) ? sanitize_title( stripslashes( $_POST[ $attribute_key ][ $i ] ) ) : '';
$value = sanitize_title( trim( stripslashes( $_POST[ 'attribute_' . sanitize_title( $attribute['name'] ) ][ $i ] ) ) ); $updated_attribute_keys[] = $attribute_key;
else update_post_meta( $variation_id, $attribute_key, $value );
$value = ''; }
update_post_meta( $variation_id, 'attribute_' . sanitize_title( $attribute['name'] ), $value );
} }
// Remove old taxonomies attributes so data is kept up to date - first get attribute key names
$delete_attribute_keys = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM {$wpdb->postmeta} WHERE meta_key LIKE 'attribute_%%' AND meta_key NOT IN ( '" . implode( "','", $updated_attribute_keys ) . "' ) AND post_id = %d;", $variation_id ) );
foreach ( $delete_attribute_keys as $key ) {
delete_post_meta( $variation_id, $key );
} }
do_action( 'woocommerce_save_product_variation', $variation_id, $i ); do_action( 'woocommerce_save_product_variation', $variation_id, $i );