Skip empty attributes and reset indexes

This commit is contained in:
Claudio Sanches 2017-05-30 19:26:31 -03:00
parent 611695b9f4
commit 51ddc36080
2 changed files with 23 additions and 16 deletions

View File

@ -501,15 +501,11 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
unset( $data[ $key ] ); unset( $data[ $key ] );
} elseif ( $this->starts_with( $key, 'attributes:value' ) ) { } elseif ( $this->starts_with( $key, 'attributes:value' ) ) {
if ( ! empty( $value ) ) {
$attributes[ str_replace( 'attributes:value', '', $key ) ]['value'] = $value; $attributes[ str_replace( 'attributes:value', '', $key ) ]['value'] = $value;
}
unset( $data[ $key ] ); unset( $data[ $key ] );
} elseif ( $this->starts_with( $key, 'attributes:visible' ) ) { } elseif ( $this->starts_with( $key, 'attributes:visible' ) ) {
if ( ! empty( $value ) ) {
$attributes[ str_replace( 'attributes:visible', '', $key ) ]['visible'] = wc_string_to_bool( $value ); $attributes[ str_replace( 'attributes:visible', '', $key ) ]['visible'] = wc_string_to_bool( $value );
}
unset( $data[ $key ] ); unset( $data[ $key ] );
} elseif ( $this->starts_with( $key, 'attributes:default' ) ) { } elseif ( $this->starts_with( $key, 'attributes:default' ) ) {
@ -542,7 +538,14 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
} }
if ( ! empty( $attributes ) ) { if ( ! empty( $attributes ) ) {
$data['raw_attributes'] = $attributes; // Remove empty attributes and clear indexes.
foreach ( $attributes as $attribute ) {
if ( empty( $attribute['name'] ) ) {
continue;
}
$data['raw_attributes'][] = $attribute;
}
} }
if ( ! empty( $downloads ) ) { if ( ! empty( $downloads ) ) {

View File

@ -281,7 +281,11 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
'manage_stock' => true, 'manage_stock' => true,
'virtual' => false, 'virtual' => false,
'downloadable' => false, 'downloadable' => false,
'raw_attributes' => array( 1 => array( 'name' => 'Color' ) ), 'raw_attributes' => array(
array(
'name' => 'Color',
),
),
), ),
array( array(
'type' => 'simple', 'type' => 'simple',
@ -319,10 +323,10 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
'downloadable' => true, 'downloadable' => true,
'manage_stock' => false, 'manage_stock' => false,
'raw_attributes' => array( 'raw_attributes' => array(
1 => array( array(
'name' => 'Label', 'name' => 'Label',
), ),
2 => array( array(
'value' => array( '180-Gram' ), 'value' => array( '180-Gram' ),
'name' => 'Vinyl', 'name' => 'Vinyl',
), ),
@ -409,11 +413,11 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
'downloadable' => false, 'downloadable' => false,
'manage_stock' => false, 'manage_stock' => false,
'raw_attributes' => array( 'raw_attributes' => array(
1 => array( array(
'name' => 'Color', 'name' => 'Color',
'default' => 'Green', 'default' => 'Green',
), ),
2 => array( array(
'value' => array( 'M', 'L' ), 'value' => array( 'M', 'L' ),
'name' => 'Size', 'name' => 'Size',
'default' => 'L' 'default' => 'L'
@ -455,10 +459,10 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
'downloadable' => false, 'downloadable' => false,
'manage_stock' => true, 'manage_stock' => true,
'raw_attributes' => array( 'raw_attributes' => array(
1 => array( array(
'name' => 'Color', 'name' => 'Color',
), ),
2 => array( array(
'value' => array( 'M' ), 'value' => array( 'M' ),
'name' => 'Size', 'name' => 'Size',
), ),
@ -499,10 +503,10 @@ class WC_Tests_Product_CSV_Importer extends WC_Unit_Test_Case {
'downloadable' => false, 'downloadable' => false,
'manage_stock' => true, 'manage_stock' => true,
'raw_attributes' => array( 'raw_attributes' => array(
1 => array( array(
'name' => 'Color', 'name' => 'Color',
), ),
2 => array( array(
'value' => array( 'L' ), 'value' => array( 'L' ),
'name' => 'Size' 'name' => 'Size'
) )