From cd137915b87660084ae2e4595b11531b5a2e5490 Mon Sep 17 00:00:00 2001 From: Claudio Sanches Date: Mon, 6 Jan 2020 21:36:54 -0300 Subject: [PATCH] Set correct post type for product variation And catch any WC_Data_Exception --- includes/import/abstract-wc-product-importer.php | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/includes/import/abstract-wc-product-importer.php b/includes/import/abstract-wc-product-importer.php index 1b6fdc5d2d1..68f872062fc 100644 --- a/includes/import/abstract-wc-product-importer.php +++ b/includes/import/abstract-wc-product-importer.php @@ -173,7 +173,21 @@ abstract class WC_Product_Importer implements WC_Importer_Interface { return new WP_Error( 'woocommerce_product_importer_invalid_type', __( 'Invalid product type.', 'woocommerce' ), array( 'status' => 401 ) ); } - $product = wc_get_product_object( $data['type'], $id ); + try { + // Prevent getting "variation_invalid_id" error message from Variation Data Store. + if ( '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 );