WC_Product_Importer::get_product_object()
Prepare a single product for create or update.
Метод класса: WC_Product_Importer{}
Хуки из метода
Возвращает
WC_Product|WP_Error
.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->get_product_object( $data );
- $data(массив) (обязательный)
- Item data.
Код WC_Product_Importer::get_product_object() WC Product Importer::get product object WC 9.3.3
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 ( '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( 'simple', $id ); } return apply_filters( 'woocommerce_product_import_get_product_object', $product, $data ); }