WC_Admin_Importers

WC_Admin_Importers Class.

Defined (1)

The class is defined in the following location(s).

/includes/admin/class-wc-admin-importers.php  
  1. class WC_Admin_Importers { 
  2.  
  3. /** 
  4. * Hook in tabs. 
  5. */ 
  6. public function __construct() { 
  7. add_action( 'admin_init', array( $this, 'register_importers' ) ); 
  8. add_action( 'import_start', array( $this, 'post_importer_compatibility' ) ); 
  9.  
  10. /** 
  11. * Add menu items. 
  12. */ 
  13. public function register_importers() { 
  14. register_importer( 'woocommerce_tax_rate_csv', __( 'WooCommerce Tax Rates (CSV)', 'woocommerce' ), __( 'Import <strong>tax rates</strong> to your store via a csv file.', 'woocommerce'), array( $this, 'tax_rates_importer' ) ); 
  15.  
  16. /** 
  17. * Add menu item. 
  18. */ 
  19. public function tax_rates_importer() { 
  20. // Load Importer API 
  21. require_once ABSPATH . 'wp-admin/includes/import.php'; 
  22.  
  23. if ( ! class_exists( 'WP_Importer' ) ) { 
  24. $class_wp_importer = ABSPATH . 'wp-admin/includes/class-wp-importer.php'; 
  25.  
  26. if ( file_exists( $class_wp_importer ) ) { 
  27. require $class_wp_importer; 
  28.  
  29. // includes 
  30. require 'importers/class-wc-tax-rate-importer.php'; 
  31.  
  32. // Dispatch 
  33. $importer = new WC_Tax_Rate_Importer(); 
  34. $importer->dispatch(); 
  35.  
  36. /** 
  37. * When running the WP importer, ensure attributes exist. 
  38. * WordPress import should work - however, it fails to import custom product attribute taxonomies. 
  39. * This code grabs the file before it is imported and ensures the taxonomies are created. 
  40. */ 
  41. public function post_importer_compatibility() { 
  42. global $wpdb; 
  43.  
  44. if ( empty( $_POST['import_id'] ) || ! class_exists( 'WXR_Parser' ) ) { 
  45. return; 
  46.  
  47. $id = absint( $_POST['import_id'] ); 
  48. $file = get_attached_file( $id ); 
  49. $parser = new WXR_Parser(); 
  50. $import_data = $parser->parse( $file ); 
  51.  
  52. if ( isset( $import_data['posts'] ) ) { 
  53. $posts = $import_data['posts']; 
  54.  
  55. if ( $posts && sizeof( $posts ) > 0 ) { 
  56. foreach ( $posts as $post ) { 
  57. if ( 'product' === $post['post_type'] ) { 
  58. if ( ! empty( $post['terms'] ) ) { 
  59. foreach ( $post['terms'] as $term ) { 
  60. if ( strstr( $term['domain'], 'pa_' ) ) { 
  61. if ( ! taxonomy_exists( $term['domain'] ) ) { 
  62. $attribute_name = wc_sanitize_taxonomy_name( str_replace( 'pa_', '', $term['domain'] ) ); 
  63.  
  64. // Create the taxonomy 
  65. if ( ! in_array( $attribute_name, wc_get_attribute_taxonomies() ) ) { 
  66. $attribute = array( 
  67. 'attribute_label' => $attribute_name,  
  68. 'attribute_name' => $attribute_name,  
  69. 'attribute_type' => 'select',  
  70. 'attribute_orderby' => 'menu_order',  
  71. 'attribute_public' => 0 
  72. ); 
  73. $wpdb->insert( $wpdb->prefix . 'woocommerce_attribute_taxonomies', $attribute ); 
  74. delete_transient( 'wc_attribute_taxonomies' ); 
  75.  
  76. // Register the taxonomy now so that the import works! 
  77. register_taxonomy( 
  78. $term['domain'],  
  79. apply_filters( 'woocommerce_taxonomy_objects_' . $term['domain'], array( 'product' ) ),  
  80. apply_filters( 'woocommerce_taxonomy_args_' . $term['domain'], array( 
  81. 'hierarchical' => true,  
  82. 'show_ui' => false,  
  83. 'query_var' => true,  
  84. 'rewrite' => false,  
  85. ) ) 
  86. );