WC_Product_Grouped_Data_Store_CPT

WC Grouped Product Data Store: Stored in CPT.

Defined (1)

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

/includes/data-stores/class-wc-product-grouped-data-store-cpt.php  
  1. class WC_Product_Grouped_Data_Store_CPT extends WC_Product_Data_Store_CPT implements WC_Object_Data_Store_Interface { 
  2.  
  3. /** 
  4. * Helper method that updates all the post meta for a grouped product. 
  5. * @param WC_Product 
  6. * @param bool Force update. Used during create. 
  7. * @since 3.0.0 
  8. */ 
  9. protected function update_post_meta( &$product, $force = false ) { 
  10. $meta_key_to_props = array( 
  11. '_children' => 'children',  
  12. ); 
  13.  
  14. $props_to_update = $force ? $meta_key_to_props : $this->get_props_to_update( $product, $meta_key_to_props ); 
  15.  
  16. foreach ( $props_to_update as $meta_key => $prop ) { 
  17. $value = $product->{"get_$prop"}( 'edit' ); 
  18. $updated = update_post_meta( $product->get_id(), $meta_key, $value ); 
  19. if ( $updated ) { 
  20. $this->updated_props[] = $prop; 
  21.  
  22. parent::update_post_meta( $product, $force ); 
  23.  
  24. /** 
  25. * Handle updated meta props after updating meta data. 
  26. * @since 3.0.0 
  27. * @param WC_Product $product 
  28. */ 
  29. protected function handle_updated_props( &$product ) { 
  30. if ( in_array( 'children', $this->updated_props ) ) { 
  31. $child_prices = array(); 
  32. foreach ( $product->get_children( 'edit' ) as $child_id ) { 
  33. $child = wc_get_product( $child_id ); 
  34. if ( $child ) { 
  35. $child_prices[] = $child->get_price(); 
  36. $child_prices = array_filter( $child_prices ); 
  37. delete_post_meta( $product->get_id(), '_price' ); 
  38.  
  39. if ( ! empty( $child_prices ) ) { 
  40. add_post_meta( $product->get_id(), '_price', min( $child_prices ) ); 
  41. add_post_meta( $product->get_id(), '_price', max( $child_prices ) ); 
  42. parent::handle_updated_props( $product ); 
  43.  
  44. /** 
  45. * Sync grouped product prices with children. 
  46. * @since 3.0.0 
  47. * @param WC_Product|int $product 
  48. */ 
  49. public function sync_price( &$product ) { 
  50. global $wpdb; 
  51.  
  52. $children_ids = get_posts( array( 
  53. 'post_parent' => $product->get_id(),  
  54. 'post_type' => 'product',  
  55. 'fields' => 'ids',  
  56. ) ); 
  57. $prices = $children_ids ? array_unique( $wpdb->get_col( "SELECT meta_value FROM $wpdb->postmeta WHERE meta_key = '_price' AND post_id IN ( " . implode( ', ', array_map( 'absint', $children_ids ) ) . " )" ) ) : array(); 
  58.  
  59. delete_post_meta( $product->get_id(), '_price' ); 
  60. delete_transient( 'wc_var_prices_' . $product->get_id() ); 
  61.  
  62. if ( $prices ) { 
  63. sort( $prices ); 
  64. // To allow sorting and filtering by multiple values, we have no choice but to store child prices in this manner. 
  65. foreach ( $prices as $price ) { 
  66. add_post_meta( $product->get_id(), '_price', $price, false );