/includes/wc-update-functions.php

  1. <?php 
  2. /** 
  3. * WooCommerce Updates 
  4. * 
  5. * Functions for updating data, used by the background updater. 
  6. * 
  7. * @author WooThemes 
  8. * @category Core 
  9. * @package WooCommerce/Functions 
  10. * @version 2.6.0 
  11. */ 
  12.  
  13. if ( ! defined( 'ABSPATH' ) ) { 
  14. exit; 
  15.  
  16. function wc_update_200_file_paths() { 
  17. global $wpdb; 
  18.  
  19. // Upgrade old style files paths to support multiple file paths 
  20. $existing_file_paths = $wpdb->get_results( "SELECT meta_value, meta_id, post_id FROM {$wpdb->postmeta} WHERE meta_key = '_file_path' AND meta_value != '';" ); 
  21.  
  22. if ( $existing_file_paths ) { 
  23.  
  24. foreach ( $existing_file_paths as $existing_file_path ) { 
  25.  
  26. $old_file_path = trim( $existing_file_path->meta_value ); 
  27.  
  28. if ( ! empty( $old_file_path ) ) { 
  29. $file_paths = serialize( array( md5( $old_file_path ) => $old_file_path ) ); 
  30.  
  31. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_key = '_file_paths', meta_value = %s WHERE meta_id = %d", $file_paths, $existing_file_path->meta_id ) ); 
  32.  
  33. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->prefix}woocommerce_downloadable_product_permissions SET download_id = %s WHERE product_id = %d", md5( $old_file_path ), $existing_file_path->post_id ) ); 
  34.  
  35.  
  36. function wc_update_200_permalinks() { 
  37. // Setup default permalinks if shop page is defined 
  38. $permalinks = get_option( 'woocommerce_permalinks' ); 
  39. $shop_page_id = wc_get_page_id( 'shop' ); 
  40.  
  41. if ( empty( $permalinks ) && $shop_page_id > 0 ) { 
  42.  
  43. $base_slug = $shop_page_id > 0 && get_post( $shop_page_id ) ? get_page_uri( $shop_page_id ) : 'shop'; 
  44.  
  45. $category_base = get_option( 'woocommerce_prepend_shop_page_to_urls' ) == "yes" ? trailingslashit( $base_slug ) : ''; 
  46. $category_slug = get_option( 'woocommerce_product_category_slug' ) ? get_option( 'woocommerce_product_category_slug' ) : _x( 'product-category', 'slug', 'woocommerce' ); 
  47. $tag_slug = get_option( 'woocommerce_product_tag_slug' ) ? get_option( 'woocommerce_product_tag_slug' ) : _x( 'product-tag', 'slug', 'woocommerce' ); 
  48.  
  49. if ( 'yes' == get_option( 'woocommerce_prepend_shop_page_to_products' ) ) { 
  50. $product_base = trailingslashit( $base_slug ); 
  51. } else { 
  52. if ( ( $product_slug = get_option( 'woocommerce_product_slug' ) ) !== false && ! empty( $product_slug ) ) { 
  53. $product_base = trailingslashit( $product_slug ); 
  54. } else { 
  55. $product_base = trailingslashit( _x( 'product', 'slug', 'woocommerce' ) ); 
  56.  
  57. if ( get_option( 'woocommerce_prepend_category_to_products' ) == 'yes' ) 
  58. $product_base .= trailingslashit( '%product_cat%' ); 
  59.  
  60. $permalinks = array( 
  61. 'product_base' => untrailingslashit( $product_base ),  
  62. 'category_base' => untrailingslashit( $category_base . $category_slug ),  
  63. 'attribute_base' => untrailingslashit( $category_base ),  
  64. 'tag_base' => untrailingslashit( $category_base . $tag_slug ),  
  65. ); 
  66.  
  67. update_option( 'woocommerce_permalinks', $permalinks ); 
  68.  
  69. function wc_update_200_subcat_display() { 
  70. // Update subcat display settings 
  71. if ( get_option( 'woocommerce_shop_show_subcategories' ) == 'yes' ) { 
  72. if ( get_option( 'woocommerce_hide_products_when_showing_subcategories' ) == 'yes' ) { 
  73. update_option( 'woocommerce_shop_page_display', 'subcategories' ); 
  74. } else { 
  75. update_option( 'woocommerce_shop_page_display', 'both' ); 
  76.  
  77. if ( get_option( 'woocommerce_show_subcategories' ) == 'yes' ) { 
  78. if ( get_option( 'woocommerce_hide_products_when_showing_subcategories' ) == 'yes' ) { 
  79. update_option( 'woocommerce_category_archive_display', 'subcategories' ); 
  80. } else { 
  81. update_option( 'woocommerce_category_archive_display', 'both' ); 
  82.  
  83. function wc_update_200_taxrates() { 
  84. global $wpdb; 
  85.  
  86. // Update tax rates 
  87. $loop = 0; 
  88. $tax_rates = get_option( 'woocommerce_tax_rates' ); 
  89.  
  90. if ( $tax_rates ) 
  91. foreach ( $tax_rates as $tax_rate ) { 
  92.  
  93. foreach ( $tax_rate['countries'] as $country => $states ) { 
  94.  
  95. $states = array_reverse( $states ); 
  96.  
  97. foreach ( $states as $state ) { 
  98.  
  99. if ( '*' == $state ) { 
  100. $state = ''; 
  101.  
  102. $wpdb->insert( 
  103. $wpdb->prefix . "woocommerce_tax_rates",  
  104. array( 
  105. 'tax_rate_country' => $country,  
  106. 'tax_rate_state' => $state,  
  107. 'tax_rate' => $tax_rate['rate'],  
  108. 'tax_rate_name' => $tax_rate['label'],  
  109. 'tax_rate_priority' => 1,  
  110. 'tax_rate_compound' => ( 'yes' === $tax_rate['compound'] ) ? 1 : 0,  
  111. 'tax_rate_shipping' => ( 'yes' === $tax_rate['shipping'] ) ? 1 : 0,  
  112. 'tax_rate_order' => $loop,  
  113. 'tax_rate_class' => $tax_rate['class'],  
  114. ); 
  115.  
  116. $loop++; 
  117.  
  118. $local_tax_rates = get_option( 'woocommerce_local_tax_rates' ); 
  119.  
  120. if ( $local_tax_rates ) 
  121. foreach ( $local_tax_rates as $tax_rate ) { 
  122.  
  123. $location_type = ( 'postcode' === $tax_rate['location_type'] ) ? 'postcode' : 'city'; 
  124.  
  125. if ( '*' == $tax_rate['state'] ) { 
  126. $tax_rate['state'] = ''; 
  127.  
  128. $wpdb->insert( 
  129. $wpdb->prefix . "woocommerce_tax_rates",  
  130. array( 
  131. 'tax_rate_country' => $tax_rate['country'],  
  132. 'tax_rate_state' => $tax_rate['state'],  
  133. 'tax_rate' => $tax_rate['rate'],  
  134. 'tax_rate_name' => $tax_rate['label'],  
  135. 'tax_rate_priority' => 2,  
  136. 'tax_rate_compound' => ( 'yes' === $tax_rate['compound'] ) ? 1 : 0,  
  137. 'tax_rate_shipping' => ( 'yes' === $tax_rate['shipping'] ) ? 1 : 0,  
  138. 'tax_rate_order' => $loop,  
  139. 'tax_rate_class' => $tax_rate['class'],  
  140. ); 
  141.  
  142. $tax_rate_id = $wpdb->insert_id; 
  143.  
  144. if ( $tax_rate['locations'] ) { 
  145. foreach ( $tax_rate['locations'] as $location ) { 
  146.  
  147. $wpdb->insert( 
  148. $wpdb->prefix . "woocommerce_tax_rate_locations",  
  149. array( 
  150. 'location_code' => $location,  
  151. 'tax_rate_id' => $tax_rate_id,  
  152. 'location_type' => $location_type,  
  153. ); 
  154.  
  155.  
  156. $loop++; 
  157.  
  158. update_option( 'woocommerce_tax_rates_backup', $tax_rates ); 
  159. update_option( 'woocommerce_local_tax_rates_backup', $local_tax_rates ); 
  160. delete_option( 'woocommerce_tax_rates' ); 
  161. delete_option( 'woocommerce_local_tax_rates' ); 
  162.  
  163. function wc_update_200_line_items() { 
  164. global $wpdb; 
  165.  
  166. // Now its time for the massive update to line items - move them to the new DB tables 
  167. // Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_items' WHERE meta_key = '_order_items_old' 
  168. $order_item_rows = $wpdb->get_results( " 
  169. SELECT meta_value, post_id FROM {$wpdb->postmeta} 
  170. WHERE meta_key = '_order_items' 
  171. " ); 
  172.  
  173. foreach ( $order_item_rows as $order_item_row ) { 
  174.  
  175. $order_items = (array) maybe_unserialize( $order_item_row->meta_value ); 
  176.  
  177. foreach ( $order_items as $order_item ) { 
  178.  
  179. if ( ! isset( $order_item['line_total'] ) && isset( $order_item['taxrate'] ) && isset( $order_item['cost'] ) ) { 
  180. $order_item['line_tax'] = number_format( ( $order_item['cost'] * $order_item['qty'] ) * ( $order_item['taxrate'] / 100 ), 2, '.', '' ); 
  181. $order_item['line_total'] = $order_item['cost'] * $order_item['qty']; 
  182. $order_item['line_subtotal_tax'] = $order_item['line_tax']; 
  183. $order_item['line_subtotal'] = $order_item['line_total']; 
  184.  
  185. $order_item['line_tax'] = isset( $order_item['line_tax'] ) ? $order_item['line_tax'] : 0; 
  186. $order_item['line_total'] = isset( $order_item['line_total'] ) ? $order_item['line_total'] : 0; 
  187. $order_item['line_subtotal_tax'] = isset( $order_item['line_subtotal_tax'] ) ? $order_item['line_subtotal_tax'] : 0; 
  188. $order_item['line_subtotal'] = isset( $order_item['line_subtotal'] ) ? $order_item['line_subtotal'] : 0; 
  189.  
  190. $item_id = wc_add_order_item( $order_item_row->post_id, array( 
  191. 'order_item_name' => $order_item['name'],  
  192. 'order_item_type' => 'line_item',  
  193. ) ); 
  194.  
  195. // Add line item meta 
  196. if ( $item_id ) { 
  197. wc_add_order_item_meta( $item_id, '_qty', absint( $order_item['qty'] ) ); 
  198. wc_add_order_item_meta( $item_id, '_tax_class', $order_item['tax_class'] ); 
  199. wc_add_order_item_meta( $item_id, '_product_id', $order_item['id'] ); 
  200. wc_add_order_item_meta( $item_id, '_variation_id', $order_item['variation_id'] ); 
  201. wc_add_order_item_meta( $item_id, '_line_subtotal', wc_format_decimal( $order_item['line_subtotal'] ) ); 
  202. wc_add_order_item_meta( $item_id, '_line_subtotal_tax', wc_format_decimal( $order_item['line_subtotal_tax'] ) ); 
  203. wc_add_order_item_meta( $item_id, '_line_total', wc_format_decimal( $order_item['line_total'] ) ); 
  204. wc_add_order_item_meta( $item_id, '_line_tax', wc_format_decimal( $order_item['line_tax'] ) ); 
  205.  
  206. $meta_rows = array(); 
  207.  
  208. // Insert meta 
  209. if ( ! empty( $order_item['item_meta'] ) ) { 
  210. foreach ( $order_item['item_meta'] as $key => $meta ) { 
  211. // Backwards compatibility 
  212. if ( is_array( $meta ) && isset( $meta['meta_name'] ) ) { 
  213. $meta_rows[] = '(' . $item_id . ', "' . esc_sql( $meta['meta_name'] ) . '", "' . esc_sql( $meta['meta_value'] ) . '")'; 
  214. } else { 
  215. $meta_rows[] = '(' . $item_id . ', "' . esc_sql( $key ) . '", "' . esc_sql( $meta ) . '")'; 
  216.  
  217. // Insert meta rows at once 
  218. if ( sizeof( $meta_rows ) > 0 ) { 
  219. $wpdb->query( $wpdb->prepare( " 
  220. INSERT INTO {$wpdb->prefix}woocommerce_order_itemmeta ( order_item_id, meta_key, meta_value ) 
  221. VALUES " . implode( ', ', $meta_rows ) . "; 
  222. ", $order_item_row->post_id ) ); 
  223.  
  224. // Delete from DB (rename) 
  225. $wpdb->query( $wpdb->prepare( " 
  226. UPDATE {$wpdb->postmeta} 
  227. SET meta_key = '_order_items_old' 
  228. WHERE meta_key = '_order_items' 
  229. AND post_id = %d 
  230. ", $order_item_row->post_id ) ); 
  231.  
  232. unset( $meta_rows, $item_id, $order_item ); 
  233.  
  234. // Do the same kind of update for order_taxes - move to lines 
  235. // Reverse with UPDATE `wpwc_postmeta` SET meta_key = '_order_taxes' WHERE meta_key = '_order_taxes_old' 
  236. $order_tax_rows = $wpdb->get_results( " 
  237. SELECT meta_value, post_id FROM {$wpdb->postmeta} 
  238. WHERE meta_key = '_order_taxes' 
  239. " ); 
  240.  
  241. foreach ( $order_tax_rows as $order_tax_row ) { 
  242.  
  243. $order_taxes = (array) maybe_unserialize( $order_tax_row->meta_value ); 
  244.  
  245. if ( ! empty( $order_taxes ) ) { 
  246. foreach ( $order_taxes as $order_tax ) { 
  247.  
  248. if ( ! isset( $order_tax['label'] ) || ! isset( $order_tax['cart_tax'] ) || ! isset( $order_tax['shipping_tax'] ) ) 
  249. continue; 
  250.  
  251. $item_id = wc_add_order_item( $order_tax_row->post_id, array( 
  252. 'order_item_name' => $order_tax['label'],  
  253. 'order_item_type' => 'tax',  
  254. ) ); 
  255.  
  256. // Add line item meta 
  257. if ( $item_id ) { 
  258. wc_add_order_item_meta( $item_id, 'compound', absint( isset( $order_tax['compound'] ) ? $order_tax['compound'] : 0 ) ); 
  259. wc_add_order_item_meta( $item_id, 'tax_amount', wc_clean( $order_tax['cart_tax'] ) ); 
  260. wc_add_order_item_meta( $item_id, 'shipping_tax_amount', wc_clean( $order_tax['shipping_tax'] ) ); 
  261.  
  262. // Delete from DB (rename) 
  263. $wpdb->query( $wpdb->prepare( " 
  264. UPDATE {$wpdb->postmeta} 
  265. SET meta_key = '_order_taxes_old' 
  266. WHERE meta_key = '_order_taxes' 
  267. AND post_id = %d 
  268. ", $order_tax_row->post_id ) ); 
  269.  
  270. unset( $tax_amount ); 
  271.  
  272. function wc_update_200_images() { 
  273. // Grab the pre 2.0 Image options and use to populate the new image options settings,  
  274. // cleaning up afterwards like nice people do 
  275. foreach ( array( 'catalog', 'single', 'thumbnail' ) as $value ) { 
  276.  
  277. $old_settings = array_filter( array( 
  278. 'width' => get_option( 'woocommerce_' . $value . '_image_width' ),  
  279. 'height' => get_option( 'woocommerce_' . $value . '_image_height' ),  
  280. 'crop' => get_option( 'woocommerce_' . $value . '_image_crop' ),  
  281. ) ); 
  282.  
  283. if ( ! empty( $old_settings ) && update_option( 'shop_' . $value . '_image_size', $old_settings ) ) { 
  284.  
  285. delete_option( 'woocommerce_' . $value . '_image_width' ); 
  286. delete_option( 'woocommerce_' . $value . '_image_height' ); 
  287. delete_option( 'woocommerce_' . $value . '_image_crop' ); 
  288.  
  289.  
  290. function wc_update_200_db_version() { 
  291. WC_Install::update_db_version( '2.0.0' ); 
  292.  
  293. function wc_update_209_brazillian_state() { 
  294. global $wpdb; 
  295.  
  296. // Update brazillian state codes 
  297. $wpdb->update( 
  298. $wpdb->postmeta,  
  299. array( 
  300. 'meta_value' => 'BA',  
  301. ),  
  302. array( 
  303. 'meta_key' => '_billing_state',  
  304. 'meta_value' => 'BH',  
  305. ); 
  306. $wpdb->update( 
  307. $wpdb->postmeta,  
  308. array( 
  309. 'meta_value' => 'BA',  
  310. ),  
  311. array( 
  312. 'meta_key' => '_shipping_state',  
  313. 'meta_value' => 'BH',  
  314. ); 
  315. $wpdb->update( 
  316. $wpdb->usermeta,  
  317. array( 
  318. 'meta_value' => 'BA',  
  319. ),  
  320. array( 
  321. 'meta_key' => 'billing_state',  
  322. 'meta_value' => 'BH',  
  323. ); 
  324. $wpdb->update( 
  325. $wpdb->usermeta,  
  326. array( 
  327. 'meta_value' => 'BA',  
  328. ),  
  329. array( 
  330. 'meta_key' => 'shipping_state',  
  331. 'meta_value' => 'BH',  
  332. ); 
  333.  
  334. function wc_update_209_db_version() { 
  335. WC_Install::update_db_version( '2.0.9' ); 
  336.  
  337. function wc_update_210_remove_pages() { 
  338. // Pages no longer used 
  339. wp_trash_post( get_option( 'woocommerce_pay_page_id' ) ); 
  340. wp_trash_post( get_option( 'woocommerce_thanks_page_id' ) ); 
  341. wp_trash_post( get_option( 'woocommerce_view_order_page_id' ) ); 
  342. wp_trash_post( get_option( 'woocommerce_change_password_page_id' ) ); 
  343. wp_trash_post( get_option( 'woocommerce_edit_address_page_id' ) ); 
  344. wp_trash_post( get_option( 'woocommerce_lost_password_page_id' ) ); 
  345.  
  346. function wc_update_210_file_paths() { 
  347. global $wpdb; 
  348.  
  349. // Upgrade file paths to support multiple file paths + names etc 
  350. $existing_file_paths = $wpdb->get_results( "SELECT meta_value, meta_id FROM {$wpdb->postmeta} WHERE meta_key = '_file_paths' AND meta_value != '';" ); 
  351.  
  352. if ( $existing_file_paths ) { 
  353.  
  354. foreach ( $existing_file_paths as $existing_file_path ) { 
  355.  
  356. $needs_update = false; 
  357. $new_value = array(); 
  358. $value = maybe_unserialize( trim( $existing_file_path->meta_value ) ); 
  359.  
  360. if ( $value ) { 
  361. foreach ( $value as $key => $file ) { 
  362. if ( ! is_array( $file ) ) { 
  363. $needs_update = true; 
  364. $new_value[ $key ] = array( 
  365. 'file' => $file,  
  366. 'name' => wc_get_filename_from_url( $file ),  
  367. ); 
  368. } else { 
  369. $new_value[ $key ] = $file; 
  370. if ( $needs_update ) { 
  371. $new_value = serialize( $new_value ); 
  372.  
  373. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_key = %s, meta_value = %s WHERE meta_id = %d", '_downloadable_files', $new_value, $existing_file_path->meta_id ) ); 
  374.  
  375. function wc_update_210_db_version() { 
  376. WC_Install::update_db_version( '2.1.0' ); 
  377.  
  378. function wc_update_220_shipping() { 
  379. $woocommerce_ship_to_destination = 'shipping'; 
  380.  
  381. if ( get_option( 'woocommerce_ship_to_billing_address_only' ) === 'yes' ) { 
  382. $woocommerce_ship_to_destination = 'billing_only'; 
  383. } elseif ( get_option( 'woocommerce_ship_to_billing' ) === 'yes' ) { 
  384. $woocommerce_ship_to_destination = 'billing'; 
  385.  
  386. add_option( 'woocommerce_ship_to_destination', $woocommerce_ship_to_destination, '', 'no' ); 
  387.  
  388. function wc_update_220_order_status() { 
  389. global $wpdb; 
  390. $wpdb->query( " 
  391. UPDATE {$wpdb->posts} as posts 
  392. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id 
  393. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id ) 
  394. LEFT JOIN {$wpdb->terms} AS term USING( term_id ) 
  395. SET posts.post_status = 'wc-pending' 
  396. WHERE posts.post_type = 'shop_order' 
  397. AND posts.post_status = 'publish' 
  398. AND tax.taxonomy = 'shop_order_status' 
  399. AND term.slug LIKE 'pending%'; 
  400. ); 
  401. $wpdb->query( " 
  402. UPDATE {$wpdb->posts} as posts 
  403. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id 
  404. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id ) 
  405. LEFT JOIN {$wpdb->terms} AS term USING( term_id ) 
  406. SET posts.post_status = 'wc-processing' 
  407. WHERE posts.post_type = 'shop_order' 
  408. AND posts.post_status = 'publish' 
  409. AND tax.taxonomy = 'shop_order_status' 
  410. AND term.slug LIKE 'processing%'; 
  411. ); 
  412. $wpdb->query( " 
  413. UPDATE {$wpdb->posts} as posts 
  414. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id 
  415. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id ) 
  416. LEFT JOIN {$wpdb->terms} AS term USING( term_id ) 
  417. SET posts.post_status = 'wc-on-hold' 
  418. WHERE posts.post_type = 'shop_order' 
  419. AND posts.post_status = 'publish' 
  420. AND tax.taxonomy = 'shop_order_status' 
  421. AND term.slug LIKE 'on-hold%'; 
  422. ); 
  423. $wpdb->query( " 
  424. UPDATE {$wpdb->posts} as posts 
  425. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id 
  426. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id ) 
  427. LEFT JOIN {$wpdb->terms} AS term USING( term_id ) 
  428. SET posts.post_status = 'wc-completed' 
  429. WHERE posts.post_type = 'shop_order' 
  430. AND posts.post_status = 'publish' 
  431. AND tax.taxonomy = 'shop_order_status' 
  432. AND term.slug LIKE 'completed%'; 
  433. ); 
  434. $wpdb->query( " 
  435. UPDATE {$wpdb->posts} as posts 
  436. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id 
  437. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id ) 
  438. LEFT JOIN {$wpdb->terms} AS term USING( term_id ) 
  439. SET posts.post_status = 'wc-cancelled' 
  440. WHERE posts.post_type = 'shop_order' 
  441. AND posts.post_status = 'publish' 
  442. AND tax.taxonomy = 'shop_order_status' 
  443. AND term.slug LIKE 'cancelled%'; 
  444. ); 
  445. $wpdb->query( " 
  446. UPDATE {$wpdb->posts} as posts 
  447. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id 
  448. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id ) 
  449. LEFT JOIN {$wpdb->terms} AS term USING( term_id ) 
  450. SET posts.post_status = 'wc-refunded' 
  451. WHERE posts.post_type = 'shop_order' 
  452. AND posts.post_status = 'publish' 
  453. AND tax.taxonomy = 'shop_order_status' 
  454. AND term.slug LIKE 'refunded%'; 
  455. ); 
  456. $wpdb->query( " 
  457. UPDATE {$wpdb->posts} as posts 
  458. LEFT JOIN {$wpdb->term_relationships} AS rel ON posts.ID = rel.object_id 
  459. LEFT JOIN {$wpdb->term_taxonomy} AS tax USING( term_taxonomy_id ) 
  460. LEFT JOIN {$wpdb->terms} AS term USING( term_id ) 
  461. SET posts.post_status = 'wc-failed' 
  462. WHERE posts.post_type = 'shop_order' 
  463. AND posts.post_status = 'publish' 
  464. AND tax.taxonomy = 'shop_order_status' 
  465. AND term.slug LIKE 'failed%'; 
  466. ); 
  467.  
  468. function wc_update_220_variations() { 
  469. global $wpdb; 
  470. // Update variations which manage stock 
  471. $update_variations = $wpdb->get_results( " 
  472. SELECT DISTINCT posts.ID AS variation_id, posts.post_parent AS variation_parent FROM {$wpdb->posts} as posts 
  473. LEFT OUTER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id AND postmeta.meta_key = '_stock' 
  474. LEFT OUTER JOIN {$wpdb->postmeta} as postmeta2 ON posts.ID = postmeta2.post_id AND postmeta2.meta_key = '_manage_stock' 
  475. WHERE posts.post_type = 'product_variation' 
  476. AND postmeta.meta_value IS NOT NULL 
  477. AND postmeta.meta_value != '' 
  478. AND postmeta2.meta_value IS NULL 
  479. " ); 
  480.  
  481. foreach ( $update_variations as $variation ) { 
  482. $parent_backorders = get_post_meta( $variation->variation_parent, '_backorders', true ); 
  483. add_post_meta( $variation->variation_id, '_manage_stock', 'yes', true ); 
  484. add_post_meta( $variation->variation_id, '_backorders', $parent_backorders ? $parent_backorders : 'no', true ); 
  485.  
  486. function wc_update_220_attributes() { 
  487. global $wpdb; 
  488. // Update taxonomy names with correct sanitized names 
  489. $attribute_taxonomies = $wpdb->get_results( "SELECT attribute_name, attribute_id FROM " . $wpdb->prefix . "woocommerce_attribute_taxonomies" ); 
  490.  
  491. foreach ( $attribute_taxonomies as $attribute_taxonomy ) { 
  492. $sanitized_attribute_name = wc_sanitize_taxonomy_name( $attribute_taxonomy->attribute_name ); 
  493. if ( $sanitized_attribute_name !== $attribute_taxonomy->attribute_name ) { 
  494. if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT 1=1 FROM {$wpdb->prefix}woocommerce_attribute_taxonomies WHERE attribute_name = %s;", $sanitized_attribute_name ) ) ) { 
  495. // Update attribute 
  496. $wpdb->update( 
  497. "{$wpdb->prefix}woocommerce_attribute_taxonomies",  
  498. array( 
  499. 'attribute_name' => $sanitized_attribute_name,  
  500. ),  
  501. array( 
  502. 'attribute_id' => $attribute_taxonomy->attribute_id,  
  503. ); 
  504.  
  505. // Update terms 
  506. $wpdb->update( 
  507. $wpdb->term_taxonomy,  
  508. array( 'taxonomy' => wc_attribute_taxonomy_name( $sanitized_attribute_name ) ),  
  509. array( 'taxonomy' => 'pa_' . $attribute_taxonomy->attribute_name ) 
  510. ); 
  511.  
  512. function wc_update_220_db_version() { 
  513. WC_Install::update_db_version( '2.2.0' ); 
  514.  
  515. function wc_update_230_options() { 
  516. // _money_spent and _order_count may be out of sync - clear them 
  517. delete_metadata( 'user', 0, '_money_spent', '', true ); 
  518. delete_metadata( 'user', 0, '_order_count', '', true ); 
  519.  
  520. // To prevent taxes being hidden when using a default 'no address' in a store with tax inc prices, set the woocommerce_default_customer_address to use the store base address by default 
  521. if ( '' === get_option( 'woocommerce_default_customer_address', false ) && wc_prices_include_tax() ) { 
  522. update_option( 'woocommerce_default_customer_address', 'base' ); 
  523.  
  524. function wc_update_230_db_version() { 
  525. WC_Install::update_db_version( '2.3.0' ); 
  526.  
  527. function wc_update_240_options() { 
  528. /** 
  529. * Coupon discount calculations. 
  530. * Maintain the old coupon logic for upgrades. 
  531. */ 
  532. update_option( 'woocommerce_calc_discounts_sequentially', 'yes' ); 
  533.  
  534. function wc_update_240_shipping_methods() { 
  535. global $wpdb; 
  536. /** 
  537. * Flat Rate Shipping. 
  538. * Update legacy options to new math based options. 
  539. */ 
  540. $shipping_methods = array( 
  541. 'woocommerce_flat_rates' => new WC_Shipping_Legacy_Flat_Rate(),  
  542. 'woocommerce_international_delivery_flat_rates' => new WC_Shipping_Legacy_International_Delivery(),  
  543. ); 
  544. foreach ( $shipping_methods as $flat_rate_option_key => $shipping_method ) { 
  545. // Stop this running more than once if routine is repeated 
  546. if ( version_compare( $shipping_method->get_option( 'version', 0 ), '2.4.0', '<' ) ) { 
  547. $has_classes = sizeof( WC()->shipping->get_shipping_classes() ) > 0; 
  548. $cost_key = $has_classes ? 'no_class_cost' : 'cost'; 
  549. $min_fee = $shipping_method->get_option( 'minimum_fee' ); 
  550. $math_cost_strings = array( 'cost' => array(), 'no_class_cost' => array() ); 
  551. $math_cost_strings[ $cost_key ][] = $shipping_method->get_option( 'cost' ); 
  552.  
  553. if ( $fee = $shipping_method->get_option( 'fee' ) ) { 
  554. $math_cost_strings[ $cost_key ][] = strstr( $fee, '%' ) ? '[fee percent="' . str_replace( '%', '', $fee ) . '" min="' . esc_attr( $min_fee ) . '"]' : $fee; 
  555.  
  556. foreach ( WC()->shipping->get_shipping_classes() as $shipping_class ) { 
  557. $rate_key = 'class_cost_' . $shipping_class->slug; 
  558. $math_cost_strings[ $rate_key ] = $math_cost_strings['no_class_cost']; 
  559.  
  560. if ( $flat_rates = array_filter( (array) get_option( $flat_rate_option_key, array() ) ) ) { 
  561. foreach ( $flat_rates as $shipping_class => $rate ) { 
  562. $rate_key = 'class_cost_' . $shipping_class; 
  563. if ( $rate['cost'] || $rate['fee'] ) { 
  564. $math_cost_strings[ $rate_key ][] = $rate['cost']; 
  565. $math_cost_strings[ $rate_key ][] = strstr( $rate['fee'], '%' ) ? '[fee percent="' . str_replace( '%', '', $rate['fee'] ) . '" min="' . esc_attr( $min_fee ) . '"]' : $rate['fee']; 
  566.  
  567. if ( 'item' === $shipping_method->type ) { 
  568. foreach ( $math_cost_strings as $key => $math_cost_string ) { 
  569. $math_cost_strings[ $key ] = array_filter( array_map( 'trim', $math_cost_strings[ $key ] ) ); 
  570. if ( ! empty( $math_cost_strings[ $key ] ) ) { 
  571. $last_key = max( 0, sizeof( $math_cost_strings[ $key ] ) - 1 ); 
  572. $math_cost_strings[ $key ][0] = '( ' . $math_cost_strings[ $key ][0]; 
  573. $math_cost_strings[ $key ][ $last_key ] .= ' ) * [qty]'; 
  574.  
  575. $math_cost_strings['cost'][] = $shipping_method->get_option( 'cost_per_order' ); 
  576.  
  577. // Save settings 
  578. foreach ( $math_cost_strings as $option_id => $math_cost_string ) { 
  579. $shipping_method->settings[ $option_id ] = implode( ' + ', array_filter( $math_cost_string ) ); 
  580.  
  581. $shipping_method->settings['version'] = '2.4.0'; 
  582. $shipping_method->settings['type'] = 'item' === $shipping_method->settings['type'] ? 'class' : $shipping_method->settings['type']; 
  583.  
  584. update_option( $shipping_method->plugin_id . $shipping_method->id . '_settings', $shipping_method->settings ); 
  585.  
  586. function wc_update_240_api_keys() { 
  587. global $wpdb; 
  588. /** 
  589. * Update the old user API keys to the new Apps keys. 
  590. */ 
  591. $api_users = $wpdb->get_results( "SELECT user_id FROM $wpdb->usermeta WHERE meta_key = 'woocommerce_api_consumer_key'" ); 
  592. $apps_keys = array(); 
  593.  
  594. // Get user data 
  595. foreach ( $api_users as $_user ) { 
  596. $user = get_userdata( $_user->user_id ); 
  597. $apps_keys[] = array( 
  598. 'user_id' => $user->ID,  
  599. 'permissions' => $user->woocommerce_api_key_permissions,  
  600. 'consumer_key' => wc_api_hash( $user->woocommerce_api_consumer_key ),  
  601. 'consumer_secret' => $user->woocommerce_api_consumer_secret,  
  602. 'truncated_key' => substr( $user->woocommerce_api_consumer_secret, -7 ),  
  603. ); 
  604.  
  605. if ( ! empty( $apps_keys ) ) { 
  606. // Create new apps 
  607. foreach ( $apps_keys as $app ) { 
  608. $wpdb->insert( 
  609. $wpdb->prefix . 'woocommerce_api_keys',  
  610. $app,  
  611. array( 
  612. '%d',  
  613. '%s',  
  614. '%s',  
  615. '%s',  
  616. '%s',  
  617. ); 
  618.  
  619. // Delete old user keys from usermeta 
  620. foreach ( $api_users as $_user ) { 
  621. $user_id = intval( $_user->user_id ); 
  622. delete_user_meta( $user_id, 'woocommerce_api_consumer_key' ); 
  623. delete_user_meta( $user_id, 'woocommerce_api_consumer_secret' ); 
  624. delete_user_meta( $user_id, 'woocommerce_api_key_permissions' ); 
  625.  
  626. function wc_update_240_webhooks() { 
  627. global $wpdb; 
  628. /** 
  629. * Webhooks. 
  630. * Make sure order.update webhooks get the woocommerce_order_edit_status hook. 
  631. */ 
  632. $order_update_webhooks = get_posts( array( 
  633. 'posts_per_page' => -1,  
  634. 'post_type' => 'shop_webhook',  
  635. 'meta_key' => '_topic',  
  636. 'meta_value' => 'order.updated',  
  637. ) ); 
  638. foreach ( $order_update_webhooks as $order_update_webhook ) { 
  639. $webhook = new WC_Webhook( $order_update_webhook->ID ); 
  640. $webhook->set_topic( 'order.updated' ); 
  641.  
  642. function wc_update_240_refunds() { 
  643. global $wpdb; 
  644. /** 
  645. * Refunds for full refunded orders. 
  646. * Update fully refunded orders to ensure they have a refund line item so reports add up. 
  647. */ 
  648. $refunded_orders = get_posts( array( 
  649. 'posts_per_page' => -1,  
  650. 'post_type' => 'shop_order',  
  651. 'post_status' => array( 'wc-refunded' ),  
  652. ) ); 
  653.  
  654. // Ensure emails are disabled during this update routine 
  655. remove_all_actions( 'woocommerce_order_status_refunded_notification' ); 
  656. remove_all_actions( 'woocommerce_order_partially_refunded_notification' ); 
  657. remove_action( 'woocommerce_order_status_refunded', array( 'WC_Emails', 'send_transactional_email' ) ); 
  658. remove_action( 'woocommerce_order_partially_refunded', array( 'WC_Emails', 'send_transactional_email' ) ); 
  659.  
  660. foreach ( $refunded_orders as $refunded_order ) { 
  661. $order_total = get_post_meta( $refunded_order->ID, '_order_total', true ); 
  662. $refunded_total = $wpdb->get_var( $wpdb->prepare( " 
  663. SELECT SUM( postmeta.meta_value ) 
  664. FROM $wpdb->postmeta AS postmeta 
  665. INNER JOIN $wpdb->posts AS posts ON ( posts.post_type = 'shop_order_refund' AND posts.post_parent = %d ) 
  666. WHERE postmeta.meta_key = '_refund_amount' 
  667. AND postmeta.post_id = posts.ID 
  668. ", $refunded_order->ID ) ); 
  669.  
  670. if ( $order_total > $refunded_total ) { 
  671. wc_create_refund( array( 
  672. 'amount' => $order_total - $refunded_total,  
  673. 'reason' => __( 'Order fully refunded', 'woocommerce' ),  
  674. 'order_id' => $refunded_order->ID,  
  675. 'line_items' => array(),  
  676. 'date' => $refunded_order->post_modified,  
  677. ) ); 
  678.  
  679. wc_delete_shop_order_transients(); 
  680.  
  681. function wc_update_240_db_version() { 
  682. WC_Install::update_db_version( '2.4.0' ); 
  683.  
  684. function wc_update_241_variations() { 
  685. global $wpdb; 
  686.  
  687. // Select variations that don't have any _stock_status implemented on WooCommerce 2.2 
  688. $update_variations = $wpdb->get_results( " 
  689. SELECT DISTINCT posts.ID AS variation_id, posts.post_parent AS variation_parent 
  690. FROM {$wpdb->posts} as posts 
  691. LEFT OUTER JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id AND postmeta.meta_key = '_stock_status' 
  692. WHERE posts.post_type = 'product_variation' 
  693. AND postmeta.meta_value IS NULL 
  694. " ); 
  695.  
  696. foreach ( $update_variations as $variation ) { 
  697. // Get the parent _stock_status 
  698. $parent_stock_status = get_post_meta( $variation->variation_parent, '_stock_status', true ); 
  699.  
  700. // Set the _stock_status 
  701. add_post_meta( $variation->variation_id, '_stock_status', $parent_stock_status ? $parent_stock_status : 'instock', true ); 
  702.  
  703. // Delete old product children array 
  704. delete_transient( 'wc_product_children_' . $variation->variation_parent ); 
  705.  
  706. // Invalidate old transients such as wc_var_price 
  707. WC_Cache_Helper::get_transient_version( 'product', true ); 
  708.  
  709. function wc_update_241_db_version() { 
  710. WC_Install::update_db_version( '2.4.1' ); 
  711.  
  712. function wc_update_250_currency() { 
  713. global $wpdb; 
  714. // Fix currency settings for LAK currency. 
  715. $current_currency = get_option( 'woocommerce_currency' ); 
  716.  
  717. if ( 'KIP' === $current_currency ) { 
  718. update_option( 'woocommerce_currency', 'LAK' ); 
  719.  
  720. // Update LAK currency code. 
  721. $wpdb->update( 
  722. $wpdb->postmeta,  
  723. array( 
  724. 'meta_value' => 'LAK',  
  725. ),  
  726. array( 
  727. 'meta_key' => '_order_currency',  
  728. 'meta_value' => 'KIP',  
  729. ); 
  730.  
  731. function wc_update_250_db_version() { 
  732. WC_Install::update_db_version( '2.5.0' ); 
  733.  
  734. function wc_update_260_options() { 
  735. /** 
  736. * woocommerce_calc_shipping option has been removed in 2.6 
  737. */ 
  738. if ( 'no' === get_option( 'woocommerce_calc_shipping' ) ) { 
  739. update_option( 'woocommerce_ship_to_countries', 'disabled' ); 
  740.  
  741. WC_Admin_Notices::add_notice( 'legacy_shipping' ); 
  742.  
  743. function wc_update_260_termmeta() { 
  744. global $wpdb; 
  745. /** 
  746. * Migrate term meta to WordPress tables 
  747. */ 
  748. if ( get_option( 'db_version' ) >= 34370 && $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_termmeta';" ) ) { 
  749. if ( $wpdb->query( "INSERT INTO {$wpdb->termmeta} ( term_id, meta_key, meta_value ) SELECT woocommerce_term_id, meta_key, meta_value FROM {$wpdb->prefix}woocommerce_termmeta;" ) ) { 
  750. $wpdb->query( "DROP TABLE IF EXISTS {$wpdb->prefix}woocommerce_termmeta" ); 
  751. wp_cache_flush(); 
  752.  
  753. function wc_update_260_zones() { 
  754. global $wpdb; 
  755. /** 
  756. * Old (table rate) shipping zones to new core shipping zones migration. 
  757. * zone_enabled and zone_type are no longer used, but it's safe to leave them be. 
  758. */ 
  759. if ( $wpdb->get_var( "SHOW COLUMNS FROM `{$wpdb->prefix}woocommerce_shipping_zones` LIKE 'zone_enabled';" ) ) { 
  760. $wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zones CHANGE `zone_type` `zone_type` VARCHAR(40) NOT NULL DEFAULT '';" ); 
  761. $wpdb->query( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zones CHANGE `zone_enabled` `zone_enabled` INT(1) NOT NULL DEFAULT 1;" ); 
  762.  
  763. function wc_update_260_zone_methods() { 
  764. global $wpdb; 
  765.  
  766. /** 
  767. * Shipping zones in WC 2.6.0 use a table named woocommerce_shipping_zone_methods. 
  768. * Migrate the old data out of woocommerce_shipping_zone_shipping_methods into the new table and port over any known options (used by table rates and flat rate boxes). 
  769. */ 
  770. if ( $wpdb->get_var( "SHOW TABLES LIKE '{$wpdb->prefix}woocommerce_shipping_zone_shipping_methods';" ) ) { 
  771. $old_methods = $wpdb->get_results( "SELECT zone_id, shipping_method_type, shipping_method_order, shipping_method_id FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods;" ); 
  772.  
  773. if ( $old_methods ) { 
  774. $max_new_id = $wpdb->get_var( "SELECT MAX(instance_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_methods" ); 
  775. $max_old_id = $wpdb->get_var( "SELECT MAX(shipping_method_id) FROM {$wpdb->prefix}woocommerce_shipping_zone_shipping_methods" ); 
  776.  
  777. // Avoid ID conflicts 
  778. $wpdb->query( $wpdb->prepare( "ALTER TABLE {$wpdb->prefix}woocommerce_shipping_zone_methods AUTO_INCREMENT = %d;", max( $max_new_id, $max_old_id ) + 1 ) ); 
  779.  
  780. // Store changes 
  781. $changes = array(); 
  782.  
  783. // Move data 
  784. foreach ( $old_methods as $old_method ) { 
  785. $wpdb->insert( $wpdb->prefix . 'woocommerce_shipping_zone_methods', array( 
  786. 'zone_id' => $old_method->zone_id,  
  787. 'method_id' => $old_method->shipping_method_type,  
  788. 'method_order' => $old_method->shipping_method_order,  
  789. ) ); 
  790.  
  791. $new_instance_id = $wpdb->insert_id; 
  792.  
  793. // Move main settings 
  794. $older_settings_key = 'woocommerce_' . $old_method->shipping_method_type . '-' . $old_method->shipping_method_id . '_settings'; 
  795. $old_settings_key = 'woocommerce_' . $old_method->shipping_method_type . '_' . $old_method->shipping_method_id . '_settings'; 
  796. add_option( 'woocommerce_' . $old_method->shipping_method_type . '_' . $new_instance_id . '_settings', get_option( $old_settings_key, get_option( $older_settings_key ) ) ); 
  797.  
  798. // Handling for table rate and flat rate box shipping. 
  799. if ( 'table_rate' === $old_method->shipping_method_type ) { 
  800. // Move priority settings 
  801. add_option( 'woocommerce_table_rate_default_priority_' . $new_instance_id, get_option( 'woocommerce_table_rate_default_priority_' . $old_method->shipping_method_id ) ); 
  802. add_option( 'woocommerce_table_rate_priorities_' . $new_instance_id, get_option( 'woocommerce_table_rate_priorities_' . $old_method->shipping_method_id ) ); 
  803.  
  804. // Move rates 
  805. $wpdb->update( 
  806. $wpdb->prefix . 'woocommerce_shipping_table_rates',  
  807. array( 
  808. 'shipping_method_id' => $new_instance_id,  
  809. ),  
  810. array( 
  811. 'shipping_method_id' => $old_method->shipping_method_id,  
  812. ); 
  813. } elseif ( 'flat_rate_boxes' === $old_method->shipping_method_type ) { 
  814. $wpdb->update( 
  815. $wpdb->prefix . 'woocommerce_shipping_flat_rate_boxes',  
  816. array( 
  817. 'shipping_method_id' => $new_instance_id,  
  818. ),  
  819. array( 
  820. 'shipping_method_id' => $old_method->shipping_method_id,  
  821. ); 
  822.  
  823. $changes[ $old_method->shipping_method_id ] = $new_instance_id; 
  824.  
  825. // $changes contains keys (old method ids) and values (new instance ids) if extra processing is needed in plugins. 
  826. // Store this to an option so extensions can pick it up later, then fire an action. 
  827. update_option( 'woocommerce_updated_instance_ids', $changes ); 
  828. do_action( 'woocommerce_updated_instance_ids', $changes ); 
  829.  
  830. // Change ranges used to ... 
  831. $wpdb->query( "UPDATE {$wpdb->prefix}woocommerce_shipping_zone_locations SET location_code = REPLACE( location_code, '-', '...' );" ); 
  832.  
  833. function wc_update_260_refunds() { 
  834. global $wpdb; 
  835. /** 
  836. * Refund item qty should be negative 
  837. */ 
  838. $wpdb->query( " 
  839. UPDATE {$wpdb->prefix}woocommerce_order_itemmeta as item_meta 
  840. LEFT JOIN {$wpdb->prefix}woocommerce_order_items as items ON item_meta.order_item_id = items.order_item_id 
  841. LEFT JOIN {$wpdb->posts} as posts ON items.order_id = posts.ID 
  842. SET item_meta.meta_value = item_meta.meta_value * -1 
  843. WHERE item_meta.meta_value > 0 AND item_meta.meta_key = '_qty' AND posts.post_type = 'shop_order_refund' 
  844. " ); 
  845.  
  846. function wc_update_260_db_version() { 
  847. WC_Install::update_db_version( '2.6.0' ); 
  848.  
  849. function wc_update_300_webhooks() { 
  850. /** 
  851. * Make sure product.update webhooks get the woocommerce_product_quick_edit_save 
  852. * and woocommerce_product_bulk_edit_save hooks. 
  853. */ 
  854. $product_update_webhooks = get_posts( array( 
  855. 'posts_per_page' => -1,  
  856. 'post_type' => 'shop_webhook',  
  857. 'meta_key' => '_topic',  
  858. 'meta_value' => 'product.updated',  
  859. ) ); 
  860. foreach ( $product_update_webhooks as $product_update_webhook ) { 
  861. $webhook = new WC_Webhook( $product_update_webhook->ID ); 
  862. $webhook->set_topic( 'product.updated' ); 
  863.  
  864. /** 
  865. * Add an index to the field comment_type to improve the response time of the query 
  866. * used by WC_Comments::wp_count_comments() to get the number of comments by type. 
  867. * 
  868. * @return null 
  869. */ 
  870. function wc_update_300_comment_type_index() { 
  871. global $wpdb; 
  872.  
  873. $index_exists = $wpdb->get_row( "SHOW INDEX FROM {$wpdb->comments} WHERE column_name = 'comment_type' and key_name = 'woo_idx_comment_type'" ); 
  874.  
  875. if ( is_null( $index_exists ) ) { 
  876. // Add an index to the field comment_type to improve the response time of the query 
  877. // used by WC_Comments::wp_count_comments() to get the number of comments by type. 
  878. $wpdb->query( "ALTER TABLE {$wpdb->comments} ADD INDEX woo_idx_comment_type (comment_type)" ); 
  879.  
  880. function wc_update_300_grouped_products() { 
  881. global $wpdb; 
  882. $parents = $wpdb->get_col( "SELECT DISTINCT( post_parent ) FROM {$wpdb->posts} WHERE post_parent > 0 AND post_type = 'product';" ); 
  883. foreach ( $parents as $parent_id ) { 
  884. $parent = wc_get_product( $parent_id ); 
  885. if ( $parent && $parent->is_type( 'grouped' ) ) { 
  886. $children_ids = get_posts( array( 
  887. 'post_parent' => $parent_id,  
  888. 'posts_per_page' => -1,  
  889. 'post_type' => 'product',  
  890. 'fields' => 'ids',  
  891. ) ); 
  892. update_post_meta( $parent_id, '_children', $children_ids ); 
  893.  
  894. // Update children to remove the parent. 
  895. $wpdb->update( 
  896. $wpdb->posts,  
  897. array( 
  898. 'post_parent' => 0,  
  899. ),  
  900. array( 
  901. 'post_parent' => $parent_id,  
  902. ); 
  903.  
  904. function wc_update_300_settings() { 
  905. $woocommerce_shipping_tax_class = get_option( 'woocommerce_shipping_tax_class' ); 
  906. if ( '' === $woocommerce_shipping_tax_class ) { 
  907. update_option( 'woocommerce_shipping_tax_class', 'inherit' ); 
  908. } elseif ( 'standard' === $woocommerce_shipping_tax_class ) { 
  909. update_option( 'woocommerce_shipping_tax_class', '' ); 
  910.  
  911. /** 
  912. * Convert meta values into term for product visibility. 
  913. */ 
  914. function wc_update_300_product_visibility() { 
  915. global $wpdb; 
  916.  
  917. WC_Install::create_terms(); 
  918.  
  919. if ( $featured_term = get_term_by( 'name', 'featured', 'product_visibility' ) ) { 
  920. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_featured' AND meta_value = 'yes';", $featured_term->term_taxonomy_id ) ); 
  921.  
  922. if ( $exclude_search_term = get_term_by( 'name', 'exclude-from-search', 'product_visibility' ) ) { 
  923. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_visibility' AND meta_value IN ('hidden', 'catalog');", $exclude_search_term->term_taxonomy_id ) ); 
  924.  
  925. if ( $exclude_catalog_term = get_term_by( 'name', 'exclude-from-catalog', 'product_visibility' ) ) { 
  926. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_visibility' AND meta_value IN ('hidden', 'search');", $exclude_catalog_term->term_taxonomy_id ) ); 
  927.  
  928. if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) { 
  929. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_stock_status' AND meta_value = 'outofstock';", $outofstock_term->term_taxonomy_id ) ); 
  930.  
  931. if ( $rating_term = get_term_by( 'name', 'rated-1', 'product_visibility' ) ) { 
  932. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_wc_average_rating' AND ROUND( meta_value ) = 1;", $rating_term->term_taxonomy_id ) ); 
  933.  
  934. if ( $rating_term = get_term_by( 'name', 'rated-2', 'product_visibility' ) ) { 
  935. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_wc_average_rating' AND ROUND( meta_value ) = 2;", $rating_term->term_taxonomy_id ) ); 
  936.  
  937. if ( $rating_term = get_term_by( 'name', 'rated-3', 'product_visibility' ) ) { 
  938. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_wc_average_rating' AND ROUND( meta_value ) = 3;", $rating_term->term_taxonomy_id ) ); 
  939.  
  940. if ( $rating_term = get_term_by( 'name', 'rated-4', 'product_visibility' ) ) { 
  941. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_wc_average_rating' AND ROUND( meta_value ) = 4;", $rating_term->term_taxonomy_id ) ); 
  942.  
  943. if ( $rating_term = get_term_by( 'name', 'rated-5', 'product_visibility' ) ) { 
  944. $wpdb->query( $wpdb->prepare( "INSERT IGNORE INTO {$wpdb->term_relationships} SELECT post_id, %d, 0 FROM {$wpdb->postmeta} WHERE meta_key = '_wc_average_rating' AND ROUND( meta_value ) = 5;", $rating_term->term_taxonomy_id ) ); 
  945.  
  946. /** 
  947. * Update DB Version. 
  948. */ 
  949. function wc_update_300_db_version() { 
  950. WC_Install::update_db_version( '3.0.0' ); 
.