WC_Meta_Box_Product_Data

WC_Meta_Box_Product_Data Class.

Defined (1)

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

/includes/admin/meta-boxes/class-wc-meta-box-product-data.php  
  1. class WC_Meta_Box_Product_Data { 
  2.  
  3. /** 
  4. * Output the metabox. 
  5. * @param WP_Post $post 
  6. */ 
  7. public static function output( $post ) { 
  8. global $post, $thepostid, $product_object; 
  9.  
  10. $thepostid = $post->ID; 
  11. $product_object = $thepostid ? wc_get_product( $thepostid ) : new WC_Product; 
  12.  
  13. include( 'views/html-product-data-panel.php' ); 
  14.  
  15. /** 
  16. * Show tab content/settings. 
  17. */ 
  18. private static function output_tabs() { 
  19. global $post, $thepostid, $product_object; 
  20.  
  21. include( 'views/html-product-data-general.php' ); 
  22. include( 'views/html-product-data-inventory.php' ); 
  23. include( 'views/html-product-data-shipping.php' ); 
  24. include( 'views/html-product-data-linked-products.php' ); 
  25. include( 'views/html-product-data-attributes.php' ); 
  26. include( 'views/html-product-data-advanced.php' ); 
  27.  
  28. /** 
  29. * Return array of product type options. 
  30. * @return array 
  31. */ 
  32. private static function get_product_type_options() { 
  33. return apply_filters( 'product_type_options', array( 
  34. 'virtual' => array( 
  35. 'id' => '_virtual',  
  36. 'wrapper_class' => 'show_if_simple',  
  37. 'label' => __( 'Virtual', 'woocommerce' ),  
  38. 'description' => __( 'Virtual products are intangible and aren\'t shipped.', 'woocommerce' ),  
  39. 'default' => 'no',  
  40. ),  
  41. 'downloadable' => array( 
  42. 'id' => '_downloadable',  
  43. 'wrapper_class' => 'show_if_simple',  
  44. 'label' => __( 'Downloadable', 'woocommerce' ),  
  45. 'description' => __( 'Downloadable products give access to a file upon purchase.', 'woocommerce' ),  
  46. 'default' => 'no',  
  47. ),  
  48. ) ); 
  49.  
  50. /** 
  51. * Return array of tabs to show. 
  52. * @return array 
  53. */ 
  54. private static function get_product_data_tabs() { 
  55. return apply_filters( 'woocommerce_product_data_tabs', array( 
  56. 'general' => array( 
  57. 'label' => __( 'General', 'woocommerce' ),  
  58. 'target' => 'general_product_data',  
  59. 'class' => array( 'hide_if_grouped' ),  
  60. ),  
  61. 'inventory' => array( 
  62. 'label' => __( 'Inventory', 'woocommerce' ),  
  63. 'target' => 'inventory_product_data',  
  64. 'class' => array( 'show_if_simple', 'show_if_variable', 'show_if_grouped', 'show_if_external' ),  
  65. ),  
  66. 'shipping' => array( 
  67. 'label' => __( 'Shipping', 'woocommerce' ),  
  68. 'target' => 'shipping_product_data',  
  69. 'class' => array( 'hide_if_virtual', 'hide_if_grouped', 'hide_if_external' ),  
  70. ),  
  71. 'linked_product' => array( 
  72. 'label' => __( 'Linked Products', 'woocommerce' ),  
  73. 'target' => 'linked_product_data',  
  74. 'class' => array(),  
  75. ),  
  76. 'attribute' => array( 
  77. 'label' => __( 'Attributes', 'woocommerce' ),  
  78. 'target' => 'product_attributes',  
  79. 'class' => array(),  
  80. ),  
  81. 'variations' => array( 
  82. 'label' => __( 'Variations', 'woocommerce' ),  
  83. 'target' => 'variable_product_options',  
  84. 'class' => array( 'variations_tab', 'show_if_variable' ),  
  85. ),  
  86. 'advanced' => array( 
  87. 'label' => __( 'Advanced', 'woocommerce' ),  
  88. 'target' => 'advanced_product_data',  
  89. 'class' => array(),  
  90. ),  
  91. ) ); 
  92.  
  93. /** 
  94. * Filter callback for finding variation attributes. 
  95. * @param WC_Product_Attribute $attribute 
  96. * @return bool 
  97. */ 
  98. private static function filter_variation_attributes( $attribute ) { 
  99. return true === $attribute->get_variation(); 
  100.  
  101. /** 
  102. * Show options for the variable product type. 
  103. */ 
  104. public static function output_variations() { 
  105. global $post, $wpdb, $product_object; 
  106.  
  107. $variation_attributes = array_filter( $product_object->get_attributes(), array( __CLASS__, 'filter_variation_attributes' ) ); 
  108. $default_attributes = $product_object->get_default_attributes(); 
  109. $variations_count = absint( $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(ID) FROM $wpdb->posts WHERE post_parent = %d AND post_type = 'product_variation' AND post_status IN ('publish', 'private')", $post->ID ) ) ); 
  110. $variations_per_page = absint( apply_filters( 'woocommerce_admin_meta_boxes_variations_per_page', 15 ) ); 
  111. $variations_total_pages = ceil( $variations_count / $variations_per_page ); 
  112.  
  113. include( 'views/html-product-data-variations.php' ); 
  114.  
  115. /** 
  116. * Prepare downloads for save. 
  117. * @return array 
  118. */ 
  119. private static function prepare_downloads( $file_names, $file_urls, $file_hashes ) { 
  120. $downloads = array(); 
  121.  
  122. if ( ! empty( $file_urls ) ) { 
  123. $file_url_size = sizeof( $file_urls ); 
  124.  
  125. for ( $i = 0; $i < $file_url_size; $i ++ ) { 
  126. if ( ! empty( $file_urls[ $i ] ) ) { 
  127. $downloads[] = array( 
  128. 'name' => wc_clean( $file_names[ $i ] ),  
  129. 'file' => wp_unslash( trim( $file_urls[ $i ] ) ),  
  130. 'previous_hash' => wc_clean( $file_hashes[ $i ] ),  
  131. ); 
  132. return $downloads; 
  133.  
  134. /** 
  135. * Prepare children for save. 
  136. * @return array 
  137. */ 
  138. private static function prepare_children() { 
  139. return isset( $_POST['grouped_products'] ) ? array_filter( array_map( 'intval', (array) $_POST['grouped_products'] ) ) : array(); 
  140.  
  141. /** 
  142. * Prepare attributes for save. 
  143. * @return array 
  144. */ 
  145. public static function prepare_attributes( $data = false ) { 
  146. $attributes = array(); 
  147.  
  148. if ( ! $data ) { 
  149. $data = $_POST; 
  150.  
  151. if ( isset( $data['attribute_names'], $data['attribute_values'] ) ) { 
  152. $attribute_names = $data['attribute_names']; 
  153. $attribute_values = $data['attribute_values']; 
  154. $attribute_visibility = isset( $data['attribute_visibility'] ) ? $data['attribute_visibility'] : array(); 
  155. $attribute_variation = isset( $data['attribute_variation'] ) ? $data['attribute_variation'] : array(); 
  156. $attribute_position = $data['attribute_position']; 
  157. $attribute_names_max_key = max( array_keys( $attribute_names ) ); 
  158.  
  159. for ( $i = 0; $i <= $attribute_names_max_key; $i++ ) { 
  160. if ( empty( $attribute_names[ $i ] ) || ! isset( $attribute_values[ $i ] ) ) { 
  161. continue; 
  162. $attribute_id = 0; 
  163. $attribute_name = wc_clean( $attribute_names[ $i ] ); 
  164.  
  165. if ( 'pa_' === substr( $attribute_name, 0, 3 ) ) { 
  166. $attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_name ); 
  167.  
  168. $options = isset( $attribute_values[ $i ] ) ? $attribute_values[ $i ] : ''; 
  169.  
  170. if ( is_array( $options ) ) { 
  171. // Term ids sent as array. 
  172. $options = wp_parse_id_list( $options ); 
  173. } else { 
  174. // Terms or text sent in textarea. 
  175. $options = 0 < $attribute_id ? wc_sanitize_textarea( wc_sanitize_term_text_based( $options ) ) : wc_sanitize_textarea( $options ); 
  176. $options = wc_get_text_attributes( $options ); 
  177.  
  178. if ( empty( $options ) ) { 
  179. continue; 
  180.  
  181. $attribute = new WC_Product_Attribute(); 
  182. $attribute->set_id( $attribute_id ); 
  183. $attribute->set_name( $attribute_name ); 
  184. $attribute->set_options( $options ); 
  185. $attribute->set_position( $attribute_position[ $i ] ); 
  186. $attribute->set_visible( isset( $attribute_visibility[ $i ] ) ); 
  187. $attribute->set_variation( isset( $attribute_variation[ $i ] ) ); 
  188. $attributes[] = $attribute; 
  189. return $attributes; 
  190.  
  191. /** 
  192. * Prepare attributes for a specific variation or defaults. 
  193. * @param array $all_attributes 
  194. * @param string $key_prefix 
  195. * @param int $index 
  196. * @return array 
  197. */ 
  198. private static function prepare_set_attributes( $all_attributes, $key_prefix = 'attribute_', $index = null ) { 
  199. $attributes = array(); 
  200.  
  201. if ( $all_attributes ) { 
  202. foreach ( $all_attributes as $attribute ) { 
  203. if ( $attribute->get_variation() ) { 
  204. $attribute_key = sanitize_title( $attribute->get_name() ); 
  205.  
  206. if ( ! is_null( $index ) ) { 
  207. $value = isset( $_POST[ $key_prefix . $attribute_key ][ $index ] ) ? stripslashes( $_POST[ $key_prefix . $attribute_key ][ $index ] ) : ''; 
  208. } else { 
  209. $value = isset( $_POST[ $key_prefix . $attribute_key ] ) ? stripslashes( $_POST[ $key_prefix . $attribute_key ] ) : ''; 
  210.  
  211. $value = $attribute->is_taxonomy() ? sanitize_title( $value ) : wc_clean( $value ); // Don't use wc_clean as it destroys sanitized characters in terms. 
  212. $attributes[ $attribute_key ] = $value; 
  213.  
  214. return $attributes; 
  215.  
  216. /** 
  217. * Save meta box data. 
  218. */ 
  219. public static function save( $post_id, $post ) { 
  220. // Process product type first so we have the correct class to run setters. 
  221. $product_type = empty( $_POST['product-type'] ) ? WC_Product_Factory::get_product_type( $post_id ) : sanitize_title( stripslashes( $_POST['product-type'] ) ); 
  222. $classname = WC_Product_Factory::get_product_classname( $post_id, $product_type ? $product_type : 'simple' ); 
  223. $product = new $classname( $post_id ); 
  224. $attributes = self::prepare_attributes(); 
  225. $errors = $product->set_props( array( 
  226. 'sku' => isset( $_POST['_sku'] ) ? wc_clean( $_POST['_sku'] ) : null,  
  227. 'purchase_note' => wp_kses_post( stripslashes( $_POST['_purchase_note'] ) ),  
  228. 'downloadable' => isset( $_POST['_downloadable'] ),  
  229. 'virtual' => isset( $_POST['_virtual'] ),  
  230. 'featured' => isset( $_POST['_featured'] ),  
  231. 'catalog_visibility' => wc_clean( $_POST['_visibility'] ),  
  232. 'tax_status' => isset( $_POST['_tax_status'] ) ? wc_clean( $_POST['_tax_status'] ) : null,  
  233. 'tax_class' => isset( $_POST['_tax_class'] ) ? wc_clean( $_POST['_tax_class'] ) : null,  
  234. 'weight' => wc_clean( $_POST['_weight'] ),  
  235. 'length' => wc_clean( $_POST['_length'] ),  
  236. 'width' => wc_clean( $_POST['_width'] ),  
  237. 'height' => wc_clean( $_POST['_height'] ),  
  238. 'shipping_class_id' => absint( $_POST['product_shipping_class'] ),  
  239. 'sold_individually' => ! empty( $_POST['_sold_individually'] ),  
  240. 'upsell_ids' => isset( $_POST['upsell_ids'] ) ? array_map( 'intval', (array) $_POST['upsell_ids'] ) : array(),  
  241. 'cross_sell_ids' => isset( $_POST['crosssell_ids'] ) ? array_map( 'intval', (array) $_POST['crosssell_ids'] ) : array(),  
  242. 'regular_price' => wc_clean( $_POST['_regular_price'] ),  
  243. 'sale_price' => wc_clean( $_POST['_sale_price'] ),  
  244. 'date_on_sale_from' => wc_clean( $_POST['_sale_price_dates_from'] ),  
  245. 'date_on_sale_to' => wc_clean( $_POST['_sale_price_dates_to'] ),  
  246. 'manage_stock' => ! empty( $_POST['_manage_stock'] ),  
  247. 'backorders' => wc_clean( $_POST['_backorders'] ),  
  248. 'stock_status' => wc_clean( $_POST['_stock_status'] ),  
  249. 'stock_quantity' => wc_stock_amount( $_POST['_stock'] ),  
  250. 'download_limit' => '' === $_POST['_download_limit'] ? '' : absint( $_POST['_download_limit'] ),  
  251. 'download_expiry' => '' === $_POST['_download_expiry'] ? '' : absint( $_POST['_download_expiry'] ),  
  252. 'downloads' => self::prepare_downloads( 
  253. isset( $_POST['_wc_file_names'] ) ? $_POST['_wc_file_names'] : array(),  
  254. isset( $_POST['_wc_file_urls'] ) ? $_POST['_wc_file_urls'] : array(),  
  255. isset( $_POST['_wc_file_hashes'] ) ? $_POST['_wc_file_hashes'] : array() 
  256. ),  
  257. 'product_url' => esc_url_raw( $_POST['_product_url'] ),  
  258. 'button_text' => wc_clean( $_POST['_button_text'] ),  
  259. 'children' => 'grouped' === $product_type ? self::prepare_children() : null,  
  260. 'reviews_allowed' => ! empty( $_POST['_reviews_allowed'] ),  
  261. 'attributes' => $attributes,  
  262. 'default_attributes' => self::prepare_set_attributes( $attributes, 'default_attribute_' ),  
  263. ) ); 
  264.  
  265. if ( is_wp_error( $errors ) ) { 
  266. WC_Admin_Meta_Boxes::add_error( $errors->get_error_message() ); 
  267.  
  268. /** 
  269. * @since 3.0.0 to set props before save. 
  270. */ 
  271. do_action( 'woocommerce_admin_process_product_object', $product ); 
  272.  
  273. $product->save(); 
  274.  
  275. if ( $product->is_type( 'variable' ) ) { 
  276. $product->get_data_store()->sync_variation_names( $product, wc_clean( $_POST['original_post_title'] ), wc_clean( $_POST['post_title'] ) ); 
  277.  
  278. do_action( 'woocommerce_process_product_meta_' . $product_type, $post_id ); 
  279.  
  280. /** 
  281. * Save meta box data. 
  282. * @param int $post_id 
  283. * @param WP_Post $post 
  284. */ 
  285. public static function save_variations( $post_id, $post ) { 
  286. if ( isset( $_POST['variable_post_id'] ) ) { 
  287. $parent = wc_get_product( $post_id ); 
  288. $parent->set_default_attributes( self::prepare_set_attributes( $parent->get_attributes(), 'default_attribute_' ) ); 
  289. $parent->save(); 
  290.  
  291. $max_loop = max( array_keys( $_POST['variable_post_id'] ) ); 
  292. $data_store = $parent->get_data_store(); 
  293. $data_store->sort_all_product_variations( $parent->get_id() ); 
  294.  
  295. for ( $i = 0; $i <= $max_loop; $i ++ ) { 
  296.  
  297. if ( ! isset( $_POST['variable_post_id'][ $i ] ) ) { 
  298. continue; 
  299. $variation_id = absint( $_POST['variable_post_id'][ $i ] ); 
  300. $variation = new WC_Product_Variation( $variation_id ); 
  301. $errors = $variation->set_props( array( 
  302. 'status' => isset( $_POST['variable_enabled'][ $i ] ) ? 'publish' : 'private',  
  303. 'menu_order' => wc_clean( $_POST['variation_menu_order'][ $i ] ),  
  304. 'regular_price' => wc_clean( $_POST['variable_regular_price'][ $i ] ),  
  305. 'sale_price' => wc_clean( $_POST['variable_sale_price'][ $i ] ),  
  306. 'virtual' => isset( $_POST['variable_is_virtual'][ $i ] ),  
  307. 'downloadable' => isset( $_POST['variable_is_downloadable'][ $i ] ),  
  308. 'date_on_sale_from' => wc_clean( $_POST['variable_sale_price_dates_from'][ $i ] ),  
  309. 'date_on_sale_to' => wc_clean( $_POST['variable_sale_price_dates_to'][ $i ] ),  
  310. 'description' => wp_kses_post( $_POST['variable_description'][ $i ] ),  
  311. 'download_limit' => wc_clean( $_POST['variable_download_limit'][ $i ] ),  
  312. 'download_expiry' => wc_clean( $_POST['variable_download_expiry'][ $i ] ),  
  313. 'downloads' => self::prepare_downloads( 
  314. isset( $_POST['_wc_variation_file_names'][ $variation_id ] ) ? $_POST['_wc_variation_file_names'][ $variation_id ] : array(),  
  315. isset( $_POST['_wc_variation_file_urls'][ $variation_id ] ) ? $_POST['_wc_variation_file_urls'][ $variation_id ] : array(),  
  316. isset( $_POST['_wc_variation_file_hashes'][ $variation_id ] ) ? $_POST['_wc_variation_file_hashes'][ $variation_id ] : array() 
  317. ),  
  318. 'manage_stock' => isset( $_POST['variable_manage_stock'][ $i ] ),  
  319. 'stock_quantity' => wc_clean( $_POST['variable_stock'][ $i ] ),  
  320. 'backorders' => wc_clean( $_POST['variable_backorders'][ $i ] ),  
  321. 'stock_status' => wc_clean( $_POST['variable_stock_status'][ $i ] ),  
  322. 'image_id' => wc_clean( $_POST['upload_image_id'][ $i ] ),  
  323. 'attributes' => self::prepare_set_attributes( $parent->get_attributes(), 'attribute_', $i ),  
  324. 'sku' => isset( $_POST['variable_sku'][ $i ] ) ? wc_clean( $_POST['variable_sku'][ $i ] ) : '',  
  325. 'weight' => isset( $_POST['variable_weight'][ $i ] ) ? wc_clean( $_POST['variable_weight'][ $i ] ) : '',  
  326. 'length' => isset( $_POST['variable_length'][ $i ] ) ? wc_clean( $_POST['variable_length'][ $i ] ) : '',  
  327. 'width' => isset( $_POST['variable_width'][ $i ] ) ? wc_clean( $_POST['variable_width'][ $i ] ) : '',  
  328. 'height' => isset( $_POST['variable_height'][ $i ] ) ? wc_clean( $_POST['variable_height'][ $i ] ) : '',  
  329. 'shipping_class_id' => wc_clean( $_POST['variable_shipping_class'][ $i ] ),  
  330. 'tax_class' => isset( $_POST['variable_tax_class'][ $i ] ) ? wc_clean( $_POST['variable_tax_class'][ $i ] ) : null,  
  331. ) ); 
  332.  
  333. if ( is_wp_error( $errors ) ) { 
  334. WC_Admin_Meta_Boxes::add_error( $errors->get_error_message() ); 
  335.  
  336. $variation->save(); 
  337.  
  338. do_action( 'woocommerce_save_product_variation', $variation_id, $i );