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 ] );
} 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 ] );
} 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 ] );
} elseif ( $this->starts_with( $key, 'attributes:default' ) ) {
@ -542,7 +538,14 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
}
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 ) ) {

View File

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