WC_Admin_Importers::post_importer_compatibilitypublicWC 1.0

When running the WP XML importer, ensure attributes exist.

WordPress import should work - however, it fails to import custom product attribute taxonomies. This code grabs the file before it is imported and ensures the taxonomies are created.

Метод класса: WC_Admin_Importers{}

Возвращает

null. Ничего (null).

Использование

$WC_Admin_Importers = new WC_Admin_Importers();
$WC_Admin_Importers->post_importer_compatibility();

Код WC_Admin_Importers::post_importer_compatibility() WC 10.4.3

public function post_importer_compatibility() {
	wc_deprecated_function( 'post_importer_compatibility', '10.1.0', 'A new integration with the WP WXR importer now filters the posts during import and registers the taxonomies, instead of initializing them at the start of the import and having to re-parse the file.' );

	// phpcs:ignore WordPress.Security.NonceVerification.Missing
	if ( empty( $_POST['import_id'] ) || ! class_exists( 'WXR_Parser' ) ) {
		return;
	}

	// phpcs:ignore WordPress.Security.NonceVerification.Missing
	$id          = absint( $_POST['import_id'] );
	$file        = get_attached_file( $id );
	$parser      = new WXR_Parser();
	$import_data = $parser->parse( $file );

	if ( isset( $import_data['posts'] ) && ! empty( $import_data['posts'] ) ) {
		foreach ( $import_data['posts'] as $post ) {
			if ( 'product' === $post['post_type'] && ! empty( $post['terms'] ) ) {
				foreach ( $post['terms'] as $term ) {
					if ( strstr( $term['domain'], 'pa_' ) ) {
						if ( ! taxonomy_exists( $term['domain'] ) ) {
							$attribute_name = wc_attribute_taxonomy_slug( $term['domain'] );

							// Create the taxonomy.
							if ( ! in_array( $attribute_name, wc_get_attribute_taxonomies(), true ) ) {
								wc_create_attribute(
									array(
										'name'         => $attribute_name,
										'slug'         => $attribute_name,
										'type'         => 'select',
										'order_by'     => 'menu_order',
										'has_archives' => false,
									)
								);
							}

							// Register the taxonomy now so that the import works!
							register_taxonomy(
								$term['domain'],
								// phpcs:ignore
								apply_filters( 'woocommerce_taxonomy_objects_' . $term['domain'], array( 'product' ) ),
								// phpcs:ignore
								apply_filters(
									'woocommerce_taxonomy_args_' . $term['domain'],
									array(
										'hierarchical' => true,
										'show_ui'      => false,
										'query_var'    => true,
										'rewrite'      => false,
									)
								)
							);
						}
					}
				}
			}
		}
	}
}