class-wc-product-grouped-data-store-cpt.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * Class WC_Product_Grouped_Data_Store_CPT file.
  4. *
  5. * @package WooCommerce\DataStores
  6. */
  7. if ( ! defined( 'ABSPATH' ) ) {
  8. exit;
  9. }
  10. /**
  11. * WC Grouped Product Data Store: Stored in CPT.
  12. *
  13. * @version 3.0.0
  14. */
  15. class WC_Product_Grouped_Data_Store_CPT extends WC_Product_Data_Store_CPT implements WC_Object_Data_Store_Interface {
  16. /**
  17. * Helper method that updates all the post meta for a grouped product.
  18. *
  19. * @param WC_Product $product Product object.
  20. * @param bool $force Force update. Used during create.
  21. * @since 3.0.0
  22. */
  23. protected function update_post_meta( &$product, $force = false ) {
  24. $meta_key_to_props = array(
  25. '_children' => 'children',
  26. );
  27. $props_to_update = $force ? $meta_key_to_props : $this->get_props_to_update( $product, $meta_key_to_props );
  28. foreach ( $props_to_update as $meta_key => $prop ) {
  29. $value = $product->{"get_$prop"}( 'edit' );
  30. $updated = update_post_meta( $product->get_id(), $meta_key, $value );
  31. if ( $updated ) {
  32. $this->updated_props[] = $prop;
  33. }
  34. }
  35. parent::update_post_meta( $product, $force );
  36. }
  37. /**
  38. * Handle updated meta props after updating meta data.
  39. *
  40. * @since 3.0.0
  41. * @param WC_Product $product Product object.
  42. */
  43. protected function handle_updated_props( &$product ) {
  44. if ( in_array( 'children', $this->updated_props, true ) ) {
  45. $this->update_prices_from_children( $product );
  46. }
  47. parent::handle_updated_props( $product );
  48. }
  49. /**
  50. * Sync grouped product prices with children.
  51. *
  52. * @since 3.0.0
  53. * @param WC_Product|int $product Product object or product ID.
  54. */
  55. public function sync_price( &$product ) {
  56. $this->update_prices_from_children( $product );
  57. }
  58. /**
  59. * Loop over child products and update the grouped product prices.
  60. *
  61. * @param WC_Product $product Product object.
  62. */
  63. protected function update_prices_from_children( &$product ) {
  64. $child_prices = array();
  65. foreach ( $product->get_children( 'edit' ) as $child_id ) {
  66. $child = wc_get_product( $child_id );
  67. if ( $child ) {
  68. $child_prices[] = $child->get_price( 'edit' );
  69. }
  70. }
  71. $child_prices = array_filter( $child_prices );
  72. delete_post_meta( $product->get_id(), '_price' );
  73. delete_post_meta( $product->get_id(), '_sale_price' );
  74. delete_post_meta( $product->get_id(), '_regular_price' );
  75. if ( ! empty( $child_prices ) ) {
  76. add_post_meta( $product->get_id(), '_price', min( $child_prices ) );
  77. add_post_meta( $product->get_id(), '_price', max( $child_prices ) );
  78. }
  79. }
  80. }