class-wc-product-external.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /**
  3. * External Product
  4. *
  5. * External products cannot be bought; they link offsite. Extends simple products.
  6. *
  7. * @package WooCommerce/Classes/Products
  8. * @version 3.0.0
  9. */
  10. defined( 'ABSPATH' ) || exit;
  11. /**
  12. * Product external class.
  13. */
  14. class WC_Product_External extends WC_Product {
  15. /**
  16. * Stores product data.
  17. *
  18. * @var array
  19. */
  20. protected $extra_data = array(
  21. 'product_url' => '',
  22. 'button_text' => '',
  23. );
  24. /**
  25. * Get internal type.
  26. *
  27. * @return string
  28. */
  29. public function get_type() {
  30. return 'external';
  31. }
  32. /*
  33. |--------------------------------------------------------------------------
  34. | Getters
  35. |--------------------------------------------------------------------------
  36. |
  37. | Methods for getting data from the product object.
  38. */
  39. /**
  40. * Get product url.
  41. *
  42. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  43. * @return string
  44. */
  45. public function get_product_url( $context = 'view' ) {
  46. return esc_url_raw( $this->get_prop( 'product_url', $context ) );
  47. }
  48. /**
  49. * Get button text.
  50. *
  51. * @param string $context What the value is for. Valid values are 'view' and 'edit'.
  52. * @return string
  53. */
  54. public function get_button_text( $context = 'view' ) {
  55. return $this->get_prop( 'button_text', $context );
  56. }
  57. /*
  58. |--------------------------------------------------------------------------
  59. | Setters
  60. |--------------------------------------------------------------------------
  61. |
  62. | Functions for setting product data. These should not update anything in the
  63. | database itself and should only change what is stored in the class
  64. | object.
  65. */
  66. /**
  67. * Set product URL.
  68. *
  69. * @since 3.0.0
  70. * @param string $product_url Product URL.
  71. */
  72. public function set_product_url( $product_url ) {
  73. $this->set_prop( 'product_url', htmlspecialchars_decode( $product_url ) );
  74. }
  75. /**
  76. * Set button text.
  77. *
  78. * @since 3.0.0
  79. * @param string $button_text Button text.
  80. */
  81. public function set_button_text( $button_text ) {
  82. $this->set_prop( 'button_text', $button_text );
  83. }
  84. /**
  85. * External products cannot be stock managed.
  86. *
  87. * @since 3.0.0
  88. * @param bool $manage_stock If manage stock.
  89. */
  90. public function set_manage_stock( $manage_stock ) {
  91. $this->set_prop( 'manage_stock', false );
  92. if ( true === $manage_stock ) {
  93. $this->error( 'product_external_invalid_manage_stock', __( 'External products cannot be stock managed.', 'woocommerce' ) );
  94. }
  95. }
  96. /**
  97. * External products cannot be stock managed.
  98. *
  99. * @since 3.0.0
  100. *
  101. * @param string $stock_status Stock status.
  102. */
  103. public function set_stock_status( $stock_status = '' ) {
  104. $this->set_prop( 'stock_status', 'instock' );
  105. if ( 'instock' !== $stock_status ) {
  106. $this->error( 'product_external_invalid_stock_status', __( 'External products cannot be stock managed.', 'woocommerce' ) );
  107. }
  108. }
  109. /**
  110. * External products cannot be backordered.
  111. *
  112. * @since 3.0.0
  113. * @param string $backorders Options: 'yes', 'no' or 'notify'.
  114. */
  115. public function set_backorders( $backorders ) {
  116. $this->set_prop( 'backorders', 'no' );
  117. if ( 'no' !== $backorders ) {
  118. $this->error( 'product_external_invalid_backorders', __( 'External products cannot be backordered.', 'woocommerce' ) );
  119. }
  120. }
  121. /*
  122. |--------------------------------------------------------------------------
  123. | Other Actions
  124. |--------------------------------------------------------------------------
  125. */
  126. /**
  127. * Returns false if the product cannot be bought.
  128. *
  129. * @access public
  130. * @return bool
  131. */
  132. public function is_purchasable() {
  133. return apply_filters( 'woocommerce_is_purchasable', false, $this );
  134. }
  135. /**
  136. * Get the add to url used mainly in loops.
  137. *
  138. * @access public
  139. * @return string
  140. */
  141. public function add_to_cart_url() {
  142. return apply_filters( 'woocommerce_product_add_to_cart_url', $this->get_product_url(), $this );
  143. }
  144. /**
  145. * Get the add to cart button text for the single page.
  146. *
  147. * @access public
  148. * @return string
  149. */
  150. public function single_add_to_cart_text() {
  151. return apply_filters( 'woocommerce_product_single_add_to_cart_text', $this->get_button_text() ? $this->get_button_text() : _x( 'Buy product', 'placeholder', 'woocommerce' ), $this );
  152. }
  153. /**
  154. * Get the add to cart button text.
  155. *
  156. * @access public
  157. * @return string
  158. */
  159. public function add_to_cart_text() {
  160. return apply_filters( 'woocommerce_product_add_to_cart_text', $this->get_button_text() ? $this->get_button_text() : _x( 'Buy product', 'placeholder', 'woocommerce' ), $this );
  161. }
  162. /**
  163. * Get the add to cart button text description - used in aria tags.
  164. *
  165. * @since 3.3.0
  166. * @return string
  167. */
  168. public function add_to_cart_description() {
  169. /* translators: %s: Product title */
  170. return apply_filters( 'woocommerce_product_add_to_cart_description', $this->get_button_text() ? $this->get_button_text() : sprintf( __( 'Buy &ldquo;%s&rdquo;', 'woocommerce' ), $this->get_name() ), $this );
  171. }
  172. }