WC_Product_CSV_Importer::expand_data │ protected │ WC 1.0
Expand special and internal data into the correct formats for the product CRUD.
Метод класса: WC_Product_CSV_Importer{}
Хуки из метода
Возвращает
Массив.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->expand_data( $data );
- $data(массив) (обязательный)
- Data to import.
Код WC_Product_CSV_Importer::expand_data() WC Product CSV Importer::expand data WC 10.5.2
protected function expand_data( $data ) {
$data = apply_filters( 'woocommerce_product_importer_pre_expand_data', $data );
// Images field maps to image and gallery id fields.
if ( isset( $data['images'] ) ) {
$images = $data['images'];
$data['raw_image_id'] = array_shift( $images );
if ( ! empty( $images ) ) {
$data['raw_gallery_image_ids'] = $images;
}
unset( $data['images'] );
}
// Type, virtual and downloadable are all stored in the same column.
if ( isset( $data['type'] ) ) {
$data['type'] = array_map( 'strtolower', $data['type'] );
$data['virtual'] = in_array( 'virtual', $data['type'], true );
$data['downloadable'] = in_array( 'downloadable', $data['type'], true );
// Convert type to string.
$data['type'] = current( array_diff( $data['type'], array( 'virtual', 'downloadable' ) ) );
if ( ! $data['type'] ) {
$data['type'] = ProductType::SIMPLE;
}
}
// Status is mapped from a special published field.
if ( isset( $data['published'] ) ) {
$published = $data['published'];
if ( is_float( $published ) ) {
$published = (int) $published;
}
$statuses = array(
-1 => ProductStatus::DRAFT,
0 => ProductStatus::PRIVATE,
1 => ProductStatus::PUBLISH,
);
$data['status'] = $statuses[ $published ] ?? ProductStatus::DRAFT;
// Fix draft status of variations.
if ( ProductType::VARIATION === ( $data['type'] ?? null ) && -1 === $published ) {
$data['status'] = ProductStatus::PUBLISH;
}
unset( $data['published'] );
}
if ( isset( $data['stock_quantity'] ) ) {
if ( '' === $data['stock_quantity'] ) {
$data['manage_stock'] = false;
$data['stock_status'] = isset( $data['stock_status'] ) ? $data['stock_status'] : true;
} else {
$data['manage_stock'] = true;
}
}
// Stock is bool or 'backorder'.
if ( isset( $data['stock_status'] ) ) {
if ( 'backorder' === $data['stock_status'] ) {
$data['stock_status'] = ProductStockStatus::ON_BACKORDER;
} else {
$data['stock_status'] = $data['stock_status'] ? ProductStockStatus::IN_STOCK : ProductStockStatus::OUT_OF_STOCK;
}
}
// Prepare grouped products.
if ( isset( $data['grouped_products'] ) ) {
$data['children'] = $data['grouped_products'];
unset( $data['grouped_products'] );
}
// Tag ids.
if ( isset( $data['tag_ids_spaces'] ) ) {
$data['tag_ids'] = $data['tag_ids_spaces'];
unset( $data['tag_ids_spaces'] );
}
if ( ! $this->cogs_is_enabled ) {
unset( $data['cogs_value'] );
}
// Handle special column names which span multiple columns.
$attributes = array();
$downloads = array();
$meta_data = array();
foreach ( $data as $key => $value ) {
if ( $this->starts_with( $key, 'attributes:name' ) ) {
if ( ! empty( $value ) ) {
$attributes[ str_replace( 'attributes:name', '', $key ) ]['name'] = $value;
}
unset( $data[ $key ] );
} elseif ( $this->starts_with( $key, 'attributes:value' ) ) {
$attributes[ str_replace( 'attributes:value', '', $key ) ]['value'] = $value;
unset( $data[ $key ] );
} elseif ( $this->starts_with( $key, 'attributes:taxonomy' ) ) {
$attributes[ str_replace( 'attributes:taxonomy', '', $key ) ]['taxonomy'] = wc_string_to_bool( $value );
unset( $data[ $key ] );
} elseif ( $this->starts_with( $key, 'attributes:visible' ) ) {
$attributes[ str_replace( 'attributes:visible', '', $key ) ]['visible'] = wc_string_to_bool( $value );
unset( $data[ $key ] );
} elseif ( $this->starts_with( $key, 'attributes:default' ) ) {
if ( ! empty( $value ) ) {
$attributes[ str_replace( 'attributes:default', '', $key ) ]['default'] = $value;
}
unset( $data[ $key ] );
} elseif ( $this->starts_with( $key, 'downloads:id' ) ) {
if ( ! empty( $value ) ) {
$downloads[ str_replace( 'downloads:id', '', $key ) ]['id'] = $value;
}
unset( $data[ $key ] );
} elseif ( $this->starts_with( $key, 'downloads:name' ) ) {
if ( ! empty( $value ) ) {
$downloads[ str_replace( 'downloads:name', '', $key ) ]['name'] = $value;
}
unset( $data[ $key ] );
} elseif ( $this->starts_with( $key, 'downloads:url' ) ) {
if ( ! empty( $value ) ) {
$downloads[ str_replace( 'downloads:url', '', $key ) ]['url'] = $value;
}
unset( $data[ $key ] );
} elseif ( $this->starts_with( $key, 'meta:' ) ) {
$meta_data[] = array(
'key' => str_replace( 'meta:', '', $key ),
'value' => $value,
);
unset( $data[ $key ] );
}
}
if ( ! empty( $attributes ) ) {
// Remove empty attributes and clear indexes.
foreach ( $attributes as $attribute ) {
if ( empty( $attribute['name'] ) ) {
continue;
}
$data['raw_attributes'][] = $attribute;
}
}
if ( ! empty( $downloads ) ) {
$data['downloads'] = array();
foreach ( $downloads as $key => $file ) {
if ( empty( $file['url'] ) ) {
continue;
}
$data['downloads'][] = array(
'download_id' => isset( $file['id'] ) ? $file['id'] : null,
'name' => $file['name'] ? $file['name'] : wc_get_filename_from_url( $file['url'] ),
'file' => $file['url'],
);
}
}
if ( ! empty( $meta_data ) ) {
$data['meta_data'] = $meta_data;
}
return $data;
}