WC_Product_Attribute

Represents a product attribute.

Defined (1)

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

/includes/class-wc-product-attribute.php  
  1. class WC_Product_Attribute implements ArrayAccess { 
  2.  
  3. /** 
  4. * Data array. 
  5. * @since 3.0.0 
  6. * @var array 
  7. */ 
  8. protected $data = array( 
  9. 'id' => 0,  
  10. 'name' => '',  
  11. 'options' => array(),  
  12. 'position' => 0,  
  13. 'visible' => false,  
  14. 'variation' => false,  
  15. ); 
  16.  
  17. /** 
  18. * Return if this attribute is a taxonomy. 
  19. * @return boolean 
  20. */ 
  21. public function is_taxonomy() { 
  22. return 0 < $this->get_id(); 
  23.  
  24. /** 
  25. * Get taxonomy name if applicable. 
  26. * @return string 
  27. */ 
  28. public function get_taxonomy() { 
  29. return $this->is_taxonomy() ? $this->get_name() : ''; 
  30.  
  31. /** 
  32. * Get taxonomy object. 
  33. * @return array|null 
  34. */ 
  35. public function get_taxonomy_object() { 
  36. global $wc_product_attributes; 
  37. return $this->is_taxonomy() ? $wc_product_attributes[ $this->get_name() ] : null; 
  38.  
  39. /** 
  40. * Gets terms from the stored options. 
  41. * @return array|null 
  42. */ 
  43. public function get_terms() { 
  44. if ( ! $this->is_taxonomy() || ! taxonomy_exists( $this->get_name() ) ) { 
  45. return null; 
  46. $terms = array(); 
  47. foreach ( $this->get_options() as $option ) { 
  48. if ( is_int( $option ) ) { 
  49. $term = get_term_by( 'id', $option, $this->get_name() ); 
  50. } else { 
  51. $term = get_term_by( 'name', $option, $this->get_name() ); 
  52.  
  53. if ( ! $term || is_wp_error( $term ) ) { 
  54. $new_term = wp_insert_term( $option, $this->get_name() ); 
  55. $term = is_wp_error( $new_term ) ? false : get_term_by( 'id', $new_term['term_id'], $this->get_name() ); 
  56. if ( $term && ! is_wp_error( $term ) ) { 
  57. $terms[] = $term; 
  58. return $terms; 
  59.  
  60. /** 
  61. * Gets slugs from the stored options, or just the string if text based. 
  62. * @return array 
  63. */ 
  64. public function get_slugs() { 
  65. if ( ! $this->is_taxonomy() || ! taxonomy_exists( $this->get_name() ) ) { 
  66. return $this->get_options(); 
  67. $terms = array(); 
  68. foreach ( $this->get_options() as $option ) { 
  69. if ( is_int( $option ) ) { 
  70. $term = get_term_by( 'id', $option, $this->get_name() ); 
  71. } else { 
  72. $term = get_term_by( 'name', $option, $this->get_name() ); 
  73.  
  74. if ( ! $term || is_wp_error( $term ) ) { 
  75. $new_term = wp_insert_term( $option, $this->get_name() ); 
  76. $term = is_wp_error( $new_term ) ? false : get_term_by( 'id', $new_term['term_id'], $this->get_name() ); 
  77. if ( $term && ! is_wp_error( $term ) ) { 
  78. $terms[] = $term->slug; 
  79. return $terms; 
  80.  
  81. /** 
  82. * Returns all data for this object. 
  83. * @return array 
  84. */ 
  85. public function get_data() { 
  86. return array_merge( $this->data, array( 
  87. 'is_visible' => $this->get_visible() ? 1 : 0,  
  88. 'is_variation' => $this->get_variation() ? 1 : 0,  
  89. 'is_taxonomy' => $this->is_taxonomy() ? 1 : 0,  
  90. 'value' => $this->is_taxonomy() ? '' : wc_implode_text_attributes( $this->get_options() ),  
  91. ) ); 
  92.  
  93. /** 
  94. |-------------------------------------------------------------------------- 
  95. | Setters 
  96. |-------------------------------------------------------------------------- 
  97. */ 
  98.  
  99. /** 
  100. * Set ID (this is the attribute ID). 
  101. * @param int $value 
  102. */ 
  103. public function set_id( $value ) { 
  104. $this->data['id'] = absint( $value ); 
  105.  
  106. /** 
  107. * Set name (this is the attribute name or taxonomy). 
  108. * @param int $value 
  109. */ 
  110. public function set_name( $value ) { 
  111. $this->data['name'] = $value; 
  112.  
  113. /** 
  114. * Set options. 
  115. * @param array $value 
  116. */ 
  117. public function set_options( $value ) { 
  118. $this->data['options'] = $value; 
  119.  
  120. /** 
  121. * Set position. 
  122. * @param int $value 
  123. */ 
  124. public function set_position( $value ) { 
  125. $this->data['position'] = absint( $value ); 
  126.  
  127. /** 
  128. * Set if visible. 
  129. * @param bool $value 
  130. */ 
  131. public function set_visible( $value ) { 
  132. $this->data['visible'] = wc_string_to_bool( $value ); 
  133.  
  134. /** 
  135. * Set if variation. 
  136. * @param bool $value 
  137. */ 
  138. public function set_variation( $value ) { 
  139. $this->data['variation'] = wc_string_to_bool( $value ); 
  140.  
  141. /** 
  142. |-------------------------------------------------------------------------- 
  143. | Getters 
  144. |-------------------------------------------------------------------------- 
  145. */ 
  146.  
  147. /** 
  148. * Get the ID. 
  149. * @return int 
  150. */ 
  151. public function get_id() { 
  152. return $this->data['id']; 
  153.  
  154. /** 
  155. * Get name. 
  156. * @return int 
  157. */ 
  158. public function get_name() { 
  159. return $this->data['name']; 
  160.  
  161. /** 
  162. * Get options. 
  163. * @return array 
  164. */ 
  165. public function get_options() { 
  166. return $this->data['options']; 
  167.  
  168. /** 
  169. * Get position. 
  170. * @return int 
  171. */ 
  172. public function get_position() { 
  173. return $this->data['position']; 
  174.  
  175. /** 
  176. * Get if visible. 
  177. * @return bool 
  178. */ 
  179. public function get_visible() { 
  180. return $this->data['visible']; 
  181.  
  182. /** 
  183. * Get if variation. 
  184. * @return bool 
  185. */ 
  186. public function get_variation() { 
  187. return $this->data['variation']; 
  188.  
  189. /** 
  190. |-------------------------------------------------------------------------- 
  191. | ArrayAccess/Backwards compatibility. 
  192. |-------------------------------------------------------------------------- 
  193. */ 
  194.  
  195. /** 
  196. * offsetGet. 
  197. * @param string $offset 
  198. * @return mixed 
  199. */ 
  200. public function offsetGet( $offset ) { 
  201. switch ( $offset ) { 
  202. case 'is_variation' : 
  203. return $this->get_variation() ? 1 : 0; 
  204. break; 
  205. case 'is_visible' : 
  206. return $this->get_visible() ? 1 : 0; 
  207. break; 
  208. case 'is_taxonomy' : 
  209. return $this->is_taxonomy() ? 1 : 0; 
  210. break; 
  211. case 'value' : 
  212. return $this->is_taxonomy() ? '' : wc_implode_text_attributes( $this->get_options() ); 
  213. break; 
  214. default : 
  215. if ( is_callable( array( $this, "get_$offset" ) ) ) { 
  216. return $this->{"get_$offset"}(); 
  217. break; 
  218. return ''; 
  219.  
  220. /** 
  221. * offsetSet. 
  222. * @param string $offset 
  223. * @param mixed $value 
  224. */ 
  225. public function offsetSet( $offset, $value ) { 
  226. switch ( $offset ) { 
  227. case 'is_variation' : 
  228. $this->set_variation( $value ); 
  229. break; 
  230. case 'is_visible' : 
  231. $this->set_visible( $value ); 
  232. break; 
  233. case 'value' : 
  234. $this->set_options( $value ); 
  235. break; 
  236. default : 
  237. if ( is_callable( array( $this, "set_$offset" ) ) ) { 
  238. return $this->{"set_$offset"}( $value ); 
  239. break; 
  240.  
  241. /** 
  242. * offsetUnset. 
  243. * @param string $offset 
  244. */ 
  245. public function offsetUnset( $offset ) {} 
  246.  
  247. /** 
  248. * offsetExists. 
  249. * @param string $offset 
  250. * @return bool 
  251. */ 
  252. public function offsetExists( $offset ) { 
  253. return in_array( $offset, array_merge( array( 'is_variation', 'is_visible', 'is_taxonomy', 'value' ), array_keys( $this->data ) ) );