class-wc-product.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. class Booked_WC_Product {
  3. private static $products = array();
  4. public $data;
  5. public $post_id;
  6. public $type;
  7. public $title;
  8. public $currency;
  9. public $variations=array();
  10. private function __construct( $post_id ) {
  11. if ( function_exists( 'icl_object_id' ) ):
  12. $post_id = icl_object_id( $post_id, 'product', true );
  13. elseif ( function_exists( 'pll_get_post' ) ):
  14. $post_id = pll_get_post( $post_id, 'product', true );
  15. endif;
  16. $this->post_id = $post_id;
  17. $this->currency = get_woocommerce_currency_symbol();
  18. $this->get_data();
  19. }
  20. public static function get( $post_id = null ) {
  21. if ( !is_integer($post_id) ) {
  22. $message = sprintf( __('%s integer expected when %s given.', 'booked-woocommerce-payments'), 'Booked_WC_Product::get($post_id)', gettype($post_id) );
  23. throw new Exception($message);
  24. } else if ( $post_id===0 ) {
  25. self::$products[$post_id] = false;
  26. } else if ( !isset(self::$products[$post_id]) ) {
  27. self::$products[$post_id] = new self($post_id);
  28. }
  29. return self::$products[$post_id];
  30. }
  31. protected function get_data() {
  32. $this->_get_product_data();
  33. $this->_get_price();
  34. $this->_get_type();
  35. $this->_get_title();
  36. $this->_get_variations();
  37. }
  38. protected function _get_product_data() {
  39. $this->data = wc_get_product($this->post_id);
  40. if ( !$this->data ) {
  41. $message = sprintf(__('An error has occur while retrieving product data for product with ID %1$d.', 'booked-woocommerce-payments'), $this->post_id);
  42. throw new Exception($message);
  43. }
  44. return $this;
  45. }
  46. protected function _get_price() {
  47. $this->data->get_price();
  48. return $this;
  49. }
  50. protected function _get_type() {
  51. $this->type = $this->data->get_type();
  52. return $this;
  53. }
  54. protected function _get_title() {
  55. $booked_wc_currency_symbol = get_woocommerce_currency_symbol();
  56. $booked_wc_currency_position = get_option( 'woocommerce_currency_pos','left' );
  57. if ( $this->type === 'variable' ) {
  58. $this->title = $this->data->post->post_title;
  59. } else {
  60. $this_price = ( $this->data->get_price() ? $this->data->get_price() : '0' );
  61. echo '<!-- ' . $this->data->post->post_title . ' -->';
  62. switch ( $booked_wc_currency_position ) {
  63. case 'left' :
  64. $this->title = $booked_wc_currency_symbol . $this_price . ' - ' . $this->data->post->post_title;
  65. break;
  66. case 'right' :
  67. $this->title = $this_price . $booked_wc_currency_symbol . ' - ' . $this->data->post->post_title;
  68. break;
  69. case 'left_space' :
  70. $this->title = $booked_wc_currency_symbol . ' ' . $this_price . ' - ' . $this->data->post->post_title;
  71. break;
  72. case 'right_space' :
  73. $this->title = $this_price . ' ' . $booked_wc_currency_symbol . ' - ' . $this->data->post->post_title;
  74. break;
  75. }
  76. }
  77. return $this;
  78. }
  79. protected function _get_variations() {
  80. if ( $this->type==='variable' ) {
  81. add_filter('woocommerce_available_variation', array('Booked_WC_Variation', 'woocommerce_available_variation'), 10, 3);
  82. $product_variations = $this->data->get_available_variations();
  83. remove_filter('woocommerce_available_variation', array('Booked_WC_Variation', 'woocommerce_available_variation'));
  84. // use variation IDs as keys for their values
  85. $variations = array();
  86. foreach ($product_variations as $variation_data) {
  87. $vid = ( isset($variation_data['variation_id']) && $variation_data['variation_id'] ? $variation_data['variation_id'] : $variation_data['id'] );
  88. $variations[$vid] = $variation_data;
  89. }
  90. $this->variations = $variations;
  91. }
  92. return $this;
  93. }
  94. }