WooCommerce Code Reference

WC_Product_CSV_Importer extends WC_Product_Importer
in package

WC_Product_CSV_Importer Class.

Table of Contents

$file  : string
CSV file.
$file_position  : int
The file position after the last read.
$file_positions  : array<string|int, mixed>
Raw data.
$mapped_keys  : array<string|int, mixed>
Mapped keys - CSV headers.
$params  : array<string|int, mixed>
Importer parameters.
$parsed_data  : array<string|int, mixed>
Parsed data.
$parsing_raw_data_index  : int
Tracks current row being parsed.
$raw_data  : array<string|int, mixed>
Raw data.
$raw_keys  : array<string|int, mixed>
Raw keys - CSV raw headers.
$start_time  : int
Start time of current import.
__construct()  : mixed
Initialize importer.
get_attachment_id_from_url()  : int
Get attachment ID.
get_attribute_taxonomy_id()  : int
Get attribute taxonomy ID from the imported data.
get_file_position()  : int
Get file pointer position from the last read.
get_mapped_keys()  : array<string|int, mixed>
Get file mapped headers.
get_params()  : array<string|int, mixed>
Get importer parameters.
get_parsed_data()  : array<string|int, mixed>
Get parsed data.
get_percent_complete()  : int
Get file pointer position as a percentage of file size.
get_raw_data()  : array<string|int, mixed>
Get raw data.
get_raw_keys()  : array<string|int, mixed>
Get file raw headers.
import()  : array<string|int, mixed>
Process importer.
parse_backorders_field()  : string
Parse backorders from a CSV.
parse_bool_field()  : bool|string
Parse a field that is generally '1' or '0' but can be something else.
parse_categories_field()  : array<string|int, mixed>
Parse a category field from a CSV.
parse_comma_field()  : array<string|int, mixed>
Parse a comma-delineated field from a CSV.
parse_date_field()  : string|null
Parse dates from a CSV.
parse_description_field()  : string
Parse a description value field
parse_download_file_field()  : string
Parse download file urls, we should allow shortcodes here.
parse_float_field()  : float|string
Parse a float value field.
parse_id_field()  : int
Parse the ID field.
parse_images_field()  : array<string|int, mixed>
Parse images list from a CSV. Images can be filenames or URLs.
parse_int_field()  : int
Parse an int value field
parse_published_field()  : float|string
Parse the published field. 1 is published, 0 is private, -1 is draft.
parse_relative_comma_field()  : array<string|int, mixed>
Parse relative comma-delineated field and return product ID.
parse_relative_field()  : int|string
Parse relative field and return product ID.
parse_shipping_class_field()  : int
Parse a shipping class field from a CSV.
parse_skip_field()  : string
Just skip current field.
parse_stock_quantity_field()  : float|string
Parse the stock qty field.
parse_tags_field()  : array<string|int, mixed>
Parse a tag field from a CSV.
parse_tags_spaces_field()  : array<string|int, mixed>
Parse a tag field from a CSV with space separators.
parse_tax_status_field()  : string
Parse the tax status field.
expand_data()  : array<string|int, mixed>
Expand special and internal data into the correct formats for the product CRUD.
explode_values()  : array<string|int, mixed>
Explode CSV cell values using commas by default, and handling escaped separators.
explode_values_formatter()  : string
Remove formatting and trim each value.
get_formating_callback()  : array<string|int, mixed>
Deprecated get formatting callback method.
get_formatting_callback()  : array<string|int, mixed>
Get formatting callback.
get_memory_limit()  : int
Get memory limit
get_product_object()  : WC_Product|WP_Error
Prepare a single product for create or update.
get_row_id()  : string
Get a string to identify the row from parsed data.
get_variation_parent_attributes()  : array<string|int, mixed>
Get variation parent attributes and set "is_variation".
memory_exceeded()  : bool
Memory exceeded
process_item()  : array<string|int, mixed>|WP_Error
Process a single item and save.
read_file()  : mixed
Read file.
remove_utf8_bom()  : string
Remove UTF-8 BOM signature.
set_image_data()  : mixed
Convert raw image URLs to IDs and set.
set_mapped_keys()  : mixed
Set file mapped keys.
set_meta_data()  : mixed
Append meta data.
set_parsed_data()  : mixed
Map and format raw data to known fields.
set_product_data()  : mixed
Set product data.
set_variation_data()  : WC_Product|WP_Error
Set variation data.
starts_with()  : bool
Check if strings starts with determined word.
time_exceeded()  : bool
Time exceeded.
unescape_data()  : string
The exporter prepends a ' to escape fields that start with =, +, - or @.
adjust_character_encoding()  : string
Convert a string from the input encoding to UTF-8.

