WC_Product_Importer::process_item()protectedWC 1.0

Process a single item and save.

Method of the class: WC_Product_Importer{}

Return

Array|WP_Error.

Usage

// protected - for code of main (parent) or child class
$result = $this->process_item( $data );
$data(array) (required)
Raw CSV data.

WC_Product_Importer::process_item() code WC 8.7.0

protected function process_item( $data ) {
	try {
		do_action( 'woocommerce_product_import_before_process_item', $data );
		$data = apply_filters( 'woocommerce_product_import_process_item_data', $data );

		// Get product ID from SKU if created during the importation.
		if ( empty( $data['id'] ) && ! empty( $data['sku'] ) ) {
			$product_id = wc_get_product_id_by_sku( $data['sku'] );

			if ( $product_id ) {
				$data['id'] = $product_id;
			}
		}

		$object   = $this->get_product_object( $data );
		$updating = false;

		if ( is_wp_error( $object ) ) {
			return $object;
		}

		if ( $object->get_id() && 'importing' !== $object->get_status() ) {
			$updating = true;
		}

		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() ) {
			$object->set_status( 'publish' );
			$object->set_slug( '' );
		}

		$result = $object->set_props( array_diff_key( $data, array_flip( array( 'meta_data', 'raw_image_id', 'raw_gallery_image_ids', 'raw_attributes' ) ) ) );

		if ( is_wp_error( $result ) ) {
			throw new Exception( $result->get_error_message() );
		}

		if ( 'variation' === $object->get_type() ) {
			$this->set_variation_data( $object, $data );
		} else {
			$this->set_product_data( $object, $data );
		}

		$this->set_image_data( $object, $data );
		$this->set_meta_data( $object, $data );

		$object = apply_filters( 'woocommerce_product_import_pre_insert_product_object', $object, $data );
		$object->save();

		do_action( 'woocommerce_product_import_inserted_product_object', $object, $data );

		return array(
			'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() ) );
	}
}