Fixed conflict between global attributes and custom attributes.

Check if is a custom attribute before trying to get an ID from a global attribute.
This commit is contained in:
Claudio Sanches 2017-05-03 16:30:50 -03:00
parent 7edf10e9e1
commit 63552c9757
2 changed files with 12 additions and 4 deletions

View File

@ -225,8 +225,13 @@ class WC_Meta_Box_Product_Data {
if ( empty( $attribute_names[ $i ] ) || ! isset( $attribute_values[ $i ] ) ) { if ( empty( $attribute_names[ $i ] ) || ! isset( $attribute_values[ $i ] ) ) {
continue; continue;
} }
$attribute_id = 0;
$attribute_name = wc_clean( $attribute_names[ $i ] ); $attribute_name = wc_clean( $attribute_names[ $i ] );
if ( 'pa_' === substr( $attribute_name, 0, 3 ) ) {
$attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_name ); $attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_name );
}
$options = isset( $attribute_values[ $i ] ) ? $attribute_values[ $i ] : ''; $options = isset( $attribute_values[ $i ] ) ? $attribute_values[ $i ] : '';
if ( is_array( $options ) ) { if ( is_array( $options ) ) {

View File

@ -369,6 +369,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
if ( ! empty( $meta_values ) && is_array( $meta_values ) ) { if ( ! empty( $meta_values ) && is_array( $meta_values ) ) {
$attributes = array(); $attributes = array();
foreach ( $meta_values as $meta_value ) { foreach ( $meta_values as $meta_value ) {
$id = 0;
$meta_value = array_merge( array( $meta_value = array_merge( array(
'name' => '', 'name' => '',
'value' => '', 'value' => '',
@ -378,17 +379,20 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
'is_taxonomy' => 0, 'is_taxonomy' => 0,
), (array) $meta_value ); ), (array) $meta_value );
// Check if is a taxonomy attribute.
if ( ! empty( $meta_value['is_taxonomy'] ) ) { if ( ! empty( $meta_value['is_taxonomy'] ) ) {
if ( ! taxonomy_exists( $meta_value['name'] ) ) { if ( ! taxonomy_exists( $meta_value['name'] ) ) {
continue; continue;
} }
$id = wc_attribute_taxonomy_id_by_name( $meta_value['name'] );
$options = wc_get_object_terms( $product->get_id(), $meta_value['name'], 'term_id' ); $options = wc_get_object_terms( $product->get_id(), $meta_value['name'], 'term_id' );
} else { } else {
$options = wc_get_text_attributes( $meta_value['value'] ); $options = wc_get_text_attributes( $meta_value['value'] );
} }
$attribute = new WC_Product_Attribute(); $attribute = new WC_Product_Attribute();
$attribute->set_id( wc_attribute_taxonomy_id_by_name( $meta_value['name'] ) );
$attribute->set_id( $id );
$attribute->set_name( $meta_value['name'] ); $attribute->set_name( $meta_value['name'] );
$attribute->set_options( $options ); $attribute->set_options( $options );
$attribute->set_position( $meta_value['position'] ); $attribute->set_position( $meta_value['position'] );
@ -652,7 +656,6 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
} elseif ( $attribute->is_taxonomy() ) { } elseif ( $attribute->is_taxonomy() ) {
wp_set_object_terms( $product->get_id(), wp_list_pluck( $attribute->get_terms(), 'term_id' ), $attribute->get_name() ); wp_set_object_terms( $product->get_id(), wp_list_pluck( $attribute->get_terms(), 'term_id' ), $attribute->get_name() );
} else { } else {
$value = wc_implode_text_attributes( $attribute->get_options() ); $value = wc_implode_text_attributes( $attribute->get_options() );
} }