WC_Product_Grouped

Grouped Product Class.

Defined (1)

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

/includes/class-wc-product-grouped.php  
  1. class WC_Product_Grouped extends WC_Product { 
  2.  
  3. /** 
  4. * Stores product data. 
  5. * @var array 
  6. */ 
  7. protected $extra_data = array( 
  8. 'children' => array(),  
  9. ); 
  10.  
  11. /** 
  12. * Get internal type. 
  13. * @return string 
  14. */ 
  15. public function get_type() { 
  16. return 'grouped'; 
  17.  
  18. /** 
  19. * Get the add to cart button text. 
  20. * @access public 
  21. * @return string 
  22. */ 
  23. public function add_to_cart_text() { 
  24. return apply_filters( 'woocommerce_product_add_to_cart_text', __( 'View products', 'woocommerce' ), $this ); 
  25.  
  26. /** 
  27. * Returns whether or not the product is on sale. 
  28. * @param string $context What the value is for. Valid values are view and edit. 
  29. * @return bool 
  30. */ 
  31. public function is_on_sale( $context = 'view' ) { 
  32. global $wpdb; 
  33.  
  34. $on_sale = $this->get_children() && null !== $wpdb->get_var( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_sale_price' AND meta_value > 0 AND post_id IN (" . implode( ', ', array_map( 'esc_sql', $this->get_children() ) ) . ");" ); 
  35.  
  36. return 'view' === $context ? apply_filters( 'woocommerce_product_is_on_sale', $on_sale, $this ) : $on_sale; 
  37.  
  38. /** 
  39. * Returns false if the product cannot be bought. 
  40. * @return bool 
  41. */ 
  42. public function is_purchasable() { 
  43. return apply_filters( 'woocommerce_is_purchasable', false, $this ); 
  44.  
  45. /** 
  46. * Returns the price in html format. 
  47. * @access public 
  48. * @param string $price (default: '') 
  49. * @return string 
  50. */ 
  51. public function get_price_html( $price = '' ) { 
  52. $tax_display_mode = get_option( 'woocommerce_tax_display_shop' ); 
  53. $child_prices = array(); 
  54.  
  55. foreach ( $this->get_children() as $child_id ) { 
  56. $child = wc_get_product( $child_id ); 
  57. if ( '' !== $child->get_price() ) { 
  58. $child_prices[] = 'incl' === $tax_display_mode ? wc_get_price_including_tax( $child ) : wc_get_price_excluding_tax( $child ); 
  59.  
  60. if ( ! empty( $child_prices ) ) { 
  61. $min_price = min( $child_prices ); 
  62. $max_price = max( $child_prices ); 
  63. } else { 
  64. $min_price = ''; 
  65. $max_price = ''; 
  66.  
  67. if ( '' !== $min_price ) { 
  68. $price = $min_price !== $max_price ? sprintf( _x( '%1$s–%2$s', 'Price range: from-to', 'woocommerce' ), wc_price( $min_price ), wc_price( $max_price ) ) : wc_price( $min_price ); 
  69. $is_free = ( 0 == $min_price && 0 == $max_price ); 
  70.  
  71. if ( $is_free ) { 
  72. $price = apply_filters( 'woocommerce_grouped_free_price_html', __( 'Free!', 'woocommerce' ), $this ); 
  73. } else { 
  74. $price = apply_filters( 'woocommerce_grouped_price_html', $price . $this->get_price_suffix(), $this, $child_prices ); 
  75. } else { 
  76. $price = apply_filters( 'woocommerce_grouped_empty_price_html', '', $this ); 
  77.  
  78. return apply_filters( 'woocommerce_get_price_html', $price, $this ); 
  79.  
  80. /** 
  81. |-------------------------------------------------------------------------- 
  82. | Getters 
  83. |-------------------------------------------------------------------------- 
  84. | Methods for getting data from the product object. 
  85. */ 
  86.  
  87. /** 
  88. * Return the children of this product. 
  89. * @param string $context 
  90. * @return array 
  91. */ 
  92. public function get_children( $context = 'view' ) { 
  93. return $this->get_prop( 'children', $context ); 
  94.  
  95. /** 
  96. |-------------------------------------------------------------------------- 
  97. | Setters 
  98. |-------------------------------------------------------------------------- 
  99. | Methods for getting data from the product object. 
  100. */ 
  101.  
  102. /** 
  103. * Return the children of this product. 
  104. * @param array $children 
  105. */ 
  106. public function set_children( $children ) { 
  107. $this->set_prop( 'children', array_filter( wp_parse_id_list( (array) $children ) ) ); 
  108.  
  109. /** 
  110. |-------------------------------------------------------------------------- 
  111. | Sync with children. 
  112. |-------------------------------------------------------------------------- 
  113. */ 
  114.  
  115. /** 
  116. * Sync a grouped product with it's children. These sync functions sync 
  117. * upwards (from child to parent) when the variation is saved. 
  118. * @param WC_Product|int $product Product object or ID for which you wish to sync. 
  119. * @param bool $save If true, the prouduct object will be saved to the DB before returning it. 
  120. * @return WC_Product Synced product object. 
  121. */ 
  122. public static function sync( $product, $save = true ) { 
  123. if ( ! is_a( $product, 'WC_Product' ) ) { 
  124. $product = wc_get_product( $product ); 
  125. if ( is_a( $product, 'WC_Product_Grouped' ) ) { 
  126. $data_store = WC_Data_Store::load( 'product-' . $product->get_type() ); 
  127. $data_store->sync_price( $product ); 
  128. if ( $save ) { 
  129. $product->save(); 
  130. return $product;