WC_Product_Importer::get_product_object
Prepare a single product for create or update.
Method of the class: WC_Product_Importer{}
Hooks from the method
Returns
WC_Product|WP_Error.
Usage
// protected - for code of main (parent) or child class $result = $this->get_product_object( $data );
- $data(array) (required)
- Item data.
WC_Product_Importer::get_product_object() WC Product Importer::get product object code WC 10.7.0
protected function get_product_object( $data ) {
$id = isset( $data['id'] ) ? absint( $data['id'] ) : 0;
// Type is the most important part here because we need to be using the correct class and methods.
if ( isset( $data['type'] ) ) {
if ( ! array_key_exists( $data['type'], WC_Admin_Exporters::get_product_types() ) ) {
return new WP_Error( 'woocommerce_product_importer_invalid_type', __( 'Invalid product type.', 'woocommerce' ), array( 'status' => 401 ) );
}
try {
// Prevent getting "variation_invalid_id" error message from Variation Data Store.
if ( ProductType::VARIATION === $data['type'] ) {
$id = wp_update_post(
array(
'ID' => $id,
'post_type' => 'product_variation',
)
);
}
$product = wc_get_product_object( $data['type'], $id );
} catch ( WC_Data_Exception $e ) {
return new WP_Error( 'woocommerce_product_csv_importer_' . $e->getErrorCode(), $e->getMessage(), array( 'status' => 401 ) );
}
} elseif ( ! empty( $data['id'] ) ) {
$product = wc_get_product( $id );
if ( ! $product ) {
return new WP_Error(
'woocommerce_product_csv_importer_invalid_id',
/* translators: %d: product ID */
sprintf( __( 'Invalid product ID %d.', 'woocommerce' ), $id ),
array(
'id' => $id,
'status' => 401,
)
);
}
} else {
$product = wc_get_product_object( ProductType::SIMPLE, $id );
}
return apply_filters( 'woocommerce_product_import_get_product_object', $product, $data );
}