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
Raw data.
$mapped_keys  : array
Mapped keys - CSV headers.
$params  : array
Importer parameters.
$parsed_data  : array
Parsed data.
$parsing_raw_data_index  : int
Tracks current row being parsed.
$raw_data  : array
Raw data.
$raw_keys  : array
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
Get file mapped headers.
get_params()  : array
Get importer parameters.
get_parsed_data()  : array
Get parsed data.
get_percent_complete()  : int
Get file pointer position as a percentage of file size.
get_raw_data()  : array
Get raw data.
get_raw_keys()  : array
Get file raw headers.
import()  : array
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
Parse a category field from a CSV.
parse_comma_field()  : array
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
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
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
Parse a tag field from a CSV.
parse_tags_spaces_field()  : array
Parse a tag field from a CSV with space separators.
parse_tax_status_field()  : string
Parse the tax status field.
expand_data()  : array
Expand special and internal data into the correct formats for the product CRUD.
explode_values()  : array
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
Deprecated get formatting callback method.
get_formatting_callback()  : array
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
Get variation parent attributes and set "is_variation".
memory_exceeded()  : bool
Memory exceeded
process_item()  : array|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 @.

Properties

Methods

__construct()

Initialize importer.

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

File to read.

$params : array = 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

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

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

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

Parameters
$value : string

Field value.

Return values
arrayof arrays with "parent" and "name" keys.

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_download_file_field()

Parse download file urls, we should allow shortcodes here.

public parse_download_file_field(string $value) : string

Allow shortcodes if present, othersiwe 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
Parameters
$value : string

Field value.

Return values
array

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
Parameters
$value : string

Field value.

Return values
array

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_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_spaces_field()

Parse a tag field from a CSV with space separators.

public parse_tags_spaces_field(string $value) : array
Parameters
$value : string

Field value.

Return values
array

expand_data()

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

protected expand_data(array $data) : array
Parameters
$data : array

Data to import.

Return values
array

explode_values()

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

protected explode_values(string $value[, string $separator = ',' ]) : array
Parameters
$value : string

Value to explode.

$separator : string = ','

Separator separating each value. Defaults to comma.

Tags
since
3.2.0
Return values
array

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_variation_parent_attributes()

Get variation parent attributes and set "is_variation".

protected get_variation_parent_attributes(array $attributes, WC_Product $parent) : array
Parameters
$attributes : array

Attributes list.

$parent : WC_Product

Parent product data.

Return values
array

process_item()

Process a single item and save.

protected process_item(array $data) : array|WP_Error
Parameters
$data : array

Raw CSV data.

Tags
throws
Exception

If item cannot be processed.

Return values
array|WP_Error

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