WC_Product_Variation_Data_Store_CPT

WC Variation Product Data Store: Stored in CPT.

Defined (1)

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

/includes/data-stores/class-wc-product-variation-data-store-cpt.php  
  1. class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT implements WC_Object_Data_Store_Interface { 
  2.  
  3. /** 
  4. * Callback to remove unwanted meta data. 
  5. * @param object $meta 
  6. * @return bool false if excluded. 
  7. */ 
  8. protected function exclude_internal_meta_keys( $meta ) { 
  9. return ! in_array( $meta->meta_key, $this->internal_meta_keys ) && 0 !== stripos( $meta->meta_key, 'attribute_' ) && 0 !== stripos( $meta->meta_key, 'wp_' ); 
  10.  
  11. /** 
  12. |-------------------------------------------------------------------------- 
  13. | CRUD Methods 
  14. |-------------------------------------------------------------------------- 
  15. */ 
  16.  
  17. /** 
  18. * Reads a product from the database and sets its data to the class. 
  19. * @since 3.0.0 
  20. * @param WC_Product 
  21. */ 
  22. public function read( &$product ) { 
  23. $product->set_defaults(); 
  24.  
  25. if ( ! $product->get_id() || ! ( $post_object = get_post( $product->get_id() ) ) || 'product_variation' !== $post_object->post_type ) { 
  26. return; 
  27.  
  28. $id = $product->get_id(); 
  29. $product->set_parent_id( $post_object->post_parent ); 
  30. $parent_id = $product->get_parent_id(); 
  31.  
  32. // The post doesn't have a parent id, therefore its invalid and we should prevent this being created. 
  33. if ( empty( $parent_id ) ) { 
  34. throw new Exception( sprintf( 'No parent product set for variation #%d', $product->get_id() ), 422 ); 
  35.  
  36. // The post parent is not a valid variable product so we should prevent this being created. 
  37. if ( 'product' !== get_post_type( $product->get_parent_id() ) ) { 
  38. throw new Exception( sprintf( 'Invalid parent for variation #%d', $product->get_id() ), 422 ); 
  39.  
  40. $product->set_props( array( 
  41. 'name' => $post_object->post_title,  
  42. 'slug' => $post_object->post_name,  
  43. 'date_created' => 0 < $post_object->post_date_gmt ? wc_string_to_timestamp( $post_object->post_date_gmt ) : null,  
  44. 'date_modified' => 0 < $post_object->post_modified_gmt ? wc_string_to_timestamp( $post_object->post_modified_gmt ) : null,  
  45. 'status' => $post_object->post_status,  
  46. 'menu_order' => $post_object->menu_order,  
  47. 'reviews_allowed' => 'open' === $post_object->comment_status,  
  48. ) ); 
  49.  
  50. $this->read_downloads( $product ); 
  51. $this->read_product_data( $product ); 
  52. $this->read_extra_data( $product ); 
  53. $product->set_attributes( wc_get_product_variation_attributes( $product->get_id() ) ); 
  54.  
  55. /** 
  56. * If a variation title is not in sync with the parent e.g. saved prior to 3.0, or if the parent title has changed, detect here and update. 
  57. */ 
  58. if ( version_compare( get_post_meta( $product->get_id(), '_product_version', true ), '3.0', '<' ) && 0 !== strpos( $post_object->post_title, get_post_field( 'post_title', $product->get_parent_id() ) ) ) { 
  59. global $wpdb; 
  60.  
  61. $new_title = $this->generate_product_title( $product ); 
  62. $product->set_name( $new_title ); 
  63. $wpdb->update( $wpdb->posts, array( 'post_title' => $new_title ), array( 'ID' => $product->get_id() ) ); 
  64. clean_post_cache( $product->get_id() ); 
  65.  
  66. // Set object_read true once all data is read. 
  67. $product->set_object_read( true ); 
  68.  
  69. /** 
  70. * Create a new product. 
  71. * @since 3.0.0 
  72. * @param WC_Product 
  73. */ 
  74. public function create( &$product ) { 
  75. if ( ! $product->get_date_created() ) { 
  76. $product->set_date_created( current_time( 'timestamp', true ) ); 
  77.  
  78. $id = wp_insert_post( apply_filters( 'woocommerce_new_product_variation_data', array( 
  79. 'post_type' => 'product_variation',  
  80. 'post_status' => $product->get_status() ? $product->get_status() : 'publish',  
  81. 'post_author' => get_current_user_id(),  
  82. 'post_title' => $this->generate_product_title( $product ),  
  83. 'post_content' => '',  
  84. 'post_parent' => $product->get_parent_id(),  
  85. 'comment_status' => 'closed',  
  86. 'ping_status' => 'closed',  
  87. 'menu_order' => $product->get_menu_order(),  
  88. 'post_date' => gmdate( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getOffsetTimestamp() ),  
  89. 'post_date_gmt' => gmdate( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getTimestamp() ),  
  90. ) ), true ); 
  91.  
  92. if ( $id && ! is_wp_error( $id ) ) { 
  93. $product->set_id( $id ); 
  94.  
  95. $this->update_post_meta( $product, true ); 
  96. $this->update_terms( $product, true ); 
  97. $this->update_attributes( $product, true ); 
  98. $this->handle_updated_props( $product ); 
  99.  
  100. $product->save_meta_data(); 
  101. $product->apply_changes(); 
  102.  
  103. $this->update_version_and_type( $product ); 
  104.  
  105. $this->clear_caches( $product ); 
  106.  
  107. do_action( 'woocommerce_new_product_variation', $id ); 
  108.  
  109. /** 
  110. * Updates an existing product. 
  111. * @since 3.0.0 
  112. * @param WC_Product 
  113. */ 
  114. public function update( &$product ) { 
  115. $product->save_meta_data(); 
  116. $changes = $product->get_changes(); 
  117. $title = $this->generate_product_title( $product ); 
  118.  
  119. // Only update the post when the post data changes. 
  120. if ( $title !== $product->get_name( 'edit' ) || array_intersect( array( 'parent_id', 'status', 'menu_order', 'date_created', 'date_modified' ), array_keys( $changes ) ) ) { 
  121. $post_data = array( 
  122. 'post_title' => $title,  
  123. 'post_parent' => $product->get_parent_id( 'edit' ),  
  124. 'comment_status' => 'closed',  
  125. 'post_status' => $product->get_status( 'edit' ) ? $product->get_status( 'edit' ) : 'publish',  
  126. 'menu_order' => $product->get_menu_order( 'edit' ),  
  127. 'post_date' => gmdate( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getOffsetTimestamp() ),  
  128. 'post_date_gmt' => gmdate( 'Y-m-d H:i:s', $product->get_date_created( 'edit' )->getTimestamp() ),  
  129. 'post_modified' => isset( $changes['date_modified'] ) ? gmdate( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' )->getOffsetTimestamp() ) : current_time( 'mysql' ),  
  130. 'post_modified_gmt' => isset( $changes['date_modified'] ) ? gmdate( 'Y-m-d H:i:s', $product->get_date_modified( 'edit' )->getTimestamp() ) : current_time( 'mysql', 1 ),  
  131. ); 
  132.  
  133. /** 
  134. * When updating this object, to prevent infinite loops, use $wpdb 
  135. * to update data, since wp_update_post spawns more calls to the 
  136. * save_post action. 
  137. * This ensures hooks are fired by either WP itself (admin screen save),  
  138. * or an update purely from CRUD. 
  139. */ 
  140. if ( doing_action( 'save_post' ) ) { 
  141. $GLOBALS['wpdb']->update( $GLOBALS['wpdb']->posts, $post_data, array( 'ID' => $product->get_id() ) ); 
  142. clean_post_cache( $product->get_id() ); 
  143. } else { 
  144. wp_update_post( array_merge( array( 'ID' => $product->get_id() ), $post_data ) ); 
  145. $product->read_meta_data( true ); // Refresh internal meta data, in case things were hooked into `save_post` or another WP hook. 
  146.  
  147. $this->update_post_meta( $product ); 
  148. $this->update_terms( $product ); 
  149. $this->update_attributes( $product ); 
  150. $this->handle_updated_props( $product ); 
  151.  
  152. $product->apply_changes(); 
  153.  
  154. $this->update_version_and_type( $product ); 
  155.  
  156. $this->clear_caches( $product ); 
  157.  
  158. do_action( 'woocommerce_update_product_variation', $product->get_id() ); 
  159.  
  160. /** 
  161. |-------------------------------------------------------------------------- 
  162. | Additional Methods 
  163. |-------------------------------------------------------------------------- 
  164. */ 
  165.  
  166. /** 
  167. * Generates a title with attribute information for a variation. 
  168. * Products will get a title of the form "Name - Value, Value" or just "Name". 
  169. * @since 3.0.0 
  170. * @param WC_Product 
  171. * @return string 
  172. */ 
  173. protected function generate_product_title( $product ) { 
  174. $attributes = (array) $product->get_attributes(); 
  175.  
  176. // Don't include attributes if the product has 3+ attributes. 
  177. $should_include_attributes = count( $attributes ) < 3; 
  178.  
  179. // Don't include attributes if an attribute name has 2+ words. 
  180. if ( $should_include_attributes ) { 
  181. foreach ( $attributes as $name => $value ) { 
  182. if ( false !== strpos( $name, '-' ) ) { 
  183. $should_include_attributes = false; 
  184. break; 
  185.  
  186. $should_include_attributes = apply_filters( 'woocommerce_product_variation_title_include_attributes', $should_include_attributes, $product ); 
  187. $separator = apply_filters( 'woocommerce_product_variation_title_attributes_separator', ' - ', $product ); 
  188. $title_base = get_post_field( 'post_title', $product->get_parent_id() ); 
  189. $title_suffix = $should_include_attributes ? wc_get_formatted_variation( $product, true, false ) : ''; 
  190.  
  191. return apply_filters( 'woocommerce_product_variation_title', rtrim( $title_base . $separator . $title_suffix, $separator ), $product, $title_base, $title_suffix ); 
  192.  
  193. /** 
  194. * Make sure we store the product version (to track data changes). 
  195. * @param WC_Product 
  196. * @since 3.0.0 
  197. */ 
  198. protected function update_version_and_type( &$product ) { 
  199. update_post_meta( $product->get_id(), '_product_version', WC_VERSION ); 
  200.  
  201. /** 
  202. * Read post data. 
  203. * @since 3.0.0 
  204. * @param WC_Product 
  205. */ 
  206. protected function read_product_data( &$product ) { 
  207. $id = $product->get_id(); 
  208. $product->set_props( array( 
  209. 'description' => get_post_meta( $id, '_variation_description', true ),  
  210. 'regular_price' => get_post_meta( $id, '_regular_price', true ),  
  211. 'sale_price' => get_post_meta( $id, '_sale_price', true ),  
  212. 'date_on_sale_from' => get_post_meta( $id, '_sale_price_dates_from', true ),  
  213. 'date_on_sale_to' => get_post_meta( $id, '_sale_price_dates_to', true ),  
  214. 'manage_stock' => get_post_meta( $id, '_manage_stock', true ),  
  215. 'stock_status' => get_post_meta( $id, '_stock_status', true ),  
  216. 'shipping_class_id' => current( $this->get_term_ids( $id, 'product_shipping_class' ) ),  
  217. 'virtual' => get_post_meta( $id, '_virtual', true ),  
  218. 'downloadable' => get_post_meta( $id, '_downloadable', true ),  
  219. 'gallery_image_ids' => array_filter( explode( ', ', get_post_meta( $id, '_product_image_gallery', true ) ) ),  
  220. 'download_limit' => get_post_meta( $id, '_download_limit', true ),  
  221. 'download_expiry' => get_post_meta( $id, '_download_expiry', true ),  
  222. 'image_id' => get_post_thumbnail_id( $id ),  
  223. 'backorders' => get_post_meta( $id, '_backorders', true ),  
  224. 'sku' => get_post_meta( $id, '_sku', true ),  
  225. 'stock_quantity' => get_post_meta( $id, '_stock', true ),  
  226. 'weight' => get_post_meta( $id, '_weight', true ),  
  227. 'length' => get_post_meta( $id, '_length', true ),  
  228. 'width' => get_post_meta( $id, '_width', true ),  
  229. 'height' => get_post_meta( $id, '_height', true ),  
  230. 'tax_class' => ! metadata_exists( 'post', $id, '_tax_class' ) ? 'parent' : get_post_meta( $id, '_tax_class', true ),  
  231. ) ); 
  232.  
  233. if ( $product->is_on_sale( 'edit' ) ) { 
  234. $product->set_price( $product->get_sale_price( 'edit' ) ); 
  235. } else { 
  236. $product->set_price( $product->get_regular_price( 'edit' ) ); 
  237.  
  238. $parent_object = get_post( $product->get_parent_id() ); 
  239. $product->set_parent_data( array( 
  240. 'title' => $parent_object->post_title,  
  241. 'sku' => get_post_meta( $product->get_parent_id(), '_sku', true ),  
  242. 'manage_stock' => get_post_meta( $product->get_parent_id(), '_manage_stock', true ),  
  243. 'backorders' => get_post_meta( $product->get_parent_id(), '_backorders', true ),  
  244. 'stock_quantity' => get_post_meta( $product->get_parent_id(), '_stock', true ),  
  245. 'weight' => get_post_meta( $product->get_parent_id(), '_weight', true ),  
  246. 'length' => get_post_meta( $product->get_parent_id(), '_length', true ),  
  247. 'width' => get_post_meta( $product->get_parent_id(), '_width', true ),  
  248. 'height' => get_post_meta( $product->get_parent_id(), '_height', true ),  
  249. 'tax_class' => get_post_meta( $product->get_parent_id(), '_tax_class', true ),  
  250. 'shipping_class_id' => absint( current( $this->get_term_ids( $product->get_parent_id(), 'product_shipping_class' ) ) ),  
  251. 'image_id' => get_post_thumbnail_id( $product->get_parent_id() ),  
  252. ) ); 
  253.  
  254. // Pull data from the parent when there is no user-facing way to set props. 
  255. $product->set_sold_individually( get_post_meta( $product->get_parent_id(), '_sold_individually', true ) ); 
  256. $product->set_tax_status( get_post_meta( $product->get_parent_id(), '_tax_status', true ) ); 
  257. $product->set_cross_sell_ids( get_post_meta( $product->get_parent_id(), '_crosssell_ids', true ) ); 
  258.  
  259. /** 
  260. * For all stored terms in all taxonomies, save them to the DB. 
  261. * @since 3.0.0 
  262. * @param WC_Product 
  263. * @param bool Force update. Used during create. 
  264. */ 
  265. protected function update_terms( &$product, $force = false ) { 
  266. $changes = $product->get_changes(); 
  267.  
  268. if ( $force || array_key_exists( 'shipping_class_id', $changes ) ) { 
  269. wp_set_post_terms( $product->get_id(), array( $product->get_shipping_class_id( 'edit' ) ), 'product_shipping_class', false ); 
  270.  
  271. /** 
  272. * Update attribute meta values. 
  273. * @since 3.0.0 
  274. * @param WC_Product 
  275. * @param bool Force update. Used during create. 
  276. */ 
  277. protected function update_attributes( &$product, $force = false ) { 
  278. $changes = $product->get_changes(); 
  279.  
  280. if ( $force || array_key_exists( 'attributes', $changes ) ) { 
  281. global $wpdb; 
  282. $attributes = $product->get_attributes(); 
  283. $updated_attribute_keys = array(); 
  284. foreach ( $attributes as $key => $value ) { 
  285. update_post_meta( $product->get_id(), 'attribute_' . $key, $value ); 
  286. $updated_attribute_keys[] = 'attribute_' . $key; 
  287.  
  288. // Remove old taxonomies attributes so data is kept up to date - first get attribute key names. 
  289. $delete_attribute_keys = $wpdb->get_col( $wpdb->prepare( "SELECT meta_key FROM {$wpdb->postmeta} WHERE meta_key LIKE 'attribute_%%' AND meta_key NOT IN ( '" . implode( "', '", array_map( 'esc_sql', $updated_attribute_keys ) ) . "' ) AND post_id = %d;", $product->get_id() ) ); 
  290.  
  291. foreach ( $delete_attribute_keys as $key ) { 
  292. delete_post_meta( $product->get_id(), $key ); 
  293.  
  294. /** 
  295. * Helper method that updates all the post meta for a product based on it's settings in the WC_Product class. 
  296. * @since 3.0.0 
  297. * @param WC_Product 
  298. * @param bool Force update. Used during create. 
  299. */ 
  300. public function update_post_meta( &$product, $force = false ) { 
  301. $meta_key_to_props = array( 
  302. '_variation_description' => 'description',  
  303. ); 
  304.  
  305. $props_to_update = $force ? $meta_key_to_props : $this->get_props_to_update( $product, $meta_key_to_props ); 
  306.  
  307. foreach ( $props_to_update as $meta_key => $prop ) { 
  308. $value = $product->{"get_$prop"}( 'edit' ); 
  309. $updated = update_post_meta( $product->get_id(), $meta_key, $value ); 
  310. if ( $updated ) { 
  311. $this->updated_props[] = $prop; 
  312.  
  313. parent::update_post_meta( $product, $force );