WC_Product_Data_Store_CPT

WC Product Data Store: Stored in CPT.

Defined (1)

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

/includes/data-stores/class-wc-product-data-store-cpt.php  
  1. class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Data_Store_Interface, WC_Product_Data_Store_Interface { 
  2.  
  3. /** 
  4. * Data stored in meta keys, but not considered "meta". 
  5. * @since 3.0.0 
  6. * @var array 
  7. */ 
  8. protected $internal_meta_keys = array( 
  9. '_visibility',  
  10. '_sku',  
  11. '_price',  
  12. '_regular_price',  
  13. '_sale_price',  
  14. '_sale_price_dates_from',  
  15. '_sale_price_dates_to',  
  16. 'total_sales',  
  17. '_tax_status',  
  18. '_tax_class',  
  19. '_manage_stock',  
  20. '_stock',  
  21. '_stock_status',  
  22. '_backorders',  
  23. '_sold_individually',  
  24. '_weight',  
  25. '_length',  
  26. '_width',  
  27. '_height',  
  28. '_upsell_ids',  
  29. '_crosssell_ids',  
  30. '_purchase_note',  
  31. '_default_attributes',  
  32. '_product_attributes',  
  33. '_virtual',  
  34. '_downloadable',  
  35. '_featured',  
  36. '_downloadable_files',  
  37. '_wc_rating_count',  
  38. '_wc_average_rating',  
  39. '_wc_review_count',  
  40. '_variation_description',  
  41. '_thumbnail_id',  
  42. '_file_paths',  
  43. '_product_image_gallery',  
  44. '_product_version',  
  45. '_wp_old_slug',  
  46. '_edit_last',  
  47. '_edit_lock',  
  48. ); 
  49.  
  50. /** 
  51. * If we have already saved our extra data, don't do automatic / default handling. 
  52. */ 
  53. protected $extra_data_saved = false; 
  54.  
  55. /** 
  56. * Stores updated props. 
  57. * @var array 
  58. */ 
  59. protected $updated_props = array(); 
  60.  
  61. /** 
  62. |-------------------------------------------------------------------------- 
  63. | CRUD Methods 
  64. |-------------------------------------------------------------------------- 
  65. */ 
  66.  
  67. /** 
  68. * Method to create a new product in the database. 
  69. * @param WC_Product 
  70. */ 
  71. public function create( &$product ) { 
  72. if ( ! $product->get_date_created() ) { 
  73. $product->set_date_created( current_time( 'timestamp', true ) ); 
  74.  
  75. $id = wp_insert_post( apply_filters( 'woocommerce_new_product_data', array( 
  76. 'post_type' => 'product',  
  77. 'post_status' => $product->get_status() ? $product->get_status() : 'publish',  
  78. 'post_author' => get_current_user_id(),  
  79. 'post_title' => $product->get_name() ? $product->get_name() : __( 'Product', 'woocommerce' ),  
  80. 'post_content' => $product->get_description(),  
  81. 'post_excerpt' => $product->get_short_description(),  
  82. 'post_parent' => $product->get_parent_id(),  
  83. 'comment_status' => $product->get_reviews_allowed() ? 'open' : 'closed',  
  84. 'ping_status' => 'closed',  
  85. 'menu_order' => $product->get_menu_order(),  
  86. 'post_date' => gmdate( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getOffsetTimestamp() ),  
  87. 'post_date_gmt' => gmdate( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getTimestamp() ),  
  88. 'post_name' => $product->get_slug( 'edit' ),  
  89. ) ), true ); 
  90.  
  91. if ( $id && ! is_wp_error( $id ) ) { 
  92. $product->set_id( $id ); 
  93.  
  94. $this->update_post_meta( $product, true ); 
  95. $this->update_terms( $product, true ); 
  96. $this->update_visibility( $product, true ); 
  97. $this->update_attributes( $product, true ); 
  98. $this->update_version_and_type( $product ); 
  99. $this->handle_updated_props( $product ); 
  100.  
  101. $product->save_meta_data(); 
  102. $product->apply_changes(); 
  103.  
  104. $this->clear_caches( $product ); 
  105.  
  106. do_action( 'woocommerce_new_product', $id ); 
  107.  
  108. /** 
  109. * Method to read a product from the database. 
  110. * @param WC_Product 
  111. */ 
  112. public function read( &$product ) { 
  113. $product->set_defaults(); 
  114.  
  115. if ( ! $product->get_id() || ! ( $post_object = get_post( $product->get_id() ) ) || 'product' !== $post_object->post_type ) { 
  116. throw new Exception( __( 'Invalid product.', 'woocommerce' ) ); 
  117.  
  118. $id = $product->get_id(); 
  119.  
  120. $product->set_props( array( 
  121. 'name' => $post_object->post_title,  
  122. 'slug' => $post_object->post_name,  
  123. 'date_created' => 0 < $post_object->post_date_gmt ? wc_string_to_timestamp( $post_object->post_date_gmt ) : null,  
  124. 'date_modified' => 0 < $post_object->post_modified_gmt ? wc_string_to_timestamp( $post_object->post_modified_gmt ) : null,  
  125. 'status' => $post_object->post_status,  
  126. 'description' => $post_object->post_content,  
  127. 'short_description' => $post_object->post_excerpt,  
  128. 'parent_id' => $post_object->post_parent,  
  129. 'menu_order' => $post_object->menu_order,  
  130. 'reviews_allowed' => 'open' === $post_object->comment_status,  
  131. ) ); 
  132.  
  133. $this->read_attributes( $product ); 
  134. $this->read_downloads( $product ); 
  135. $this->read_visibility( $product ); 
  136. $this->read_product_data( $product ); 
  137. $this->read_extra_data( $product ); 
  138. $product->set_object_read( true ); 
  139.  
  140. /** 
  141. * Method to update a product in the database. 
  142. * @param WC_Product 
  143. */ 
  144. public function update( &$product ) { 
  145. $product->save_meta_data(); 
  146. $changes = $product->get_changes(); 
  147.  
  148. // Only update the post when the post data changes. 
  149. if ( array_intersect( array( 'description', 'short_description', 'name', 'parent_id', 'reviews_allowed', 'status', 'menu_order', 'date_created', 'date_modified', 'slug' ), array_keys( $changes ) ) ) { 
  150. $post_data = array( 
  151. 'post_content' => $product->get_description( 'edit' ),  
  152. 'post_excerpt' => $product->get_short_description( 'edit' ),  
  153. 'post_title' => $product->get_name( 'edit' ),  
  154. 'post_parent' => $product->get_parent_id( 'edit' ),  
  155. 'comment_status' => $product->get_reviews_allowed( 'edit' ) ? 'open' : 'closed',  
  156. 'post_status' => $product->get_status( 'edit' ) ? $product->get_status( 'edit' ) : 'publish',  
  157. 'menu_order' => $product->get_menu_order( 'edit' ),  
  158. 'post_name' => $product->get_slug( 'edit' ),  
  159. ); 
  160. if ( $product->get_date_created( 'edit' ) ) { 
  161. $post_data['post_date'] = gmdate( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getOffsetTimestamp() ); 
  162. $post_data['post_date_gmt'] = gmdate( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getTimestamp() ); 
  163. if ( isset( $changes['date_modified'] ) && $product->get_date_modified( 'edit' ) ) { 
  164. $post_data['post_modified'] = gmdate( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' )->getOffsetTimestamp() ); 
  165. $post_data['post_modified_gmt'] = gmdate( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' )->getTimestamp() ); 
  166. } else { 
  167. $post_data['post_modified'] = current_time( 'mysql' ); 
  168. $post_data['post_modified_gmt'] = current_time( 'mysql', 1 ); 
  169.  
  170. /** 
  171. * When updating this object, to prevent infinite loops, use $wpdb 
  172. * to update data, since wp_update_post spawns more calls to the 
  173. * save_post action. 
  174. * This ensures hooks are fired by either WP itself (admin screen save),  
  175. * or an update purely from CRUD. 
  176. */ 
  177. if ( doing_action( 'save_post' ) ) { 
  178. $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $product->get_id() ) ); 
  179. clean_post_cache( $product->get_id() ); 
  180. } else { 
  181. wp_update_post( array_merge( array( 'ID' => $product->get_id() ), $post_data ) ); 
  182. $product->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. 
  183.  
  184. $this->update_post_meta( $product ); 
  185. $this->update_terms( $product ); 
  186. $this->update_visibility( $product ); 
  187. $this->update_attributes( $product ); 
  188. $this->update_version_and_type( $product ); 
  189. $this->handle_updated_props( $product ); 
  190.  
  191. $product->apply_changes(); 
  192.  
  193. $this->clear_caches( $product ); 
  194.  
  195. do_action( 'woocommerce_update_product', $product->get_id() ); 
  196.  
  197. /** 
  198. * Method to delete a product from the database. 
  199. * @param WC_Product 
  200. * @param array $args Array of args to pass to the delete method. 
  201. */ 
  202. public function delete( &$product, $args = array() ) { 
  203. $id = $product->get_id(); 
  204. $post_type = $product->is_type( 'variation' ) ? 'product_variation' : 'product'; 
  205.  
  206. $args = wp_parse_args( $args, array( 
  207. 'force_delete' => false,  
  208. ) ); 
  209.  
  210. if ( $args['force_delete'] ) { 
  211. wp_delete_post( $product->get_id() ); 
  212. $product->set_id( 0 ); 
  213. do_action( 'woocommerce_delete_' . $post_type, $id ); 
  214. } else { 
  215. wp_trash_post( $product->get_id() ); 
  216. $product->set_status( 'trash' ); 
  217. do_action( 'woocommerce_trash_' . $post_type, $id ); 
  218.  
  219. /** 
  220. |-------------------------------------------------------------------------- 
  221. | Additional Methods 
  222. |-------------------------------------------------------------------------- 
  223. */ 
  224.  
  225. /** 
  226. * Read product data. Can be overridden by child classes to load other props. 
  227. * @param WC_Product 
  228. * @since 3.0.0 
  229. */ 
  230. protected function read_product_data( &$product ) { 
  231. $id = $product->get_id(); 
  232.  
  233. if ( '' === ( $review_count = get_post_meta( $id, '_wc_review_count', true ) ) ) { 
  234. WC_Comments::get_review_count_for_product( $product ); 
  235. } else { 
  236. $product->set_review_count( $review_count ); 
  237.  
  238. if ( '' === ( $rating_counts = get_post_meta( $id, '_wc_rating_count', true ) ) ) { 
  239. WC_Comments::get_rating_counts_for_product( $product ); 
  240. } else { 
  241. $product->set_rating_counts( $rating_counts ); 
  242.  
  243. if ( '' === ( $average_rating = get_post_meta( $id, '_wc_average_rating', true ) ) ) { 
  244. WC_Comments::get_average_rating_for_product( $product ); 
  245. } else { 
  246. $product->set_average_rating( $average_rating ); 
  247.  
  248. $product->set_props( array( 
  249. 'sku' => get_post_meta( $id, '_sku', true ),  
  250. 'regular_price' => get_post_meta( $id, '_regular_price', true ),  
  251. 'sale_price' => get_post_meta( $id, '_sale_price', true ),  
  252. 'price' => get_post_meta( $id, '_price', true ),  
  253. 'date_on_sale_from' => get_post_meta( $id, '_sale_price_dates_from', true ),  
  254. 'date_on_sale_to' => get_post_meta( $id, '_sale_price_dates_to', true ),  
  255. 'total_sales' => get_post_meta( $id, 'total_sales', true ),  
  256. 'tax_status' => get_post_meta( $id, '_tax_status', true ),  
  257. 'tax_class' => get_post_meta( $id, '_tax_class', true ),  
  258. 'manage_stock' => get_post_meta( $id, '_manage_stock', true ),  
  259. 'stock_quantity' => get_post_meta( $id, '_stock', true ),  
  260. 'stock_status' => get_post_meta( $id, '_stock_status', true ),  
  261. 'backorders' => get_post_meta( $id, '_backorders', true ),  
  262. 'sold_individually' => get_post_meta( $id, '_sold_individually', true ),  
  263. 'weight' => get_post_meta( $id, '_weight', true ),  
  264. 'length' => get_post_meta( $id, '_length', true ),  
  265. 'width' => get_post_meta( $id, '_width', true ),  
  266. 'height' => get_post_meta( $id, '_height', true ),  
  267. 'upsell_ids' => get_post_meta( $id, '_upsell_ids', true ),  
  268. 'cross_sell_ids' => get_post_meta( $id, '_crosssell_ids', true ),  
  269. 'purchase_note' => get_post_meta( $id, '_purchase_note', true ),  
  270. 'default_attributes' => get_post_meta( $id, '_default_attributes', true ),  
  271. 'category_ids' => $this->get_term_ids( $product, 'product_cat' ),  
  272. 'tag_ids' => $this->get_term_ids( $product, 'product_tag' ),  
  273. 'shipping_class_id' => current( $this->get_term_ids( $product, 'product_shipping_class' ) ),  
  274. 'virtual' => get_post_meta( $id, '_virtual', true ),  
  275. 'downloadable' => get_post_meta( $id, '_downloadable', true ),  
  276. 'gallery_image_ids' => array_filter( explode( ', ', get_post_meta( $id, '_product_image_gallery', true ) ) ),  
  277. 'download_limit' => get_post_meta( $id, '_download_limit', true ),  
  278. 'download_expiry' => get_post_meta( $id, '_download_expiry', true ),  
  279. 'image_id' => get_post_thumbnail_id( $id ),  
  280. ) ); 
  281.  
  282. /** 
  283. * Read extra data associated with the product, like button text or product URL for external products. 
  284. * @param WC_Product 
  285. * @since 3.0.0 
  286. */ 
  287. protected function read_extra_data( &$product ) { 
  288. foreach ( $product->get_extra_data_keys() as $key ) { 
  289. $function = 'set_' . $key; 
  290. if ( is_callable( array( $product, $function ) ) ) { 
  291. $product->{$function}( get_post_meta( $product->get_id(), '_' . $key, true ) ); 
  292.  
  293. /** 
  294. * Convert visibility terms to props. 
  295. * Catalog visibility valid values are 'visible', 'catalog', 'search', and 'hidden'. 
  296. * @param WC_Product 
  297. * @since 3.0.0 
  298. */ 
  299. protected function read_visibility( &$product ) { 
  300. $terms = get_the_terms( $product->get_id(), 'product_visibility' ); 
  301. $term_names = is_array( $terms ) ? wp_list_pluck( $terms, 'name' ) : array(); 
  302. $featured = in_array( 'featured', $term_names ); 
  303. $exclude_search = in_array( 'exclude-from-search', $term_names ); 
  304. $exclude_catalog = in_array( 'exclude-from-catalog', $term_names ); 
  305.  
  306. if ( $exclude_search && $exclude_catalog ) { 
  307. $catalog_visibility = 'hidden'; 
  308. } elseif ( $exclude_search ) { 
  309. $catalog_visibility = 'catalog'; 
  310. } elseif ( $exclude_catalog ) { 
  311. $catalog_visibility = 'search'; 
  312. } else { 
  313. $catalog_visibility = 'visible'; 
  314.  
  315. $product->set_props( array( 
  316. 'featured' => $featured,  
  317. 'catalog_visibility' => $catalog_visibility,  
  318. ) ); 
  319.  
  320. /** 
  321. * Read attributes from post meta. 
  322. * @param WC_Product 
  323. * @since 3.0.0 
  324. */ 
  325. protected function read_attributes( &$product ) { 
  326. $meta_values = get_post_meta( $product->get_id(), '_product_attributes', true ); 
  327.  
  328. if ( ! empty( $meta_values ) && is_array( $meta_values ) ) { 
  329. $attributes = array(); 
  330. foreach ( $meta_values as $meta_value ) { 
  331. $id = 0; 
  332. $meta_value = array_merge( array( 
  333. 'name' => '',  
  334. 'value' => '',  
  335. 'position' => 0,  
  336. 'is_visible' => 0,  
  337. 'is_variation' => 0,  
  338. 'is_taxonomy' => 0,  
  339. ), (array) $meta_value ); 
  340.  
  341. // Check if is a taxonomy attribute. 
  342. if ( ! empty( $meta_value['is_taxonomy'] ) ) { 
  343. if ( ! taxonomy_exists( $meta_value['name'] ) ) { 
  344. continue; 
  345. $id = wc_attribute_taxonomy_id_by_name( $meta_value['name'] ); 
  346. $options = wc_get_object_terms( $product->get_id(), $meta_value['name'], 'term_id' ); 
  347. } else { 
  348. $options = wc_get_text_attributes( $meta_value['value'] ); 
  349.  
  350. $attribute = new WC_Product_Attribute(); 
  351.  
  352. $attribute->set_id( $id ); 
  353. $attribute->set_name( $meta_value['name'] ); 
  354. $attribute->set_options( $options ); 
  355. $attribute->set_position( $meta_value['position'] ); 
  356. $attribute->set_visible( $meta_value['is_visible'] ); 
  357. $attribute->set_variation( $meta_value['is_variation'] ); 
  358. $attributes[] = $attribute; 
  359. $product->set_attributes( $attributes ); 
  360.  
  361. /** 
  362. * Read downloads from post meta. 
  363. * @param WC_Product 
  364. * @since 3.0.0 
  365. */ 
  366. protected function read_downloads( &$product ) { 
  367. $meta_values = array_filter( (array) get_post_meta( $product->get_id(), '_downloadable_files', true ) ); 
  368.  
  369. if ( $meta_values ) { 
  370. $downloads = array(); 
  371. foreach ( $meta_values as $key => $value ) { 
  372. if ( ! isset( $value['name'], $value['file'] ) ) { 
  373. continue; 
  374. $download = new WC_Product_Download(); 
  375. $download->set_id( $key ); 
  376. $download->set_name( $value['name'] ? $value['name'] : wc_get_filename_from_url( $value['file'] ) ); 
  377. $download->set_file( apply_filters( 'woocommerce_file_download_path', $value['file'], $product, $key ) ); 
  378. $downloads[] = $download; 
  379. $product->set_downloads( $downloads ); 
  380.  
  381. /** 
  382. * Helper method that updates all the post meta for a product based on it's settings in the WC_Product class. 
  383. * @param WC_Product 
  384. * @param bool Force update. Used during create. 
  385. * @since 3.0.0 
  386. */ 
  387. protected function update_post_meta( &$product, $force = false ) { 
  388. $meta_key_to_props = array( 
  389. '_sku' => 'sku',  
  390. '_regular_price' => 'regular_price',  
  391. '_sale_price' => 'sale_price',  
  392. '_sale_price_dates_from' => 'date_on_sale_from',  
  393. '_sale_price_dates_to' => 'date_on_sale_to',  
  394. 'total_sales' => 'total_sales',  
  395. '_tax_status' => 'tax_status',  
  396. '_tax_class' => 'tax_class',  
  397. '_manage_stock' => 'manage_stock',  
  398. '_backorders' => 'backorders',  
  399. '_sold_individually' => 'sold_individually',  
  400. '_weight' => 'weight',  
  401. '_length' => 'length',  
  402. '_width' => 'width',  
  403. '_height' => 'height',  
  404. '_upsell_ids' => 'upsell_ids',  
  405. '_crosssell_ids' => 'cross_sell_ids',  
  406. '_purchase_note' => 'purchase_note',  
  407. '_default_attributes' => 'default_attributes',  
  408. '_virtual' => 'virtual',  
  409. '_downloadable' => 'downloadable',  
  410. '_product_image_gallery' => 'gallery_image_ids',  
  411. '_download_limit' => 'download_limit',  
  412. '_download_expiry' => 'download_expiry',  
  413. '_thumbnail_id' => 'image_id',  
  414. '_stock' => 'stock_quantity',  
  415. '_stock_status' => 'stock_status',  
  416. '_wc_average_rating' => 'average_rating',  
  417. '_wc_rating_count' => 'rating_counts',  
  418. '_wc_review_count' => 'review_count',  
  419. ); 
  420.  
  421. // Make sure to take extra data (like product url or text for external products) into account. 
  422. $extra_data_keys = $product->get_extra_data_keys(); 
  423.  
  424. foreach ( $extra_data_keys as $key ) { 
  425. $meta_key_to_props[ '_' . $key ] = $key; 
  426.  
  427. $props_to_update = $force ? $meta_key_to_props : $this->get_props_to_update( $product, $meta_key_to_props ); 
  428.  
  429. foreach ( $props_to_update as $meta_key => $prop ) { 
  430. $value = $product->{"get_$prop"}( 'edit' ); 
  431. switch ( $prop ) { 
  432. case 'virtual' : 
  433. case 'downloadable' : 
  434. case 'manage_stock' : 
  435. case 'sold_individually' : 
  436. $updated = update_post_meta( $product->get_id(), $meta_key, wc_bool_to_string( $value ) ); 
  437. break; 
  438. case 'gallery_image_ids' : 
  439. $updated = update_post_meta( $product->get_id(), $meta_key, implode( ', ', $value ) ); 
  440. break; 
  441. case 'image_id' : 
  442. if ( ! empty( $value ) ) { 
  443. set_post_thumbnail( $product->get_id(), $value ); 
  444. } else { 
  445. delete_post_meta( $product->get_id(), '_thumbnail_id' ); 
  446. $updated = true; 
  447. break; 
  448. case 'date_on_sale_from' : 
  449. case 'date_on_sale_to' : 
  450. $updated = update_post_meta( $product->get_id(), $meta_key, $value ? $value->getTimestamp() : '' ); 
  451. break; 
  452. default : 
  453. $updated = update_post_meta( $product->get_id(), $meta_key, $value ); 
  454. break; 
  455. if ( $updated ) { 
  456. $this->updated_props[] = $prop; 
  457.  
  458. // Update extra data associated with the product like button text or product URL for external products. 
  459. if ( ! $this->extra_data_saved ) { 
  460. foreach ( $extra_data_keys as $key ) { 
  461. if ( ! array_key_exists( $key, $props_to_update ) ) { 
  462. continue; 
  463. $function = 'get_' . $key; 
  464. if ( is_callable( array( $product, $function ) ) ) { 
  465. if ( update_post_meta( $product->get_id(), '_' . $key, $product->{$function}( 'edit' ) ) ) { 
  466. $this->updated_props[] = $key; 
  467.  
  468. if ( $this->update_downloads( $product, $force ) ) { 
  469. $this->updated_props[] = 'downloads'; 
  470.  
  471. /** 
  472. * Handle updated meta props after updating meta data. 
  473. * @since 3.0.0 
  474. * @param WC_Product $product 
  475. */ 
  476. protected function handle_updated_props( &$product ) { 
  477. if ( in_array( 'date_on_sale_from', $this->updated_props ) || in_array( 'date_on_sale_to', $this->updated_props ) || in_array( 'regular_price', $this->updated_props ) || in_array( 'sale_price', $this->updated_props ) ) { 
  478. if ( $product->is_on_sale( 'edit' ) ) { 
  479. update_post_meta( $product->get_id(), '_price', $product->get_sale_price( 'edit' ) ); 
  480. $product->set_price( $product->get_sale_price( 'edit' ) ); 
  481. } else { 
  482. update_post_meta( $product->get_id(), '_price', $product->get_regular_price( 'edit' ) ); 
  483. $product->set_price( $product->get_regular_price( 'edit' ) ); 
  484.  
  485. if ( in_array( 'stock_quantity', $this->updated_props ) ) { 
  486. do_action( $product->is_type( 'variation' ) ? 'woocommerce_variation_set_stock' : 'woocommerce_product_set_stock' , $product ); 
  487.  
  488. if ( in_array( 'stock_status', $this->updated_props ) ) { 
  489. do_action( $product->is_type( 'variation' ) ? 'woocommerce_variation_set_stock_status' : 'woocommerce_product_set_stock_status' , $product->get_id(), $product->get_stock_status(), $product ); 
  490.  
  491. // Trigger action so 3rd parties can deal with updated props. 
  492. do_action( 'woocommerce_product_object_updated_props', $product, $this->updated_props ); 
  493.  
  494. // After handling, we can reset the props array. 
  495. $this->updated_props = array(); 
  496.  
  497. /** 
  498. * For all stored terms in all taxonomies, save them to the DB. 
  499. * @param WC_Product 
  500. * @param bool Force update. Used during create. 
  501. * @since 3.0.0 
  502. */ 
  503. protected function update_terms( &$product, $force = false ) { 
  504. $changes = $product->get_changes(); 
  505.  
  506. if ( $force || array_key_exists( 'category_ids', $changes ) ) { 
  507. wp_set_post_terms( $product->get_id(), $product->get_category_ids( 'edit' ), 'product_cat', false ); 
  508. if ( $force || array_key_exists( 'tag_ids', $changes ) ) { 
  509. wp_set_post_terms( $product->get_id(), $product->get_tag_ids( 'edit' ), 'product_tag', false ); 
  510. if ( $force || array_key_exists( 'shipping_class_id', $changes ) ) { 
  511. wp_set_post_terms( $product->get_id(), array( $product->get_shipping_class_id( 'edit' ) ), 'product_shipping_class', false ); 
  512.  
  513. /** 
  514. * Update visibility terms based on props. 
  515. * @since 3.0.0 
  516. * @param bool Force update. Used during create. 
  517. * @param WC_Product 
  518. */ 
  519. protected function update_visibility( &$product, $force = false ) { 
  520. $changes = $product->get_changes(); 
  521.  
  522. if ( $force || array_intersect( array( 'featured', 'stock_status', 'average_rating', 'catalog_visibility' ), array_keys( $changes ) ) ) { 
  523. $terms = array(); 
  524.  
  525. if ( $product->get_featured() ) { 
  526. $terms[] = 'featured'; 
  527.  
  528. if ( 'outofstock' === $product->get_stock_status() ) { 
  529. $terms[] = 'outofstock'; 
  530.  
  531. $rating = max( array( 5, min( array( 1, round( $product->get_average_rating(), 0 ) ) ) ) ); 
  532. $terms[] = 'rated-' . $rating; 
  533.  
  534. switch ( $product->get_catalog_visibility() ) { 
  535. case 'hidden' : 
  536. $terms[] = 'exclude-from-search'; 
  537. $terms[] = 'exclude-from-catalog'; 
  538. break; 
  539. case 'catalog' : 
  540. $terms[] = 'exclude-from-search'; 
  541. break; 
  542. case 'search' : 
  543. $terms[] = 'exclude-from-catalog'; 
  544. break; 
  545.  
  546. if ( ! is_wp_error( wp_set_post_terms( $product->get_id(), $terms, 'product_visibility', false ) ) ) { 
  547. delete_transient( 'wc_featured_products' ); 
  548. do_action( 'woocommerce_product_set_visibility', $product->get_id(), $product->get_catalog_visibility() ); 
  549.  
  550. /** 
  551. * Update attributes which are a mix of terms and meta data. 
  552. * @param WC_Product 
  553. * @param bool Force update. Used during create. 
  554. * @since 3.0.0 
  555. */ 
  556. protected function update_attributes( &$product, $force = false ) { 
  557. $changes = $product->get_changes(); 
  558.  
  559. if ( $force || array_key_exists( 'attributes', $changes ) ) { 
  560. $attributes = $product->get_attributes(); 
  561. $meta_values = array(); 
  562.  
  563. if ( $attributes ) { 
  564. foreach ( $attributes as $attribute_key => $attribute ) { 
  565. $value = ''; 
  566.  
  567. if ( is_null( $attribute ) ) { 
  568. if ( taxonomy_exists( $attribute_key ) ) { 
  569. // Handle attributes that have been unset. 
  570. wp_set_object_terms( $product->get_id(), array(), $attribute_key ); 
  571. continue; 
  572.  
  573. } elseif ( $attribute->is_taxonomy() ) { 
  574. wp_set_object_terms( $product->get_id(), wp_list_pluck( $attribute->get_terms(), 'term_id' ), $attribute->get_name() ); 
  575. } else { 
  576. $value = wc_implode_text_attributes( $attribute->get_options() ); 
  577.  
  578. // Store in format WC uses in meta. 
  579. $meta_values[ $attribute_key ] = array( 
  580. 'name' => $attribute->get_name(),  
  581. 'value' => $value,  
  582. 'position' => $attribute->get_position(),  
  583. 'is_visible' => $attribute->get_visible() ? 1 : 0,  
  584. 'is_variation' => $attribute->get_variation() ? 1 : 0,  
  585. 'is_taxonomy' => $attribute->is_taxonomy() ? 1 : 0,  
  586. ); 
  587. update_post_meta( $product->get_id(), '_product_attributes', $meta_values ); 
  588.  
  589. /** 
  590. * Update downloads. 
  591. * @since 3.0.0 
  592. * @param WC_Product $product 
  593. * @param bool Force update. Used during create. 
  594. * @return bool If updated or not. 
  595. */ 
  596. protected function update_downloads( &$product, $force = false ) { 
  597. $changes = $product->get_changes(); 
  598.  
  599. if ( $force || array_key_exists( 'downloads', $changes ) ) { 
  600. $downloads = $product->get_downloads(); 
  601. $meta_values = array(); 
  602.  
  603. if ( $downloads ) { 
  604. foreach ( $downloads as $key => $download ) { 
  605. // Store in format WC uses in meta. 
  606. $meta_values[ $key ] = $download->get_data(); 
  607.  
  608. if ( $product->is_type( 'variation' ) ) { 
  609. do_action( 'woocommerce_process_product_file_download_paths', $product->get_parent_id(), $product->get_id(), $downloads ); 
  610. } else { 
  611. do_action( 'woocommerce_process_product_file_download_paths', $product->get_id(), 0, $downloads ); 
  612.  
  613. return update_post_meta( $product->get_id(), '_downloadable_files', $meta_values ); 
  614. return false; 
  615.  
  616. /** 
  617. * Make sure we store the product type and version (to track data changes). 
  618. * @param WC_Product 
  619. * @since 3.0.0 
  620. */ 
  621. protected function update_version_and_type( &$product ) { 
  622. $old_type = WC_Product_Factory::get_product_type( $product->get_id() ); 
  623. $new_type = $product->get_type(); 
  624.  
  625. wp_set_object_terms( $product->get_id(), $new_type, 'product_type' ); 
  626. update_post_meta( $product->get_id(), '_product_version', WC_VERSION ); 
  627.  
  628. // Action for the transition. 
  629. if ( $old_type !== $new_type ) { 
  630. do_action( 'woocommerce_product_type_changed', $product, $old_type, $new_type ); 
  631.  
  632. /** 
  633. * Clear any caches. 
  634. * @param WC_Product 
  635. * @since 3.0.0 
  636. */ 
  637. protected function clear_caches( &$product ) { 
  638. wc_delete_product_transients( $product->get_id() ); 
  639.  
  640. /** 
  641. |-------------------------------------------------------------------------- 
  642. | wc-product-functions.php methods 
  643. |-------------------------------------------------------------------------- 
  644. */ 
  645.  
  646. /** 
  647. * Returns an array of on sale products, as an array of objects with an 
  648. * ID and parent_id present. Example: $return[0]->id, $return[0]->parent_id. 
  649. * @return array 
  650. * @since 3.0.0 
  651. */ 
  652. public function get_on_sale_products() { 
  653. global $wpdb; 
  654.  
  655. $decimals = absint( wc_get_price_decimals() ); 
  656.  
  657. return $wpdb->get_results( " 
  658. SELECT post.ID as id, post.post_parent as parent_id FROM `$wpdb->posts` AS post 
  659. LEFT JOIN `$wpdb->postmeta` AS meta ON post.ID = meta.post_id 
  660. LEFT JOIN `$wpdb->postmeta` AS meta2 ON post.ID = meta2.post_id 
  661. WHERE post.post_type IN ( 'product', 'product_variation' ) 
  662. AND post.post_status = 'publish' 
  663. AND meta.meta_key = '_sale_price' 
  664. AND meta2.meta_key = '_price' 
  665. AND CAST( meta.meta_value AS DECIMAL ) >= 0 
  666. AND CAST( meta.meta_value AS CHAR ) != '' 
  667. AND CAST( meta.meta_value AS DECIMAL( 10, {$decimals} ) ) = CAST( meta2.meta_value AS DECIMAL( 10, {$decimals} ) ) 
  668. GROUP BY post.ID; 
  669. " ); 
  670.  
  671. /** 
  672. * Returns a list of product IDs ( id as key => parent as value) that are 
  673. * featured. Uses get_posts instead of wc_get_products since we want 
  674. * some extra meta queries and ALL products (posts_per_page = -1). 
  675. * @return array 
  676. * @since 3.0.0 
  677. */ 
  678. public function get_featured_product_ids() { 
  679. $product_visibility_term_ids = wc_get_product_visibility_term_ids(); 
  680.  
  681. return get_posts( array( 
  682. 'post_type' => array( 'product', 'product_variation' ),  
  683. 'posts_per_page' => -1,  
  684. 'post_status' => 'publish',  
  685. 'tax_query' => array( 
  686. 'relation' => 'AND',  
  687. array( 
  688. 'taxonomy' => 'product_visibility',  
  689. 'field' => 'term_taxonomy_id',  
  690. 'terms' => array( $product_visibility_term_ids['featured'] ),  
  691. ),  
  692. array( 
  693. 'taxonomy' => 'product_visibility',  
  694. 'field' => 'term_taxonomy_id',  
  695. 'terms' => array( $product_visibility_term_ids['exclude-from-catalog'] ),  
  696. 'operator' => 'NOT IN',  
  697. ),  
  698. ),  
  699. 'fields' => 'id=>parent',  
  700. ) ); 
  701.  
  702. /** 
  703. * Check if product sku is found for any other product IDs. 
  704. * @since 3.0.0 
  705. * @param int $product_id 
  706. * @param string $sku Will be slashed to work around https://core.trac.wordpress.org/ticket/27421 
  707. * @return bool 
  708. */ 
  709. public function is_existing_sku( $product_id, $sku ) { 
  710. global $wpdb; 
  711. return $wpdb->get_var( $wpdb->prepare( " 
  712. SELECT $wpdb->posts.ID 
  713. FROM $wpdb->posts 
  714. LEFT JOIN $wpdb->postmeta ON ( $wpdb->posts.ID = $wpdb->postmeta.post_id ) 
  715. WHERE $wpdb->posts.post_type IN ( 'product', 'product_variation' ) 
  716. AND $wpdb->posts.post_status != 'trash' 
  717. AND $wpdb->postmeta.meta_key = '_sku' AND $wpdb->postmeta.meta_value = '%s' 
  718. AND $wpdb->postmeta.post_id <> %d LIMIT 1 
  719. ", wp_slash( $sku ), $product_id ) ); 
  720.  
  721. /** 
  722. * Return product ID based on SKU. 
  723. * @since 3.0.0 
  724. * @param string $sku 
  725. * @return int 
  726. */ 
  727. public function get_product_id_by_sku( $sku ) { 
  728. global $wpdb; 
  729. return $wpdb->get_var( $wpdb->prepare( " 
  730. SELECT posts.ID 
  731. FROM $wpdb->posts AS posts 
  732. LEFT JOIN $wpdb->postmeta AS postmeta ON ( posts.ID = postmeta.post_id ) 
  733. WHERE posts.post_type IN ( 'product', 'product_variation' ) 
  734. AND posts.post_status != 'trash' 
  735. AND postmeta.meta_key = '_sku' 
  736. AND postmeta.meta_value = '%s' 
  737. LIMIT 1 
  738. ", $sku ) ); 
  739.  
  740. /** 
  741. * Returns an array of IDs of products that have sales starting soon. 
  742. * @since 3.0.0 
  743. * @return array 
  744. */ 
  745. public function get_starting_sales() { 
  746. global $wpdb; 
  747. return $wpdb->get_col( $wpdb->prepare( " 
  748. SELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta 
  749. LEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id 
  750. LEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id 
  751. WHERE postmeta.meta_key = '_sale_price_dates_from' 
  752. AND postmeta_2.meta_key = '_price' 
  753. AND postmeta_3.meta_key = '_sale_price' 
  754. AND postmeta.meta_value > 0 
  755. AND postmeta.meta_value < %s 
  756. AND postmeta_2.meta_value != postmeta_3.meta_value 
  757. ", current_time( 'timestamp', true ) ) ); 
  758.  
  759. /** 
  760. * Returns an array of IDs of products that have sales which are due to end. 
  761. * @since 3.0.0 
  762. * @return array 
  763. */ 
  764. public function get_ending_sales() { 
  765. global $wpdb; 
  766. return $wpdb->get_col( $wpdb->prepare( " 
  767. SELECT postmeta.post_id FROM {$wpdb->postmeta} as postmeta 
  768. LEFT JOIN {$wpdb->postmeta} as postmeta_2 ON postmeta.post_id = postmeta_2.post_id 
  769. LEFT JOIN {$wpdb->postmeta} as postmeta_3 ON postmeta.post_id = postmeta_3.post_id 
  770. WHERE postmeta.meta_key = '_sale_price_dates_to' 
  771. AND postmeta_2.meta_key = '_price' 
  772. AND postmeta_3.meta_key = '_regular_price' 
  773. AND postmeta.meta_value > 0 
  774. AND postmeta.meta_value < %s 
  775. AND postmeta_2.meta_value != postmeta_3.meta_value 
  776. ", current_time( 'timestamp', true ) ) ); 
  777.  
  778. /** 
  779. * Find a matching (enabled) variation within a variable product. 
  780. * @since 3.0.0 
  781. * @param WC_Product $product Variable product. 
  782. * @param array $match_attributes Array of attributes we want to try to match. 
  783. * @return int Matching variation ID or 0. 
  784. */ 
  785. public function find_matching_product_variation( $product, $match_attributes = array() ) { 
  786. $query_args = array( 
  787. 'post_parent' => $product->get_id(),  
  788. 'post_type' => 'product_variation',  
  789. 'orderby' => 'menu_order',  
  790. 'order' => 'ASC',  
  791. 'fields' => 'ids',  
  792. 'post_status' => 'publish',  
  793. 'numberposts' => 1,  
  794. 'meta_query' => array(),  
  795. ); 
  796.  
  797. // Allow large queries in case user has many variations or attributes. 
  798. $GLOBALS['wpdb']->query( 'SET SESSION SQL_BIG_SELECTS=1' ); 
  799.  
  800. foreach ( $product->get_attributes() as $attribute ) { 
  801. if ( ! $attribute->get_variation() ) { 
  802. continue; 
  803.  
  804. $attribute_field_name = 'attribute_' . sanitize_title( $attribute->get_name() ); 
  805.  
  806. if ( ! isset( $match_attributes[ $attribute_field_name ] ) ) { 
  807. return 0; 
  808.  
  809. // Note not wc_clean here to prevent removal of entities. 
  810. $value = $match_attributes[ $attribute_field_name ]; 
  811.  
  812. $query_args['meta_query'][] = array( 
  813. 'relation' => 'OR',  
  814. array( 
  815. 'key' => $attribute_field_name,  
  816. 'value' => array( '', $value ),  
  817. 'compare' => 'IN',  
  818. ),  
  819. array( 
  820. 'key' => $attribute_field_name,  
  821. 'compare' => 'NOT EXISTS',  
  822. ); 
  823.  
  824. $variations = get_posts( $query_args ); 
  825.  
  826. if ( $variations && ! is_wp_error( $variations ) ) { 
  827. return current( $variations ); 
  828. } elseif ( version_compare( get_post_meta( $product->get_id(), '_product_version', true ), '2.4.0', '<' ) ) { 
  829. /** 
  830. * Pre 2.4 handling where 'slugs' were saved instead of the full text attribute. 
  831. * Fallback is here because there are cases where data will be 'synced' but the product version will remain the same. 
  832. */ 
  833. return ( array_map( 'sanitize_title', $match_attributes ) === $match_attributes ) ? 0 : $this->find_matching_product_variation( $product, array_map( 'sanitize_title', $match_attributes ) ); 
  834.  
  835. return 0; 
  836.  
  837. /** 
  838. * Make sure all variations have a sort order set so they can be reordered correctly. 
  839. * @param int $parent_id 
  840. */ 
  841. public function sort_all_product_variations( $parent_id ) { 
  842. global $wpdb; 
  843. $ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_type='product_variation' AND post_parent=%d AND post_status='publish' ORDER BY menu_order ASC, ID ASC", $parent_id ) ); 
  844. $index = 0; 
  845.  
  846. foreach ( $ids as $id ) { 
  847. $wpdb->update( $wpdb->posts, array( 'menu_order' => ( $index ++ ) ), array( 'ID' => absint( $id ) ) ); 
  848.  
  849. /** 
  850. * Return a list of related products (using data like categories and IDs). 
  851. * @since 3.0.0 
  852. * @param array $cats_array List of categories IDs. 
  853. * @param array $tags_array List of tags IDs. 
  854. * @param array $exclude_ids Excluded IDs. 
  855. * @param int $limit Limit of results. 
  856. * @param int $product_id 
  857. * @return array 
  858. */ 
  859. public function get_related_products( $cats_array, $tags_array, $exclude_ids, $limit, $product_id ) { 
  860. global $wpdb; 
  861. return $wpdb->get_col( implode( ' ', apply_filters( 'woocommerce_product_related_posts_query', $this->get_related_products_query( $cats_array, $tags_array, $exclude_ids, $limit + 10 ), $product_id ) ) ); 
  862.  
  863. /** 
  864. * Builds the related posts query. 
  865. * @since 3.0.0 
  866. * @param array $cats_array List of categories IDs. 
  867. * @param array $tags_array List of tags IDs. 
  868. * @param array $exclude_ids Excluded IDs. 
  869. * @param int $limit Limit of results. 
  870. * @return string 
  871. */ 
  872. public function get_related_products_query( $cats_array, $tags_array, $exclude_ids, $limit ) { 
  873. global $wpdb; 
  874.  
  875. $include_term_ids = array_merge( $cats_array, $tags_array ); 
  876. $exclude_term_ids = array(); 
  877. $product_visibility_term_ids = wc_get_product_visibility_term_ids(); 
  878.  
  879. if ( $product_visibility_term_ids['exclude-from-catalog'] ) { 
  880. $exclude_term_ids[] = $product_visibility_term_ids['exclude-from-catalog']; 
  881.  
  882. if ( 'yes' === get_option( 'woocommerce_hide_out_of_stock_items' ) && $product_visibility_term_ids['outofstock'] ) { 
  883. $exclude_term_ids[] = $product_visibility_term_ids['outofstock']; 
  884.  
  885. $query = array( 
  886. 'fields' => " 
  887. SELECT DISTINCT ID FROM {$wpdb->posts} p 
  888. ",  
  889. 'join' => '',  
  890. 'where' => " 
  891. WHERE 1=1 
  892. AND p.post_status = 'publish' 
  893. AND p.post_type = 'product' 
  894.  
  895. ",  
  896. 'limits' => " 
  897. LIMIT " . absint( $limit ) . " 
  898. ",  
  899. ); 
  900.  
  901. if ( count( $exclude_term_ids ) ) { 
  902. $query['join'] .= " LEFT JOIN ( SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( " . implode( ', ', array_map( 'absint', $exclude_term_ids ) ) . " ) ) AS exclude_join ON exclude_join.object_id = p.ID"; 
  903. $query['where'] .= " AND exclude_join.object_id IS NULL"; 
  904.  
  905. if ( count( $include_term_ids ) ) { 
  906. $query['join'] .= " INNER JOIN ( SELECT object_id FROM {$wpdb->term_relationships} INNER JOIN {$wpdb->term_taxonomy} using( term_taxonomy_id ) WHERE term_id IN ( " . implode( ', ', array_map( 'absint', $include_term_ids ) ) . " ) ) AS include_join ON include_join.object_id = p.ID"; 
  907.  
  908. if ( count( $exclude_ids ) ) { 
  909. $query['where'] .= " AND p.ID NOT IN ( " . implode( ', ', array_map( 'absint', $exclude_ids ) ) . " )"; 
  910.  
  911. return $query; 
  912.  
  913. /** 
  914. * Update a product's stock amount directly. 
  915. * Uses queries rather than update_post_meta so we can do this in one query (to avoid stock issues). 
  916. * @since 3.0.0 this supports set, increase and decrease. 
  917. * @param int 
  918. * @param int|null $stock_quantity 
  919. * @param string $operation set, increase and decrease. 
  920. */ 
  921. public function update_product_stock( $product_id_with_stock, $stock_quantity = null, $operation = 'set' ) { 
  922. global $wpdb; 
  923. add_post_meta( $product_id_with_stock, '_stock', 0, true ); 
  924.  
  925. // Update stock in DB directly 
  926. switch ( $operation ) { 
  927. case 'increase' : 
  928. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = meta_value + %f WHERE post_id = %d AND meta_key='_stock'", $stock_quantity, $product_id_with_stock ) ); 
  929. break; 
  930. case 'decrease' : 
  931. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = meta_value - %f WHERE post_id = %d AND meta_key='_stock'", $stock_quantity, $product_id_with_stock ) ); 
  932. break; 
  933. default : 
  934. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = %f WHERE post_id = %d AND meta_key='_stock'", $stock_quantity, $product_id_with_stock ) ); 
  935. break; 
  936.  
  937. wp_cache_delete( $product_id_with_stock, 'post_meta' ); 
  938.  
  939. /** 
  940. * Update a product's sale count directly. 
  941. * Uses queries rather than update_post_meta so we can do this in one query for performance. 
  942. * @since 3.0.0 this supports set, increase and decrease. 
  943. * @param int 
  944. * @param int|null $quantity 
  945. * @param string $operation set, increase and decrease. 
  946. */ 
  947. public function update_product_sales( $product_id, $quantity = null, $operation = 'set' ) { 
  948. global $wpdb; 
  949. add_post_meta( $product_id, 'total_sales', 0, true ); 
  950.  
  951. // Update stock in DB directly 
  952. switch ( $operation ) { 
  953. case 'increase' : 
  954. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = meta_value + %f WHERE post_id = %d AND meta_key='total_sales'", $quantity, $product_id ) ); 
  955. break; 
  956. case 'decrease' : 
  957. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = meta_value - %f WHERE post_id = %d AND meta_key='total_sales'", $quantity, $product_id ) ); 
  958. break; 
  959. default : 
  960. $wpdb->query( $wpdb->prepare( "UPDATE {$wpdb->postmeta} SET meta_value = %f WHERE post_id = %d AND meta_key='total_sales'", $quantity, $product_id ) ); 
  961. break; 
  962.  
  963. wp_cache_delete( $product_id, 'post_meta' ); 
  964.  
  965. /** 
  966. * Update a products average rating meta. 
  967. * @since 3.0.0 
  968. * @param WC_Product $product 
  969. */ 
  970. public function update_average_rating( $product ) { 
  971. update_post_meta( $product->get_id(), '_wc_average_rating', $product->get_average_rating( 'edit' ) ); 
  972.  
  973. /** 
  974. * Update a products review count meta. 
  975. * @since 3.0.0 
  976. * @param WC_Product $product 
  977. */ 
  978. public function update_review_count( $product ) { 
  979. update_post_meta( $product->get_id(), '_wc_review_count', $product->get_review_count( 'edit' ) ); 
  980.  
  981. /** 
  982. * Update a products rating counts. 
  983. * @since 3.0.0 
  984. * @param WC_Product $product 
  985. */ 
  986. public function update_rating_counts( $product ) { 
  987. update_post_meta( $product->get_id(), '_wc_rating_count', $product->get_rating_counts( 'edit' ) ); 
  988.  
  989. /** 
  990. * Get shipping class ID by slug. 
  991. * @since 3.0.0 
  992. * @param $slug string 
  993. * @return int|false 
  994. */ 
  995. public function get_shipping_class_id_by_slug( $slug ) { 
  996. $shipping_class_term = get_term_by( 'slug', $slug, 'product_shipping_class' ); 
  997. if ( $shipping_class_term ) { 
  998. return $shipping_class_term->term_id; 
  999. } else { 
  1000. return false; 
  1001.  
  1002. /** 
  1003. * Returns an array of products. 
  1004. * @param array $args @see wc_get_products 
  1005. * @return array 
  1006. */ 
  1007. public function get_products( $args = array() ) { 
  1008. /** 
  1009. * Generate WP_Query args. 
  1010. */ 
  1011. $wp_query_args = array( 
  1012. 'post_type' => 'variation' === $args['type'] ? 'product_variation' : 'product',  
  1013. 'post_status' => $args['status'],  
  1014. 'posts_per_page' => $args['limit'],  
  1015. 'meta_query' => array(),  
  1016. 'orderby' => $args['orderby'],  
  1017. 'order' => $args['order'],  
  1018. 'tax_query' => array(),  
  1019. ); 
  1020. // Do not load unnecessary post data if the user only wants IDs. 
  1021. if ( 'ids' === $args['return'] ) { 
  1022. $wp_query_args['fields'] = 'ids'; 
  1023.  
  1024. if ( 'variation' !== $args['type'] ) { 
  1025. $wp_query_args['tax_query'][] = array( 
  1026. 'taxonomy' => 'product_type',  
  1027. 'field' => 'slug',  
  1028. 'terms' => $args['type'],  
  1029. ); 
  1030.  
  1031. if ( ! empty( $args['sku'] ) ) { 
  1032. $wp_query_args['meta_query'][] = array( 
  1033. 'key' => '_sku',  
  1034. 'value' => $args['sku'],  
  1035. 'compare' => 'LIKE',  
  1036. ); 
  1037.  
  1038. if ( ! empty( $args['category'] ) ) { 
  1039. $wp_query_args['tax_query'][] = array( 
  1040. 'taxonomy' => 'product_cat',  
  1041. 'field' => 'slug',  
  1042. 'terms' => $args['category'],  
  1043. ); 
  1044.  
  1045. if ( ! empty( $args['tag'] ) ) { 
  1046. $wp_query_args['tax_query'][] = array( 
  1047. 'taxonomy' => 'product_tag',  
  1048. 'field' => 'slug',  
  1049. 'terms' => $args['tag'],  
  1050. ); 
  1051.  
  1052. if ( ! empty( $args['shipping_class'] ) ) { 
  1053. $wp_query_args['tax_query'][] = array( 
  1054. 'taxonomy' => 'product_shipping_class',  
  1055. 'field' => 'slug',  
  1056. 'terms' => $args['shipping_class'],  
  1057. ); 
  1058.  
  1059. if ( ! is_null( $args['parent'] ) ) { 
  1060. $wp_query_args['post_parent'] = absint( $args['parent'] ); 
  1061.  
  1062. if ( ! is_null( $args['offset'] ) ) { 
  1063. $wp_query_args['offset'] = absint( $args['offset'] ); 
  1064. } else { 
  1065. $wp_query_args['paged'] = absint( $args['page'] ); 
  1066.  
  1067. if ( ! empty( $args['include'] ) ) { 
  1068. $wp_query_args['post__in'] = array_map( 'absint', $args['include'] ); 
  1069.  
  1070. if ( ! empty( $args['exclude'] ) ) { 
  1071. $wp_query_args['post__not_in'] = array_map( 'absint', $args['exclude'] ); 
  1072.  
  1073. if ( ! $args['paginate'] ) { 
  1074. $wp_query_args['no_found_rows'] = true; 
  1075.  
  1076. // Get results. 
  1077. $products = new WP_Query( $wp_query_args ); 
  1078.  
  1079. if ( 'objects' === $args['return'] ) { 
  1080. $return = array_filter( array_map( 'wc_get_product', $products->posts ) ); 
  1081. } else { 
  1082. $return = $products->posts; 
  1083.  
  1084. if ( $args['paginate'] ) { 
  1085. return (object) array( 
  1086. 'products' => $return,  
  1087. 'total' => $products->found_posts,  
  1088. 'max_num_pages' => $products->max_num_pages,  
  1089. ); 
  1090. } else { 
  1091. return $return; 
  1092.  
  1093. /** 
  1094. * Search product data for a term and return ids. 
  1095. * @param string $term 
  1096. * @param string $type of product 
  1097. * @param bool $include_variations in search or not 
  1098. * @return array of ids 
  1099. */ 
  1100. public function search_products( $term, $type = '', $include_variations = false ) { 
  1101. global $wpdb; 
  1102.  
  1103. $search_fields = array_map( 'wc_clean', apply_filters( 'woocommerce_product_search_fields', array( 
  1104. '_sku',  
  1105. ) ) ); 
  1106. $like_term = '%' . $wpdb->esc_like( $term ) . '%'; 
  1107. $post_types = $include_variations ? array( 'product', 'product_variation' ) : array( 'product' ); 
  1108. $post_statuses = current_user_can( 'edit_private_products' ) ? array( 'private', 'publish' ) : array( 'publish' ); 
  1109. $type_join = ''; 
  1110. $type_where = ''; 
  1111.  
  1112. if ( $type ) { 
  1113. if ( in_array( $type, array( 'virtual', 'downloadable' ) ) ) { 
  1114. $type_join = " LEFT JOIN {$wpdb->postmeta} postmeta_type ON posts.ID = postmeta_type.post_id "; 
  1115. $type_where = " AND ( postmeta_type.meta_key = '_{$type}' AND postmeta_type.meta_value = 'yes' ) "; 
  1116.  
  1117. $product_ids = $wpdb->get_col( 
  1118. $wpdb->prepare( " 
  1119. SELECT DISTINCT posts.ID FROM {$wpdb->posts} posts 
  1120. LEFT JOIN {$wpdb->postmeta} postmeta ON posts.ID = postmeta.post_id 
  1121. $type_join 
  1122. WHERE ( 
  1123. posts.post_title LIKE %s 
  1124. OR posts.post_content LIKE %s 
  1125. OR ( 
  1126. postmeta.meta_key = '_sku' AND postmeta.meta_value LIKE %s 
  1127. AND posts.post_type IN ('" . implode( "', '", $post_types ) . "') 
  1128. AND posts.post_status IN ('" . implode( "', '", $post_statuses ) . "') 
  1129. $type_where 
  1130. ORDER BY posts.post_parent ASC, posts.post_title ASC 
  1131. ",  
  1132. $like_term,  
  1133. $like_term,  
  1134. $like_term 
  1135. ); 
  1136.  
  1137. if ( is_numeric( $term ) ) { 
  1138. $post_id = absint( $term ); 
  1139. $post_type = get_post_type( $post_id ); 
  1140.  
  1141. if ( 'product_variation' === $post_type && $include_variations ) { 
  1142. $product_ids[] = $post_id; 
  1143. } elseif ( 'product' === $post_type ) { 
  1144. $product_ids[] = $post_id; 
  1145.  
  1146. $product_ids[] = wp_get_post_parent_id( $post_id ); 
  1147.  
  1148. return wp_parse_id_list( $product_ids ); 
  1149.  
  1150. /** 
  1151. * Get the product type based on product ID. 
  1152. * @since 3.0.0 
  1153. * @param int $product_id 
  1154. * @return bool|string 
  1155. */ 
  1156. public function get_product_type( $product_id ) { 
  1157. $post_type = get_post_type( $product_id ); 
  1158. if ( 'product_variation' === $post_type ) { 
  1159. return 'variation'; 
  1160. } elseif ( 'product' === $post_type ) { 
  1161. $terms = get_the_terms( $product_id, 'product_type' ); 
  1162. return ! empty( $terms ) ? sanitize_title( current( $terms )->name ) : 'simple'; 
  1163. } else { 
  1164. return false;