WC_GZD_Admin_Importer

The WooCommerce Germanized WC GZD Admin Importer class.

Defined (1)

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

/includes/admin/class-wc-gzd-admin-importer.php  
  1. class WC_GZD_Admin_Importer { 
  2.  
  3. /** 
  4. * Single instance of WC_GZD_Importer 
  5. * @var object 
  6. */ 
  7. protected static $_instance = null; 
  8.  
  9. public $enabled = false; 
  10.  
  11. public $taxonomies = array( 
  12. 'product_delivery_time' => 'product_delivery_times',  
  13. 'product_unit' => 'pa_masseinheit',  
  14. 'product_price_label' => 'product_sale_labels',  
  15. ); 
  16.  
  17. public static function instance() { 
  18. if ( is_null( self::$_instance ) ) { 
  19. self::$_instance = new self(); 
  20. return self::$_instance; 
  21.  
  22. /** 
  23. * Cloning is forbidden. 
  24. * @since 1.0 
  25. */ 
  26. public function __clone() { 
  27. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized-pro' ), '1.0' ); 
  28.  
  29. /** 
  30. * Unserializing instances of this class is forbidden. 
  31. * @since 1.0 
  32. */ 
  33. public function __wakeup() { 
  34. _doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'woocommerce-germanized-pro' ), '1.0' ); 
  35.  
  36. public function __construct() { 
  37.  
  38. // Check for previous installs 
  39. if ( ! $this->is_available() ) 
  40. return; 
  41.  
  42. add_action( 'admin_init', array( $this, 'init' ) ); 
  43.  
  44.  
  45. public function is_available() { 
  46. return ( get_option( 'wgm_upgrade_24' ) && ! get_option( '_wc_gzd_import_finished' ) ? true : false ); 
  47.  
  48. public function init() { 
  49.  
  50. if ( isset( $_GET[ 'import' ] ) && check_admin_referer( 'wc-gzd-import', 'nonce' ) && current_user_can( 'edit_products' ) ) { 
  51. $this->import(); 
  52. } elseif ( isset( $_GET[ 'skip-import' ] ) && check_admin_referer( 'wc-gzd-skip-import', 'nonce' ) ) { 
  53. delete_option( '_wc_gzd_import_available' ); 
  54. update_option( '_wc_gzd_import_finished', 1 ); 
  55. wp_safe_redirect( remove_query_arg( array( 'nonce', 'skip-import' ) ) ); 
  56.  
  57.  
  58. private function import() { 
  59.  
  60. // Import legal pages 
  61. $this->import_pages(); 
  62.  
  63. // Import some settings 
  64. $this->import_settings(); 
  65.  
  66. // Import delivery time 
  67. $this->import_product_data(); 
  68.  
  69. // Import default delivery time/sale price label options 
  70. $this->import_defaults(); 
  71.  
  72. // Finished 
  73. delete_option( '_wc_gzd_import_available' ); 
  74. update_option( '_wc_gzd_import_finished', 1 ); 
  75.  
  76. // Save redirect 
  77. wp_safe_redirect( remove_query_arg( array( 'nonce', 'import' ) ) ); 
  78.  
  79. private function import_defaults() { 
  80.  
  81. $defaults = array( 
  82. 'product_delivery_time' => array( 
  83. 'org_option' => 'woocommerce_global_lieferzeit',  
  84. 'option' => 'woocommerce_gzd_default_delivery_time',  
  85. 'type' => 'id',  
  86. ),  
  87. 'product_price_label' => array( 
  88. 'org_option' => 'woocommerce_global_sale_label',  
  89. 'option' => 'woocommerce_gzd_default_sale_price_label',  
  90. 'type' => 'slug',  
  91. ),  
  92. ); 
  93.  
  94. foreach ( $defaults as $taxonomy => $options ) { 
  95.  
  96. $default = get_option( $options[ 'org_option' ] ); 
  97.  
  98. if ( ! empty( $default ) ) { 
  99.  
  100. $term = get_term_by( 'id', $default, $this->taxonomies[ $taxonomy ] ); 
  101.  
  102. if ( ! empty( $term ) && ! is_wp_error( $term ) ) { 
  103.  
  104. $gzd_term = $this->insert_term_if_necessary( $term, $taxonomy ); 
  105.  
  106. if ( ! empty( $gzd_term ) && ! is_wp_error( $gzd_term ) ) { 
  107. update_option( $options[ 'option' ], $options[ 'type' ] === 'slug' ? $gzd_term->slug : $gzd_term->term_id ); 
  108.  
  109. private function import_settings() { 
  110.  
  111. $settings = array( 
  112. 'woocommerce_widerrufsadressdaten' => 'woocommerce_gzd_revocation_address',  
  113. ); 
  114.  
  115. $settings_on_off = array( 
  116. 'woocommerce_de_show_delivery_time_overview' => 'woocommerce_gzd_display_listings_delivery_time',  
  117. 'woocommerce_de_show_price_per_unit' => 'woocommerce_gzd_display_listings_unit_price',  
  118. 'wgm_use_split_tax' => 'woocommerce_gzd_shipping_tax',  
  119. 'wgm_use_split_tax' => 'woocommerce_gzd_fee_tax',  
  120. ); 
  121.  
  122. // Small Business 
  123. if ( 'on' === get_option( 'woocommerce_de_kleinunternehmerregelung' ) ) { 
  124. update_option( 'woocommerce_gzd_small_enterprise', 'yes' ); 
  125. update_option( 'woocommerce_gzd_display_product_detail_small_enterprise', 'yes' ); 
  126.  
  127. // Update 1:1 
  128. foreach ( $settings as $old => $new ) { 
  129. if ( get_option( $old ) ) 
  130. update_option( $new, get_option( $old ) ); 
  131.  
  132. // Update on off 
  133. foreach ( $settings_on_off as $old => $new ) { 
  134. update_option( $new, ( 'on' === get_option( $old ) ? 'yes' : 'no' ) ); 
  135.  
  136.  
  137. private function import_pages() { 
  138.  
  139. $pages = array( 
  140. 'woocommerce_widerruf_page_id' => 'woocommerce_revocation_page_id',  
  141. 'woocommerce_impressum_page_id' => 'woocommerce_imprint_page_id',  
  142. 'woocommerce_datenschutz_page_id' => 'woocommerce_data_security_page_id',  
  143. 'woocommerce_zahlungsarten_page_id' => 'woocommerce_payment_methods_page_id',  
  144. 'woocommerce_versandkosten__lieferung_page_id' => 'woocommerce_shipping_costs_page_id',  
  145. ); 
  146.  
  147. foreach ( $pages as $old => $new ) { 
  148.  
  149. if ( get_option( $old ) ) 
  150. update_option( $new, get_option( $old ) ); 
  151.  
  152.  
  153.  
  154. private function import_single_product_data( $product ) { 
  155.  
  156. $save = array( 
  157. 'product-type' => $product->get_type(),  
  158. '_unit_price_sale' => '',  
  159. '_unit_price_regular' => '',  
  160. ); 
  161.  
  162. // Price per unit 
  163. if ( get_post_meta( $product->id, '_regular_price_per_unit', true ) ) { 
  164.  
  165. $regular = get_post_meta( $product->id, '_regular_price_per_unit', true ); 
  166. $base = get_post_meta( $product->id, '_unit_regular_price_per_unit_mult', true ); 
  167. $sale = get_post_meta( $product->id, '_sale_price_per_unit', true ); 
  168. $unit = get_post_meta( $product->id, '_unit_regular_price_per_unit', true ); 
  169.  
  170. if ( $unit ) { 
  171.  
  172. $unit_term = get_term_by( 'slug', $unit, $this->taxonomies[ 'product_unit' ] ); 
  173.  
  174. if ( $unit_term && ! is_wp_error( $unit_term ) ) { 
  175.  
  176. $gzd_term = $this->insert_term_if_necessary( $unit_term, 'product_unit' ); 
  177.  
  178. if ( $gzd_term && ! is_wp_error( $gzd_term ) ) { 
  179.  
  180. $save['_unit'] = $gzd_term->slug; 
  181. $save['_unit_base'] = $base; 
  182. $save['_unit_price_regular'] = $regular; 
  183. $save['_unit_price_sale'] = $sale; 
  184. $save['_sale_price'] = $product->get_sale_price(); 
  185. $save['_sale_price_dates_from'] = ( get_post_meta( $product->id, '_sale_price_dates_from', true ) ? get_post_meta( $product->id, '_sale_price_dates_from', true ) : '' ); 
  186. $save['_sale_price_dates_to'] = ( get_post_meta( $product->id, '_sale_price_dates_to', true ) ? get_post_meta( $product->id, '_sale_price_dates_to', true ) : '' ); 
  187.  
  188.  
  189.  
  190.  
  191.  
  192. // Labels 
  193. if ( get_post_meta( $product->id, '_sale_label', true ) ) { 
  194.  
  195. $term_id = get_post_meta( $product->id, '_sale_label', true ); 
  196. $term = get_term_by( 'id', absint( $term_id ), $this->taxonomies[ 'product_price_label' ] ); 
  197.  
  198. if ( ! empty( $term ) && ! is_wp_error( $term ) ) { 
  199. $gzd_term = $this->insert_term_if_necessary( $term, 'product_price_label' ); 
  200. $save['_sale_price_label'] = $term->slug; 
  201.  
  202.  
  203. // Delivery time (if does not exist will be added automatically) 
  204. if ( $delivery_time = get_post_meta( $product->id, '_lieferzeit', true ) ) { 
  205.  
  206. $term = get_term_by( 'id', $delivery_time, $this->taxonomies[ 'product_delivery_time' ] ); 
  207.  
  208. if ( $term && ! is_wp_error( $term ) ) 
  209. $save['delivery_time'] = $term->name; 
  210.  
  211.  
  212. // Free shipping 
  213. if ( 'on' === get_post_meta( $product->id, '_suppress_shipping_notice', true ) && ! $product->is_type( 'variation' ) ) { 
  214. $save['_free_shipping'] = 'yes'; 
  215.  
  216. // Save 
  217. if ( sizeof( $save ) > 3 ) { 
  218. WC_Germanized_Meta_Box_Product_Data::save_product_data( $product->id, $save, ( $product->is_type( 'variation' ) ) ); 
  219.  
  220.  
  221. private function insert_term_if_necessary( $term, $taxonomy ) { 
  222.  
  223. $gzd_term = false; 
  224.  
  225. if ( ! $gzd_term = get_term_by( 'slug', $term->slug, $taxonomy ) ) { 
  226.  
  227. $term_data = wp_insert_term( $term->name, $taxonomy, array( 'description' => $term->description ) ); 
  228.  
  229. if ( ! empty( $term_data ) && ! is_wp_error( $term_data ) ) { 
  230. $gzd_term = get_term_by( 'id', $term_data[ 'term_id' ], $taxonomy ); 
  231.  
  232. return $gzd_term; 
  233.  
  234.  
  235. private function import_product_data() { 
  236.  
  237. // Temporarily add taxonomy if doesnt exist 
  238. foreach ( $this->taxonomies as $taxonomy ) { 
  239.  
  240. if ( ! taxonomy_exists( $taxonomy ) ) 
  241. register_taxonomy( $taxonomy, array( 'product' ) ); 
  242.  
  243.  
  244. // First get products 
  245. $posts = get_posts( array(  
  246. 'post_type' => 'product',  
  247. 'posts_per_page' => -1,  
  248. 'fields' => 'ids',  
  249. 'post_status' => array( 'publish', 'draft', 'private' ),  
  250. ) ); 
  251.  
  252. if ( ! empty( $posts ) ) { 
  253.  
  254. foreach ( $posts as $post_id ) { 
  255.  
  256. $product = wc_get_product( $post_id ); 
  257.  
  258. $this->import_single_product_data( $product ); 
  259.  
  260. if ( $product->is_type( 'variable' ) ) { 
  261.  
  262. $variations = $product->get_children(); 
  263.  
  264. if ( ! empty( $variations ) ) { 
  265.  
  266. foreach ( $variations as $variation_id ) { 
  267.  
  268. $variation = wc_get_product( $variation_id ); 
  269. $this->import_single_product_data( $variation ); 
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.