From 36a49f87c2c675f3a7f82e47e43c89cc80e1c1dc Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 10 Apr 2015 14:50:23 +0100 Subject: [PATCH] Improve save_attributes ajax function to correctly save text attributes. Closes #7832 --- includes/class-wc-ajax.php | 18 ++++++++++++++++-- readme.txt | 1 + 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-ajax.php b/includes/class-wc-ajax.php index cf5d1fddae0..5551289f3a0 100644 --- a/includes/class-wc-ajax.php +++ b/includes/class-wc-ajax.php @@ -607,9 +607,23 @@ class WC_AJAX { if ( is_array( $attribute_values[ $i ] ) ) { $values = array_map( 'sanitize_title', $attribute_values[ $i ] ); - // Text based attributes - Posted values are term names - don't change to slugs + // Text based attributes - Posted values are term names, wp_set_object_terms wants ids or slugs. } else { - $values = array_map( 'stripslashes', array_map( 'strip_tags', explode( WC_DELIMITER, $attribute_values[ $i ] ) ) ); + $values = array(); + $raw_values = array_map( 'stripslashes', array_map( 'strip_tags', explode( WC_DELIMITER, $attribute_values[ $i ] ) ) ); + + foreach ( $raw_values as $value ) { + $term = get_term_by( 'name', $value, $attribute_names[ $i ] ); + if ( ! $term ) { + $term = wp_insert_term( $value, $attribute_names[ $i ] ); + + if ( $term && ! is_wp_error( $term ) ) { + $values[] = $term['term_id']; + } + } else { + $values[] = $term->term_id; + } + } } // Remove empty items in the array diff --git a/readme.txt b/readme.txt index fd61109d3ca..6d77d45cd0e 100644 --- a/readme.txt +++ b/readme.txt @@ -141,6 +141,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc * Feature - Show full category hierarchy in permalinks. * Fix - Ensure coupon taxes are reset when calculating totals. * Fix - Improve discount amount rounding. +* Tweak - Improve save_attributes ajax function to correctly save text attributes. * Tweak - Base discounts on the undiscounted price. #5874 * Tweak - Added wc_product_cat_class functions.