Properties

Methods

__construct()

Initialize importer.

public __construct(string $file[, array<string|int, mixed> $params = array() ]) : mixed
Parameters
$file : string

File to read.

$params : array<string|int, mixed> = array()

Arguments for the parser.

Return values
mixed

get_attachment_id_from_url()

Get attachment ID.

public get_attachment_id_from_url(string $url, int $product_id) : int
Parameters
$url : string

Attachment URL.

$product_id : int

Product ID.

Tags
throws
Exception

If attachment cannot be loaded.

Return values
int

get_attribute_taxonomy_id()

Get attribute taxonomy ID from the imported data.

public get_attribute_taxonomy_id(string $raw_name) : int

If does not exists register a new attribute.

Parameters
$raw_name : string

Attribute name.

Tags
throws
Exception

If taxonomy cannot be loaded.

Return values
int

import()

Process importer.

public import() : array<string|int, mixed>

Do not import products with IDs or SKUs that already exist if option update existing is false, and likewise, if updating products, do not process rows which do not exist if an ID/SKU is provided.

Return values
array<string|int, mixed>

parse_bool_field()

Parse a field that is generally '1' or '0' but can be something else.

public parse_bool_field(string $value) : bool|string
Parameters
$value : string

Field value.

Return values
bool|string

parse_categories_field()

Parse a category field from a CSV.

public parse_categories_field(string $value) : array<string|int, mixed>

Categories are separated by commas and subcategories are "parent > subcategory".

Parameters
$value : string

Field value.

Return values
array<string|int, mixed>of arrays with "parent" and "name" keys.

parse_comma_field()

Parse a comma-delineated field from a CSV.

public parse_comma_field(string $value) : array<string|int, mixed>
Parameters
$value : string

Field value.

Return values
array<string|int, mixed>

parse_date_field()

Parse dates from a CSV.

public parse_date_field(string $value) : string|null

Dates requires the format YYYY-MM-DD and time is optional.

Parameters
$value : string

Field value.

Return values
string|null

parse_description_field()

Parse a description value field

public parse_description_field(string $description) : string
Parameters
$description : string

field value.

Return values
string

parse_download_file_field()

Parse download file urls, we should allow shortcodes here.

public parse_download_file_field(string $value) : string

Allow shortcodes if present, otherwise esc_url the value.

Parameters
$value : string

Field value.

Return values
string

parse_id_field()

Parse the ID field.

public parse_id_field(string $value) : int

If we're not doing an update, create a placeholder product so mapping works for rows following this one.

Parameters
$value : string

Field value.

Return values
int

parse_images_field()

Parse images list from a CSV. Images can be filenames or URLs.

public parse_images_field(string $value) : array<string|int, mixed>
Parameters
$value : string

Field value.

Return values
array<string|int, mixed>

parse_published_field()

Parse the published field. 1 is published, 0 is private, -1 is draft.

public parse_published_field(string $value) : float|string

Alternatively, 'true' can be used for published and 'false' for draft.

Parameters
$value : string

Field value.

Return values
float|string

parse_relative_comma_field()

Parse relative comma-delineated field and return product ID.

public parse_relative_comma_field(string $value) : array<string|int, mixed>
Parameters
$value : string

Field value.

Return values
array<string|int, mixed>

parse_relative_field()

Parse relative field and return product ID.

public parse_relative_field(string $value) : int|string

Handles id:xx and SKUs.

If mapping to an id: and the product ID does not exist, this link is not valid.

If mapping to a SKU and the product ID does not exist, a temporary object will be created so it can be updated later.

