woocsv_import_product

The Woocommerce CSV Import woocsv import product class.

Defined (1)

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

/include/class-woocsv-product.php  
  1. class woocsv_import_product 
  2. // @since 3.0.2 skip products if flag is set to true during runtime 
  3. private $logger; 
  4.  
  5. public $batch_code; 
  6.  
  7. public $skip = FALSE; 
  8.  
  9. public $new = TRUE; 
  10.  
  11. public $header = array (); 
  12.  
  13. public $tags = array (); 
  14.  
  15. public $categories = array (); 
  16.  
  17. public $images = array (); 
  18.  
  19. public $raw_data = array (); 
  20.  
  21. public $shipping_class = ''; 
  22.  
  23. public $featured_image = ''; 
  24.  
  25. public $product_gallery = ''; 
  26.  
  27. public $product_type = 'simple'; 
  28.  
  29.  
  30. /** since 3.0.6 
  31. no more use of the global $woocsv_import 
  32. */ 
  33. public $log = array (); 
  34.  
  35. //body 
  36. public $body = array ( 
  37. 'ID' => '',  
  38. 'post_type' => 'product',  
  39. 'post_status' => 'publish',  
  40. 'post_title' => '',  
  41. 'post_name' => '',  
  42. 'post_date' => '',  
  43. 'post_date_gmt' => '',  
  44. 'post_content' => '',  
  45. 'post_excerpt' => '',  
  46. 'post_parent' => 0,  
  47. 'post_password' => '',  
  48. 'comment_status' => 'open',  
  49. 'ping_status' => 'open',  
  50. 'menu_order' => 0,  
  51. 'post_author' => '',  
  52. ); 
  53.  
  54. public $meta = array ( 
  55. '_sku' => '',  
  56. '_downloadable' => 'no',  
  57. '_virtual' => 'no',  
  58. '_price' => '',  
  59. '_visibility' => 'visible',  
  60. '_stock' => '',  
  61. '_stock_status' => 'instock',  
  62. '_backorders' => 'no',  
  63. '_manage_stock' => 'yes',  
  64. '_sale_price' => '',  
  65. '_regular_price' => '',  
  66. '_weight' => '',  
  67. '_length' => '',  
  68. '_width' => '',  
  69. '_height' => '',  
  70. '_tax_status' => 'taxable',  
  71. '_tax_class' => '',  
  72. '_upsell_ids' => array (),  
  73. '_crosssell_ids' => array (),  
  74. '_sale_price_dates_from' => '',  
  75. '_sale_price_dates_to' => '',  
  76. '_min_variation_price' => '',  
  77. '_max_variation_price' => '',  
  78. '_min_variation_regular_price' => '',  
  79. '_max_variation_regular_price' => '',  
  80. '_min_variation_sale_price' => '',  
  81. '_max_variation_sale_price' => '',  
  82. '_featured' => 'no',  
  83. '_file_path' => '',  
  84. '_download_limit' => '',  
  85. '_download_expiry' => '',  
  86. '_product_url' => '',  
  87. '_button_text' => '',  
  88. // 'total_sales'=>0,  
  89. ); 
  90.  
  91. public function __construct (Allaerd\Woocsv\LogToFile $logger) 
  92. $this->logger = $logger; 
  93.  
  94. public function parse_data () 
  95. do_action( 'woocsv_before_parse_data' ); 
  96.  
  97. $this->hasMandetoryFields(); 
  98.  
  99. $this->checkPostStatus(); 
  100.  
  101. $this->checkPingStatus(); 
  102.  
  103. $this->checkPostTitle(); 
  104.  
  105. $this->checkMenuOrder(); 
  106.  
  107. $this->chekcStockStatus(); 
  108.  
  109. $this->checkVisibility(); 
  110.  
  111. $this->checkBackorders(); 
  112.  
  113. $this->checkFeatured(); 
  114.  
  115. $this->checkManageStock(); 
  116.  
  117. $this->handlePrices(); 
  118.  
  119. //add action after parsing all data 
  120. do_action( 'woocsv_after_parse_data' ); 
  121.  
  122. public function handlePrices () 
  123. global $woocsv_import; 
  124.  
  125.  
  126. //handle prices 
  127. if ($woocsv_import->get_merge_products() == 1) { 
  128. $regular_price = (in_array( 'regular_price',  
  129. $this->header ) && strlen( $this->meta[ '_regular_price' ] ) > 0) ? $this->meta[ '_regular_price' ] : $this->meta[ '_regular_price' ]; 
  130. $sale_price = (in_array( 'sale_price',  
  131. $this->header ) && strlen( $this->meta[ '_sale_price' ] ) > 0) ? $this->meta[ '_sale_price' ] : $this->meta[ '_sale_price' ]; 
  132. $price = (in_array( 'price',  
  133. $this->header ) && strlen( $this->meta[ '_price' ] ) > 0) ? $this->meta[ '_price' ] : $this->meta[ '_price' ]; 
  134. } else { 
  135. $regular_price = (in_array( 'regular_price',  
  136. $this->header ) && strlen( $this->meta[ '_regular_price' ] ) > 0) ? $this->meta[ '_regular_price' ] : ''; 
  137. $sale_price = (in_array( 'sale_price',  
  138. $this->header ) && strlen( $this->meta[ '_sale_price' ] ) > 0) ? $this->meta[ '_sale_price' ] : ''; 
  139. $price = (in_array( 'price',  
  140. $this->header ) && strlen( $this->meta[ '_price' ] ) > 0) ? $this->meta[ '_price' ] : ''; 
  141.  
  142. $regular_price = $this->normalizePrice( $regular_price ); 
  143. $sale_price = $this->normalizePrice( $sale_price ); 
  144. $price = $this->normalizePrice( $price ); 
  145.  
  146. //product on sale 
  147. if ($sale_price > 0 && $sale_price < $regular_price) { 
  148. $this->logger->log( __( 'Product is on sale', 'woocommerce-csvimport' ) ); 
  149. $price = $sale_price; 
  150. } else { 
  151. //the product is not on sale 
  152. $price = $regular_price; 
  153. $sale_price = ''; 
  154.  
  155. //set prices 
  156. $this->meta[ '_regular_price' ] = $regular_price; 
  157. $this->meta[ '_sale_price' ] = $sale_price; 
  158. $this->meta[ '_price' ] = $price; 
  159.  
  160. public function merge_product ($id) 
  161. //get post data and store it 
  162. $post = get_post( $id, 'ARRAY_A' ); 
  163. $this->body = $post; 
  164.  
  165. //get meta data and store it 
  166. $post_meta = get_metadata( 'post', $id, '', TRUE ); 
  167. foreach ($post_meta as $key => $value) { 
  168. $this->meta[ $key ] = maybe_unserialize( $value[ 0 ] ); 
  169.  
  170. //get product_tpe 
  171. $product_types = wp_get_object_terms( $this->body[ 'ID' ], 'product_type' ); 
  172.  
  173. if (!is_wp_error( $product_types )) { 
  174. foreach ($product_types as $product_type) { 
  175. $this->product_type = $product_type->name; 
  176.  
  177.  
  178. public function get_product_by_id ($sku = NULL) 
  179. global $wpdb; 
  180. $product_id = NULL; 
  181.  
  182. /** ! @since 3.0.6 use new function wc_get_product_id_by_sku to get SKU */ 
  183. if (function_exists( 'wc_get_product_id_by_sku' )) { 
  184. $product_id = wc_get_product_id_by_sku( $sku ); 
  185. } else { 
  186. $product_id = $wpdb->get_var( $wpdb->prepare( "SELECT max(post_id) FROM $wpdb->postmeta a, $wpdb->posts b 
  187. WHERE a.post_id= b.id and meta_key='_sku' AND meta_value='%s' LIMIT 1", $sku ) ); 
  188.  
  189. if ($product_id) { 
  190. $product[ 'ID' ] = $product_id; 
  191.  
  192. return $product_id = apply_filters( 'woocsv_get_product_id', $product_id, $sku ); 
  193.  
  194. public function save () 
  195.  
  196. // @since 3.0.2 if skip is true, skip the product during import 
  197. if ($this->skip) { 
  198. return FALSE; 
  199.  
  200. // ! @since 3.0.6 
  201. // do it all in one update 
  202. //save tags before 
  203. if ($this->tags) { 
  204. $this->body[ 'tax_input' ] = array ( 'product_tag' => explode( '|', $this->tags ) ); 
  205.  
  206. //save the post 
  207. $post_id = wp_insert_post( $this->body, TRUE ); 
  208.  
  209. if (is_wp_error( $post_id )) { 
  210.  
  211. $this->logger->log( __( 'Product could not be saved and skipped', 'woocommerce-csvimport' ) ); 
  212. return FALSE; 
  213.  
  214. } else { 
  215.  
  216. $this->logger->log( sprintf( __( 'Product saved with ID: %s', 'woocommerce-csvimport' ), $post_id ) ); 
  217. $this->body[ 'ID' ] = $post_id; 
  218.  
  219.  
  220. do_action( 'woocsv_product_after_body_save' ); 
  221.  
  222. //save the product type 
  223. wp_set_object_terms( $post_id, $this->product_type, 'product_type', FALSE ); 
  224.  
  225. do_action( 'woocsv_product_before_meta_save' ); 
  226.  
  227. //save the meta 
  228. foreach ($this->meta as $key => $value) { 
  229. update_post_meta( $post_id, $key, $value ); 
  230.  
  231. do_action( 'woocsv_product_before_categorie_save' ); 
  232.  
  233. //save categories 
  234. if (!empty($this->categories)) { 
  235. $this->save_categories( $post_id ); 
  236.  
  237. do_action( 'woocsv_product_before_images_save' ); 
  238.  
  239. if (!empty($this->featured_image)) { 
  240. $this->save_featured_image(); 
  241. } else { 
  242. $this->logger->log(__('No featured image available ', 'woocommerce-csvimport')); 
  243.  
  244. //save the product gallery 
  245. if (!empty($this->product_gallery)) { 
  246. $this->save_product_gallery(); 
  247.  
  248. do_action( 'woocsv_product_before_shipping_save' ); 
  249.  
  250. // save shipping class 
  251. if ($this->shipping_class) { 
  252. $this->save_shipping_class(); 
  253.  
  254. do_action( 'woocsv_after_save', $this ); 
  255.  
  256. //clear transients 
  257. if (function_exists( 'wc_delete_product_transients' )) { 
  258. wc_delete_product_transients( $post_id ); 
  259.  
  260. do_action( 'woocsv_product_after_save' ); 
  261.  
  262. //and return the ID 
  263. return $post_id; 
  264.  
  265. public function save_tags ($post_id) 
  266. global $woocsv_import; 
  267. //2.1.1 If merging do not delete else clear currrent tag 
  268. if (!$woocsv_import->get_merge_products()) { 
  269. wp_set_object_terms( $this->body[ 'ID' ], NULL, 'product_tag' ); 
  270.  
  271. //handle tags 
  272. foreach ($this->tags as $tags) { 
  273. $tags = explode( '|', $tags ); 
  274. wp_set_object_terms( $post_id, $tags, 'product_tag', TRUE ); 
  275.  
  276. public function save_shipping_class () 
  277. global $woocsv_import; 
  278.  
  279. //2.2.2 If merging do not delete else clear currrent tag 
  280. if (!$woocsv_import->get_merge_products()) { 
  281. wp_set_object_terms( $this->body[ 'ID' ], NULL, 'product_shipping_class' ); 
  282.  
  283. $term = term_exists( $this->shipping_class, 'product_shipping_class' ); 
  284.  
  285. // @since 2.2.2 beter handling for shipping class 
  286. if (!is_array( $term )) { 
  287. $term = wp_insert_term( $this->shipping_class, 'product_shipping_class' ); 
  288.  
  289. if (!is_wp_error( $term )) { 
  290. wp_set_object_terms( $this->body[ 'ID' ], array ( (int)$term[ 'term_id' ] ), 'product_shipping_class' ); 
  291.  
  292. public function save_categories () 
  293. global $woocsv_import; 
  294.  
  295. //check out http://wordpress.stackexchange.com/questions/24498/wp-insert-term-parent-child-problem 
  296. delete_option( "product_cat_children" ); 
  297.  
  298. //clear currrent 
  299. //2.1.1 If merging do not delete else clear currrent category 
  300. if (!$woocsv_import->get_merge_products()) { 
  301. wp_set_object_terms( $this->body[ 'ID' ], NULL, 'product_cat' ); 
  302.  
  303. foreach ($this->categories as $category) { 
  304. $cats = explode( '|', $category ); 
  305. foreach ($cats as $cat) { 
  306. $cat_taxs = explode( '->', $cat ); 
  307.  
  308. $parent = 0; 
  309.  
  310. foreach ($cat_taxs as $cat_tax) { 
  311.  
  312. $new_cat = term_exists( $cat_tax, 'product_cat', $parent ); 
  313. if (!is_array( $new_cat )) { 
  314. $new_cat = wp_insert_term( $cat_tax, 'product_cat',  
  315. array ( 'slug' => $cat_tax, 'parent' => $parent ) ); 
  316. if (!is_wp_error( $new_cat )) { 
  317. $parent = $new_cat[ 'term_id' ]; 
  318.  
  319. if (!is_wp_error( $new_cat ) && $woocsv_import->get_add_to_categories() == 1) { 
  320. wp_set_object_terms( $this->body[ 'ID' ], (int)$new_cat[ 'term_id' ], 'product_cat', TRUE ); 
  321.  
  322. if (!is_wp_error( $new_cat ) && $woocsv_import->get_add_to_categories() == 0) { 
  323. wp_set_object_terms( $this->body[ 'ID' ], (int)$new_cat[ 'term_id' ], 'product_cat', TRUE ); 
  324.  
  325. public function save_featured_image () 
  326. $imageID = FALSE; 
  327.  
  328. if ($this->is_valid_url( $this->featured_image )) { 
  329. $this->logger->log( __( 'featured image is imported using the URL', 'woocommerce-csvimport' ) ); 
  330. $imageID = $this->save_image_with_url( $this->featured_image ); 
  331. } else { 
  332. $this->logger->log( __( 'featured image is imported using the filename', 'woocommerce-csvimport' ) ); 
  333. $imageID = $this->save_image_with_name( $this->featured_image ); 
  334.  
  335. // @ since XXX check if the url is valid 
  336. if (!is_wp_error( $imageID )) { 
  337. set_post_thumbnail( $this->body[ 'ID' ], $imageID ); 
  338. $this->logger->log( __( 'The image is attached as featured image', 'woocommerce-csvimport' ) ); 
  339. } else { 
  340. $this->logger->log( __( 'The image could not be attached as featured image', 'woocommerce-csvimport' ) ); 
  341.  
  342.  
  343.  
  344. public function save_product_gallery () 
  345.  
  346. $images = explode( '|', $this->product_gallery ); 
  347. $gallery = FALSE; 
  348. foreach ($images as $image) { 
  349. if ($this->is_valid_url( $image )) { 
  350. $imageID = $this->save_image_with_url( $image ); 
  351. } else { 
  352. $imageID = $this->save_image_with_name( $image ); 
  353.  
  354. if ($imageID) { 
  355. $gallery[] = $imageID; 
  356.  
  357. if ($gallery) { 
  358. $meta_value = implode( ', ', $gallery ); 
  359. update_post_meta( $this->body[ 'ID' ], '_product_image_gallery', $meta_value ); 
  360.  
  361.  
  362. //@since 3.0.5 use WP functions to upload and handle images with url's 
  363. function save_image_with_url ($url) 
  364. global $woocsv_import; 
  365.  
  366. $tmp = download_url( $url, 10 ); 
  367.  
  368. if (is_wp_error( $tmp )) { 
  369. //something went wrong during download 
  370. @unlink( $file_array[ 'tmp_name' ] ); 
  371. $this->logger->log( sprintf( __( 'Image with url: %s could not be downloaded', 'woocommerce-csvimport' ), $url ) ); 
  372. return FALSE; 
  373.  
  374. $post_id = $this->body[ 'ID' ]; 
  375. $file_array = array (); 
  376.  
  377. @preg_match( '/[^\?]+\.(jpg|jpe|jpeg|gif|png|pdf)/i', $url, $matches ); 
  378. if (!$matches) { 
  379. $file_array[ 'tmp_name' ] = $tmp; 
  380. $file_array[ 'name' ] = sanitize_file_name( $url ); 
  381. $desc = ''; 
  382. } else { 
  383. $file_array[ 'name' ] = basename( $matches[ 0 ] ); 
  384. $file_array[ 'tmp_name' ] = $tmp; 
  385. $desc = $file_array[ 'name' ]; 
  386.  
  387. // do the validation and storage stuff 
  388. $id = media_handle_sideload( $file_array, $post_id, $desc ); 
  389.  
  390. // If error storing permanently, unlink 
  391. if (is_wp_error( $id )) { 
  392. @unlink( $file_array[ 'tmp_name' ] ); 
  393. $this->logger->log( sprintf( __( 'Image with url: %s could not be linked to the post', 'woocommerce-csvimport' ), $url ) ); 
  394. return $id; 
  395.  
  396. $this->logger->log( sprintf( __( 'Image with url: %s uploaded', 'woocommerce-csvimport' ), $url ) ); 
  397.  
  398. return $id; 
  399.  
  400. public function save_image_with_name ($image) 
  401. /** use get_posts to retrieve image instead of query direct! */ 
  402.  
  403. //set up the args 
  404. $args = array ( 
  405. 'numberposts' => 1,  
  406. 'orderby' => 'post_date',  
  407. 'order' => 'DESC',  
  408. 'post_type' => 'attachment',  
  409. //'post_mime_type'=> 'image',  
  410. 'post_status' => 'any',  
  411. 'meta_query' => array ( 
  412. array ( 
  413. 'key' => '_wp_attached_file',  
  414. 'value' => '/' . sanitize_file_name( $image ),  
  415. 'compare' => 'LIKE',  
  416. ),  
  417. ),  
  418. ); 
  419. //get the images 
  420. $images = get_posts( $args ); 
  421.  
  422. if (!empty($images)) { 
  423. //we found a match, return it! 
  424. return (int)$images[ 0 ]->ID; 
  425. } else { 
  426. //no image found with the same name, return false 
  427. return FALSE; 
  428.  
  429.  
  430. public function fill_in_data () 
  431. global $woocsv_import; 
  432. do_action( 'woocsv_product_before_fill_in_data' ); 
  433.  
  434. $id = FALSE; 
  435.  
  436. //check if the product already exists by checking it's ID 
  437. if (in_array( 'ID', $this->header )) { 
  438. $tempID = $this->raw_data[ array_search( 'ID', $this->header ) ]; 
  439. if ($tempID) { 
  440.  
  441. //use get_post instead of get_posts 
  442. $test = new WC_Product( $tempID ); 
  443.  
  444. if ($test->post) { 
  445. $this->logger->log( sprintf( __( 'Product found (ID), ID is: %s', 'woocommerce-csvimport' ),  
  446. $tempID ) ); 
  447. $this->new = FALSE; 
  448. // @ since 3.0.5 add ID else merging will not work using ID's 
  449. $id = $tempID; 
  450. } else { 
  451. /** set the ID to null */ 
  452. $this->raw_data[ array_search( 'ID', $this->header ) ] = ''; 
  453. $this->body[ 'ID' ] = ''; 
  454. $this->logger->log( sprintf( __( 'ID : %s not found!', 'woocommerce-csvimport' ), $tempID ) ); 
  455.  
  456. //check if the product already exists by checking it's sku 
  457. if (empty($id) && in_array( 'sku', $this->header ) && $woocsv_import->get_match_by() == 'sku') { 
  458. $sku = $this->raw_data[ array_search( 'sku', $this->header ) ]; 
  459.  
  460. if (!empty($sku)) { 
  461. $id = $this->get_product_by_id( $sku ); 
  462. if (!empty($id)) { 
  463. $this->new = FALSE; 
  464. $this->logger->log(sprintf( __( 'Product found (SKU), ID is: %s', 'woocommerce-csvimport' ), $id ) ); 
  465. } else { 
  466. $this->logger->log( __( 'New product', 'woocommerce-csvimport' ) ); 
  467.  
  468. //check if the product already exists by checking it's post title 
  469. if (empty($id) && in_array( 'post_title', $this->header ) && $woocsv_import->get_match_by() == 'title') { 
  470. $post_title = $this->raw_data[ array_search( 'post_title', $this->header ) ]; 
  471.  
  472. if ($post_title) { 
  473. $testID = get_page_by_title( $post_title, ARRAY_A, 'product' ); 
  474. if ($testID) { 
  475. $this->logger->log( __( 'Product found by title', 'woocommerce-csvimport' ) ); 
  476. $id = $testID[ 'ID' ]; 
  477. $this->new = FALSE; 
  478. } else { 
  479. $this->logger->log( __( 'Product not found', 'woocommerce-csvimport' ) ); 
  480.  
  481. //check for if we need to merge the product 
  482.  
  483. if ($id && $woocsv_import->get_merge_products() == 1) { 
  484. $this->merge_product( $id ); 
  485. //fill in the product body 
  486. foreach ($this->body as $key => $value) { 
  487. if (in_array( $key, $this->header )) { 
  488. $key_body = array_search( $key, $this->header ); 
  489. if (isset ($this->raw_data[ $key_body ])) { 
  490. $this->body[ $key ] = $this->raw_data[ $key_body ]; 
  491.  
  492. // get the author 
  493. if (isset($this->body[ 'post_author' ]) && in_array( 'post_author', $this->header )) { 
  494.  
  495. $user = get_user_by( ($woocsv_import->get_match_author_by()) ? $woocsv_import->get_match_author_by() : 'login',  
  496. $this->body[ 'post_author' ] ); 
  497. if ($user) { 
  498. $this->body[ 'post_author' ] = $user->ID; 
  499. $this->logger->log( __( 'user found', 'woocommerce-csvimport' ) ); 
  500. } else { 
  501. $this->body[ 'post_author' ] = ''; 
  502. $this->logger->log( __( 'user not found', 'woocommerce-csvimport' ) ); 
  503.  
  504. //fill in the ID if the product already exists 
  505. if ($id) { 
  506. $this->body[ 'ID' ] = $id; 
  507.  
  508. //fill in the meta data 
  509. // @ since 3.0.5 
  510. // trim meta values to loose spaces 
  511. foreach ($this->meta as $key => $value) { 
  512. if (in_array( substr( $key, 1 ), $this->header )) { 
  513. $this->meta[ $key ] = trim( $this->raw_data[ array_search( substr( $key, 1 ), $this->header ) ] ); 
  514.  
  515. // @ since 3.0.5 
  516. // if the product is new add total_sales to show it in the front end 
  517. // some themes needed total_sales for popularity sorting 
  518. if (!empty ($this->body[ 'ID' ])) { 
  519. $this->meta[ 'total_sales' ] = 0; 
  520.  
  521. //check if there are tags 
  522. if (in_array( 'tags', $this->header )) { 
  523. $key = array_search( 'tags', $this->header ); 
  524. $this->tags = $this->raw_data[ $key ]; 
  525.  
  526. //check if there is a shipping 
  527. if (in_array( 'shipping_class', $this->header )) { 
  528. $key = array_search( 'shipping_class', $this->header ); 
  529. $this->shipping_class = trim( $this->raw_data[ $key ] ); 
  530.  
  531. //check if there are categories 
  532. if (in_array( 'category', $this->header )) { 
  533. foreach ($this->header as $key => $value) { 
  534. if ($value == 'category') { 
  535. $this->categories[] = $this->raw_data[ $key ]; 
  536.  
  537. /** change_stock */ 
  538. if (in_array( 'change_stock', $this->header )) { 
  539. $key = array_search( 'change_stock', $this->header ); 
  540. $change_stock = $this->raw_data[ $key ]; 
  541.  
  542. //get the stock 
  543. $stock = get_post_meta( $this->body[ 'ID' ], '_stock', TRUE ); 
  544.  
  545. //if the stock is empty set it to 0 
  546. if (!$stock) { 
  547. $stock = 0; 
  548.  
  549. //calculate the new stock level 
  550. $new_stock = $stock + $change_stock; 
  551.  
  552. //set new stock in the meta 
  553. $this->meta[ '_stock' ] = $new_stock; 
  554.  
  555. //set log 
  556. $this->logger->log( sprintf( __( 'Change stock modus: stock changed from %s to %s', 'woocommerce-csvimport' ), $stock, $new_stock ) ); 
  557.  
  558. //check if there is a featured image 
  559. if (in_array( 'featured_image', $this->header )) { 
  560. $key = array_search('featured_image', $this->header); 
  561. $this->featured_image = $this->raw_data[ $key ]; 
  562.  
  563. //check if there is a product gallery 
  564. if (in_array( 'product_gallery', $this->header )) { 
  565. $key = array_search( 'product_gallery', $this->header ); 
  566. $this->product_gallery = $this->raw_data[ $key ]; 
  567.  
  568. $this->logger->log(__($this->featured_image, 'woocommerce-csvimport')); 
  569.  
  570. do_action( 'woocsv_product_after_fill_in_data' ); 
  571.  
  572.  
  573. public function normalizePrice ($num) 
  574.  
  575. if (!$num) { 
  576. return false; 
  577.  
  578. $dotPos = strrpos( $num, '.' ); 
  579. $commaPos = strrpos( $num, ', ' ); 
  580. $sep = (($dotPos > $commaPos) && $dotPos) ? $dotPos : ((($commaPos > $dotPos) && $commaPos) ? $commaPos : FALSE); 
  581.  
  582. if (!$sep) { 
  583. return floatval( preg_replace( "/[^0-9]/", "", $num ) ); 
  584.  
  585. return floatval( preg_replace( "/[^0-9]/", "", substr( $num, 0, $sep ) ) . '.' . preg_replace( "/[^0-9]/", "",  
  586. substr( $num, $sep + 1, strlen( $num ) ) ) ); 
  587.  
  588. // helpers 
  589. public function is_valid_url ($url) 
  590. // alternative way to check for a valid url 
  591. if (filter_var( $url, FILTER_VALIDATE_URL ) === FALSE) { 
  592. return FALSE; 
  593. } else { 
  594. return TRUE; 
  595.  
  596.  
  597. public function hasMandetoryFields () 
  598. if (empty($this->body[ 'post_title' ]) && empty($this->body[ 'post_name' ]) && empty($this->body[ 'post_content' ]) && $this->body[ 'post_type' ] == 'product') { 
  599. $this->logger->log( __( 'No title, slug or content. Filled in dummy content', 'woocommerce-csvimport' ) ); 
  600. $this->body[ 'post_content' ] = ' '; 
  601.  
  602. public function checkPostStatus () 
  603. $post_status = array ( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ); 
  604. if (!in_array( $this->body[ 'post_status' ], $post_status )) { 
  605. $this->logger->log( __( 'post status changed to publish', 'woocommerce-csvimport' ) ); 
  606. $this->body[ 'post_status' ] = 'publish'; 
  607.  
  608. public function checkPingStatus () 
  609. if (!in_array( $this->body[ 'ping_status' ], array ( 'open', 'closed' ) )) { 
  610. $this->logger->log( __( 'ping status changed to ping', 'woocommerce-csvimport' ) ); 
  611. $this->body[ 'ping_status' ] = 'open'; 
  612.  
  613. public function checkPostTitle () 
  614. if (empty($this->body[ 'post_title' ]) && $this->body[ 'post_type' ] == 'product') { 
  615. $this->logger->log( __( 'title is empty status changed to draft', 'woocommerce-csvimport' ) ); 
  616. $this->body[ 'post_status' ] = 'draft'; 
  617.  
  618. public function checkMenuOrder () 
  619. if (!is_numeric( $this->body[ 'menu_order' ] )) { 
  620. $this->logger->log( __( 'menu order changed to 0', 'woocommerce-csvimport' ) ); 
  621. $this->body[ 'menu_order' ] = 0; 
  622.  
  623. public function chekcStockStatus () 
  624. if (in_array( 'stock_status', $this->header ) && !in_array( $this->meta[ '_stock_status' ],  
  625. array ( 'instock', 'outofstock' ) ) 
  626. ) { 
  627. $this->logger->log( __( 'stock status changed to instock', 'woocommerce-csvimport' ) ); 
  628. $this->meta[ '_stock_status' ] = 'instock'; 
  629.  
  630. public function checkVisibility () 
  631. if (in_array( 'visibility', $this->header ) && !in_array( $this->meta[ '_visibility' ],  
  632. array ( 'visible', 'catalog', 'search', 'hidden' ) ) 
  633. ) { 
  634. $this->logger->log( __( 'visibility changed to visible', 'woocommerce-csvimport' ) ); 
  635. $this->meta[ '_visibility' ] = 'visible'; 
  636.  
  637. public function checkBackorders () 
  638. if (in_array( 'backorders', $this->header ) && !in_array( $this->meta[ '_backorders' ],  
  639. array ( 'yes', 'no', 'notify' ) ) 
  640. ) { 
  641. $this->logger->log( __( 'backorders changed to no', 'woocommerce-csvimport' ) ); 
  642. $this->meta[ '_backorders' ] = 'no'; 
  643.  
  644. public function checkFeatured () 
  645. if (in_array( 'featured', $this->header ) && !in_array( $this->meta[ '_featured' ], array ( 'yes', 'no' ) )) { 
  646. $this->logger->log( __( 'featured changed to no', 'woocommerce-csvimport' ) ); 
  647. $this->meta[ '_featured' ] = 'no'; 
  648.  
  649. public function checkManageStock () 
  650. if (in_array( 'manage_stock', $this->header ) && !in_array( $this->meta[ '_manage_stock' ],  
  651. array ( 'yes', 'no' ) ) 
  652. ) { 
  653. $this->logger->log( __( 'manage_stock changed to no', 'woocommerce-csvimport' ) ); 
  654. $this->meta[ '_manage_stock' ] = 'no'; 
  655.