/includes/data-stores/class-wc-product-variation-data-store-cpt.php

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