Parameters
$value : string

Field value.

Return values
int|string

parse_shipping_class_field()

Parse a shipping class field from a CSV.

public parse_shipping_class_field(string $value) : int
Parameters
$value : string

Field value.

Return values
int

parse_skip_field()

Just skip current field.

public parse_skip_field(string $value) : string

By default is applied wc_clean() to all not listed fields in self::get_formatting_callback(), use this method to skip any formatting.

Parameters
$value : string

Field value.

Return values
string

parse_stock_quantity_field()

Parse the stock qty field.

public parse_stock_quantity_field(string $value) : float|string
Parameters
$value : string

Field value.

Return values
float|string

parse_tags_field()

Parse a tag field from a CSV.

public parse_tags_field(string $value) : array<string|int, mixed>
Parameters
$value : string

Field value.

Return values
array<string|int, mixed>

parse_tags_spaces_field()

Parse a tag field from a CSV with space separators.

public parse_tags_spaces_field(string $value) : array<string|int, mixed>
Parameters
$value : string

Field value.

Return values
array<string|int, mixed>

expand_data()

Expand special and internal data into the correct formats for the product CRUD.

protected expand_data(array<string|int, mixed> $data) : array<string|int, mixed>
Parameters
$data : array<string|int, mixed>

Data to import.

Return values
array<string|int, mixed>

explode_values()

Explode CSV cell values using commas by default, and handling escaped separators.

protected explode_values(string $value[, string $separator = ',' ]) : array<string|int, mixed>
Parameters
$value : string

Value to explode.

$separator : string = ','

Separator separating each value. Defaults to comma.

Tags
since
3.2.0
Return values
array<string|int, mixed>

explode_values_formatter()

Remove formatting and trim each value.

protected explode_values_formatter(string $value) : string
Parameters
$value : string

Value to format.

Tags
since
3.2.0
Return values
string

get_formating_callback()

Deprecated get formatting callback method.

protected get_formating_callback() : array<string|int, mixed>
Tags
deprecated
4.3.0
Return values
array<string|int, mixed>

get_row_id()

Get a string to identify the row from parsed data.

protected get_row_id(array<string|int, mixed> $parsed_data) : string
Parameters
$parsed_data : array<string|int, mixed>

Parsed data.

Return values
string

get_variation_parent_attributes()

Get variation parent attributes and set "is_variation".

protected get_variation_parent_attributes(array<string|int, mixed> $attributes, WC_Product $parent) : array<string|int, mixed>
Parameters
$attributes : array<string|int, mixed>

Attributes list.

$parent : WC_Product

Parent product data.

Return values
array<string|int, mixed>

process_item()

Process a single item and save.

protected process_item(array<string|int, mixed> $data) : array<string|int, mixed>|WP_Error
Parameters
$data : array<string|int, mixed>

Raw CSV data.

Tags
throws
Exception

If item cannot be processed.

Return values
array<string|int, mixed>|WP_Error

set_product_data()

Set product data.

protected set_product_data(WC_Product &$product, array<string|int, mixed> $data) : mixed
Parameters
$product : WC_Product

Product instance.

$data : array<string|int, mixed>

Item data.

Tags
throws
Exception

If data cannot be set.

Return values
mixed

starts_with()

Check if strings starts with determined word.

protected starts_with(string $haystack, string $needle) : bool
Parameters
$haystack : string

Complete sentence.

$needle : string

Excerpt.

Return values
bool

time_exceeded()

Time exceeded.

protected time_exceeded() : bool

Ensures the batch never exceeds a sensible time limit. A timeout limit of 30s is common on shared hosting.

Return values
bool

unescape_data()

The exporter prepends a ' to escape fields that start with =, +, - or @.

protected unescape_data(string $value) : string

Remove the prepended ' character preceding those characters.

Parameters
$value : string

A string that may or may not have been escaped with '.

Tags
since
3.5.2
Return values
string

adjust_character_encoding()

Convert a string from the input encoding to UTF-8.

private adjust_character_encoding(string $value) : string
Parameters
$value : string

The string to convert.

Return values
stringThe converted string.