rebase on oct-10-2023
This commit is contained in:
@@ -250,11 +250,12 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
|
||||
if ( 'external' === $object->get_type() ) {
|
||||
unset( $data['manage_stock'], $data['stock_status'], $data['backorders'], $data['low_stock_amount'] );
|
||||
}
|
||||
|
||||
$is_variation = false;
|
||||
if ( 'variation' === $object->get_type() ) {
|
||||
if ( isset( $data['status'] ) && -1 === $data['status'] ) {
|
||||
$data['status'] = 0; // Variations cannot be drafts - set to private.
|
||||
}
|
||||
$is_variation = true;
|
||||
}
|
||||
|
||||
if ( 'importing' === $object->get_status() ) {
|
||||
@@ -283,8 +284,9 @@ abstract class WC_Product_Importer implements WC_Importer_Interface {
|
||||
do_action( 'woocommerce_product_import_inserted_product_object', $object, $data );
|
||||
|
||||
return array(
|
||||
'id' => $object->get_id(),
|
||||
'updated' => $updating,
|
||||
'id' => $object->get_id(),
|
||||
'updated' => $updating,
|
||||
'is_variation' => $is_variation,
|
||||
);
|
||||
} catch ( Exception $e ) {
|
||||
return new WP_Error( 'woocommerce_product_importer_error', $e->getMessage(), array( 'status' => $e->getCode() ) );
|
||||
|
||||
@@ -590,6 +590,35 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse dates from a CSV.
|
||||
* Dates can be Unix timestamps or in any format supported by strtotime().
|
||||
*
|
||||
* @param string $value Field value.
|
||||
*
|
||||
* @return string|null
|
||||
*/
|
||||
public function parse_datetime_field( $value ) {
|
||||
try {
|
||||
// If value is a Unix timestamp, convert it to a datetime string.
|
||||
if ( is_numeric( $value ) ) {
|
||||
$datetime = new DateTime( "@{$value}" );
|
||||
// Return datetime string in ISO8601 format (eg. 2018-01-01T00:00:00Z) to preserve UTC timezone since Unix timestamps are always UTC.
|
||||
return $datetime->format( 'Y-m-d\TH:i:s\Z' );
|
||||
}
|
||||
// Check whether the value is a valid date string.
|
||||
if ( false !== strtotime( $value ) ) {
|
||||
// If the value is a valid date string, return as is.
|
||||
return $value;
|
||||
}
|
||||
} catch ( Exception $e ) {
|
||||
// DateTime constructor throws an exception if the value is not a valid Unix timestamp.
|
||||
return null;
|
||||
}
|
||||
// If value is not valid Unix timestamp or date string, return null.
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse backorders from a CSV.
|
||||
*
|
||||
@@ -725,8 +754,8 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
|
||||
'type' => array( $this, 'parse_comma_field' ),
|
||||
'published' => array( $this, 'parse_published_field' ),
|
||||
'featured' => array( $this, 'parse_bool_field' ),
|
||||
'date_on_sale_from' => array( $this, 'parse_date_field' ),
|
||||
'date_on_sale_to' => array( $this, 'parse_date_field' ),
|
||||
'date_on_sale_from' => array( $this, 'parse_datetime_field' ),
|
||||
'date_on_sale_to' => array( $this, 'parse_datetime_field' ),
|
||||
'name' => array( $this, 'parse_skip_field' ),
|
||||
'short_description' => array( $this, 'parse_description_field' ),
|
||||
'description' => array( $this, 'parse_description_field' ),
|
||||
@@ -1080,10 +1109,11 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
|
||||
$index = 0;
|
||||
$update_existing = $this->params['update_existing'];
|
||||
$data = array(
|
||||
'imported' => array(),
|
||||
'failed' => array(),
|
||||
'updated' => array(),
|
||||
'skipped' => array(),
|
||||
'imported' => array(),
|
||||
'imported_variations' => array(),
|
||||
'failed' => array(),
|
||||
'updated' => array(),
|
||||
'skipped' => array(),
|
||||
);
|
||||
|
||||
foreach ( $this->parsed_data as $parsed_data_key => $parsed_data ) {
|
||||
@@ -1150,7 +1180,11 @@ class WC_Product_CSV_Importer extends WC_Product_Importer {
|
||||
} elseif ( $result['updated'] ) {
|
||||
$data['updated'][] = $result['id'];
|
||||
} else {
|
||||
$data['imported'][] = $result['id'];
|
||||
if ( $result['is_variation'] ) {
|
||||
$data['imported_variations'][] = $result['id'];
|
||||
} else {
|
||||
$data['imported'][] = $result['id'];
|
||||
}
|
||||
}
|
||||
|
||||
$index ++;
|
||||
|
||||
Reference in New Issue
Block a user