class-wc-meta-box-product-data.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. <?php
  2. /**
  3. * Product Data
  4. *
  5. * Displays the product data box, tabbed, with several panels covering price, stock etc.
  6. *
  7. * @author WooThemes
  8. * @category Admin
  9. * @package WooCommerce/Admin/Meta Boxes
  10. * @version 3.0.0
  11. */
  12. if ( ! defined( 'ABSPATH' ) ) {
  13. exit;
  14. }
  15. /**
  16. * WC_Meta_Box_Product_Data Class.
  17. */
  18. class WC_Meta_Box_Product_Data {
  19. /**
  20. * Output the metabox.
  21. *
  22. * @param WP_Post $post
  23. */
  24. public static function output( $post ) {
  25. global $thepostid, $product_object;
  26. $thepostid = $post->ID;
  27. $product_object = $thepostid ? wc_get_product( $thepostid ) : new WC_Product();
  28. wp_nonce_field( 'woocommerce_save_data', 'woocommerce_meta_nonce' );
  29. include 'views/html-product-data-panel.php';
  30. }
  31. /**
  32. * Show tab content/settings.
  33. */
  34. private static function output_tabs() {
  35. global $post, $thepostid, $product_object;
  36. include 'views/html-product-data-general.php';
  37. include 'views/html-product-data-inventory.php';
  38. include 'views/html-product-data-shipping.php';
  39. include 'views/html-product-data-linked-products.php';
  40. include 'views/html-product-data-attributes.php';
  41. include 'views/html-product-data-advanced.php';
  42. }
  43. /**
  44. * Return array of product type options.
  45. *
  46. * @return array
  47. */
  48. private static function get_product_type_options() {
  49. return apply_filters(
  50. 'product_type_options', array(
  51. 'virtual' => array(
  52. 'id' => '_virtual',
  53. 'wrapper_class' => 'show_if_simple',
  54. 'label' => __( 'Virtual', 'woocommerce' ),
  55. 'description' => __( 'Virtual products are intangible and are not shipped.', 'woocommerce' ),
  56. 'default' => 'no',
  57. ),
  58. 'downloadable' => array(
  59. 'id' => '_downloadable',
  60. 'wrapper_class' => 'show_if_simple',
  61. 'label' => __( 'Downloadable', 'woocommerce' ),
  62. 'description' => __( 'Downloadable products give access to a file upon purchase.', 'woocommerce' ),
  63. 'default' => 'no',
  64. ),
  65. )
  66. );
  67. }
  68. /**
  69. * Return array of tabs to show.
  70. *
  71. * @return array
  72. */
  73. private static function get_product_data_tabs() {
  74. $tabs = apply_filters(
  75. 'woocommerce_product_data_tabs', array(
  76. 'general' => array(
  77. 'label' => __( 'General', 'woocommerce' ),
  78. 'target' => 'general_product_data',
  79. 'class' => array( 'hide_if_grouped' ),
  80. 'priority' => 10,
  81. ),
  82. 'inventory' => array(
  83. 'label' => __( 'Inventory', 'woocommerce' ),
  84. 'target' => 'inventory_product_data',
  85. 'class' => array( 'show_if_simple', 'show_if_variable', 'show_if_grouped', 'show_if_external' ),
  86. 'priority' => 20,
  87. ),
  88. 'shipping' => array(
  89. 'label' => __( 'Shipping', 'woocommerce' ),
  90. 'target' => 'shipping_product_data',
  91. 'class' => array( 'hide_if_virtual', 'hide_if_grouped', 'hide_if_external' ),
  92. 'priority' => 30,
  93. ),
  94. 'linked_product' => array(
  95. 'label' => __( 'Linked Products', 'woocommerce' ),
  96. 'target' => 'linked_product_data',
  97. 'class' => array(),
  98. 'priority' => 40,
  99. ),
  100. 'attribute' => array(
  101. 'label' => __( 'Attributes', 'woocommerce' ),
  102. 'target' => 'product_attributes',
  103. 'class' => array(),
  104. 'priority' => 50,
  105. ),
  106. 'variations' => array(
  107. 'label' => __( 'Variations', 'woocommerce' ),
  108. 'target' => 'variable_product_options',
  109. 'class' => array( 'variations_tab', 'show_if_variable' ),
  110. 'priority' => 60,
  111. ),
  112. 'advanced' => array(
  113. 'label' => __( 'Advanced', 'woocommerce' ),
  114. 'target' => 'advanced_product_data',
  115. 'class' => array(),
  116. 'priority' => 70,
  117. ),
  118. )
  119. );
  120. // Sort tabs based on priority.
  121. uasort( $tabs, array( __CLASS__, 'product_data_tabs_sort' ) );
  122. return $tabs;
  123. }
  124. /**
  125. * Callback to sort product data tabs on priority.
  126. *
  127. * @since 3.1.0
  128. * @param int $a First item.
  129. * @param int $b Second item.
  130. *
  131. * @return bool
  132. */
  133. private static function product_data_tabs_sort( $a, $b ) {
  134. if ( ! isset( $a['priority'], $b['priority'] ) ) {
  135. return -1;
  136. }
  137. if ( $a['priority'] == $b['priority'] ) {
  138. return 0;
  139. }
  140. return $a['priority'] < $b['priority'] ? -1 : 1;
  141. }
  142. /**
  143. * Filter callback for finding variation attributes.
  144. *
  145. * @param WC_Product_Attribute $attribute
  146. * @return bool
  147. */
  148. private static function filter_variation_attributes( $attribute ) {
  149. return true === $attribute->get_variation();
  150. }
  151. /**
  152. * Show options for the variable product type.
  153. */
  154. public static function output_variations() {
  155. global $post, $wpdb, $product_object;
  156. $variation_attributes = array_filter( $product_object->get_attributes(), array( __CLASS__, 'filter_variation_attributes' ) );
  157. $default_attributes = $product_object->get_default_attributes();
  158. $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 ) ) );
  159. $variations_per_page = absint( apply_filters( 'woocommerce_admin_meta_boxes_variations_per_page', 15 ) );
  160. $variations_total_pages = ceil( $variations_count / $variations_per_page );
  161. include 'views/html-product-data-variations.php';
  162. }
  163. /**
  164. * Prepare downloads for save.
  165. *
  166. * @param array $file_names
  167. * @param array $file_urls
  168. * @param array $file_hashes
  169. *
  170. * @return array
  171. */
  172. private static function prepare_downloads( $file_names, $file_urls, $file_hashes ) {
  173. $downloads = array();
  174. if ( ! empty( $file_urls ) ) {
  175. $file_url_size = sizeof( $file_urls );
  176. for ( $i = 0; $i < $file_url_size; $i ++ ) {
  177. if ( ! empty( $file_urls[ $i ] ) ) {
  178. $downloads[] = array(
  179. 'name' => wc_clean( $file_names[ $i ] ),
  180. 'file' => wp_unslash( trim( $file_urls[ $i ] ) ),
  181. 'download_id' => wc_clean( $file_hashes[ $i ] ),
  182. );
  183. }
  184. }
  185. }
  186. return $downloads;
  187. }
  188. /**
  189. * Prepare children for save.
  190. *
  191. * @return array
  192. */
  193. private static function prepare_children() {
  194. return isset( $_POST['grouped_products'] ) ? array_filter( array_map( 'intval', (array) $_POST['grouped_products'] ) ) : array();
  195. }
  196. /**
  197. * Prepare attributes for save.
  198. *
  199. * @param array $data
  200. *
  201. * @return array
  202. */
  203. public static function prepare_attributes( $data = false ) {
  204. $attributes = array();
  205. if ( ! $data ) {
  206. $data = $_POST;
  207. }
  208. if ( isset( $data['attribute_names'], $data['attribute_values'] ) ) {
  209. $attribute_names = $data['attribute_names'];
  210. $attribute_values = $data['attribute_values'];
  211. $attribute_visibility = isset( $data['attribute_visibility'] ) ? $data['attribute_visibility'] : array();
  212. $attribute_variation = isset( $data['attribute_variation'] ) ? $data['attribute_variation'] : array();
  213. $attribute_position = $data['attribute_position'];
  214. $attribute_names_max_key = max( array_keys( $attribute_names ) );
  215. for ( $i = 0; $i <= $attribute_names_max_key; $i++ ) {
  216. if ( empty( $attribute_names[ $i ] ) || ! isset( $attribute_values[ $i ] ) ) {
  217. continue;
  218. }
  219. $attribute_id = 0;
  220. $attribute_name = wc_clean( $attribute_names[ $i ] );
  221. if ( 'pa_' === substr( $attribute_name, 0, 3 ) ) {
  222. $attribute_id = wc_attribute_taxonomy_id_by_name( $attribute_name );
  223. }
  224. $options = isset( $attribute_values[ $i ] ) ? $attribute_values[ $i ] : '';
  225. if ( is_array( $options ) ) {
  226. // Term ids sent as array.
  227. $options = wp_parse_id_list( $options );
  228. } else {
  229. // Terms or text sent in textarea.
  230. $options = 0 < $attribute_id ? wc_sanitize_textarea( wc_sanitize_term_text_based( $options ) ) : wc_sanitize_textarea( $options );
  231. $options = wc_get_text_attributes( $options );
  232. }
  233. if ( empty( $options ) ) {
  234. continue;
  235. }
  236. $attribute = new WC_Product_Attribute();
  237. $attribute->set_id( $attribute_id );
  238. $attribute->set_name( $attribute_name );
  239. $attribute->set_options( $options );
  240. $attribute->set_position( $attribute_position[ $i ] );
  241. $attribute->set_visible( isset( $attribute_visibility[ $i ] ) );
  242. $attribute->set_variation( isset( $attribute_variation[ $i ] ) );
  243. $attributes[] = $attribute;
  244. }
  245. }
  246. return $attributes;
  247. }
  248. /**
  249. * Prepare attributes for a specific variation or defaults.
  250. *
  251. * @param array $all_attributes
  252. * @param string $key_prefix
  253. * @param int $index
  254. * @return array
  255. */
  256. private static function prepare_set_attributes( $all_attributes, $key_prefix = 'attribute_', $index = null ) {
  257. $attributes = array();
  258. if ( $all_attributes ) {
  259. foreach ( $all_attributes as $attribute ) {
  260. if ( $attribute->get_variation() ) {
  261. $attribute_key = sanitize_title( $attribute->get_name() );
  262. if ( ! is_null( $index ) ) {
  263. $value = isset( $_POST[ $key_prefix . $attribute_key ][ $index ] ) ? wp_unslash( $_POST[ $key_prefix . $attribute_key ][ $index ] ) : '';
  264. } else {
  265. $value = isset( $_POST[ $key_prefix . $attribute_key ] ) ? wp_unslash( $_POST[ $key_prefix . $attribute_key ] ) : '';
  266. }
  267. if ( $attribute->is_taxonomy() ) {
  268. // Don't use wc_clean as it destroys sanitized characters.
  269. $value = sanitize_title( $value );
  270. } else {
  271. $value = html_entity_decode( wc_clean( $value ), ENT_QUOTES, get_bloginfo( 'charset' ) ); // WPCS: sanitization ok.
  272. }
  273. $attributes[ $attribute_key ] = $value;
  274. }
  275. }
  276. }
  277. return $attributes;
  278. }
  279. /**
  280. * Save meta box data.
  281. *
  282. * @param int $post_id
  283. * @param $post
  284. */
  285. public static function save( $post_id, $post ) {
  286. // Process product type first so we have the correct class to run setters.
  287. $product_type = empty( $_POST['product-type'] ) ? WC_Product_Factory::get_product_type( $post_id ) : sanitize_title( stripslashes( $_POST['product-type'] ) );
  288. $classname = WC_Product_Factory::get_product_classname( $post_id, $product_type ? $product_type : 'simple' );
  289. $product = new $classname( $post_id );
  290. $attributes = self::prepare_attributes();
  291. $stock = null;
  292. // Handle stock changes.
  293. if ( isset( $_POST['_stock'] ) ) {
  294. if ( isset( $_POST['_original_stock'] ) && wc_stock_amount( $product->get_stock_quantity( 'edit' ) ) !== wc_stock_amount( $_POST['_original_stock'] ) ) {
  295. /* translators: 1: product ID 2: quantity in stock */
  296. WC_Admin_Meta_Boxes::add_error( sprintf( __( 'The stock has not been updated because the value has changed since editing. Product %1$d has %2$d units in stock.', 'woocommerce' ), $product->get_id(), $product->get_stock_quantity( 'edit' ) ) );
  297. } else {
  298. $stock = wc_stock_amount( wp_unslash( $_POST['_stock'] ) );
  299. }
  300. }
  301. $errors = $product->set_props(
  302. array(
  303. 'sku' => isset( $_POST['_sku'] ) ? wc_clean( wp_unslash( $_POST['_sku'] ) ) : null,
  304. 'purchase_note' => wp_kses_post( wp_unslash( $_POST['_purchase_note'] ) ),
  305. 'downloadable' => isset( $_POST['_downloadable'] ),
  306. 'virtual' => isset( $_POST['_virtual'] ),
  307. 'featured' => isset( $_POST['_featured'] ),
  308. 'catalog_visibility' => wc_clean( wp_unslash( $_POST['_visibility'] ) ),
  309. 'tax_status' => isset( $_POST['_tax_status'] ) ? wc_clean( wp_unslash( $_POST['_tax_status'] ) ) : null,
  310. 'tax_class' => isset( $_POST['_tax_class'] ) ? wc_clean( wp_unslash( $_POST['_tax_class'] ) ) : null,
  311. 'weight' => wc_clean( wp_unslash( $_POST['_weight'] ) ),
  312. 'length' => wc_clean( wp_unslash( $_POST['_length'] ) ),
  313. 'width' => wc_clean( wp_unslash( $_POST['_width'] ) ),
  314. 'height' => wc_clean( wp_unslash( $_POST['_height'] ) ),
  315. 'shipping_class_id' => absint( wp_unslash( $_POST['product_shipping_class'] ) ),
  316. 'sold_individually' => ! empty( $_POST['_sold_individually'] ),
  317. 'upsell_ids' => isset( $_POST['upsell_ids'] ) ? array_map( 'intval', (array) wp_unslash( $_POST['upsell_ids'] ) ) : array(),
  318. 'cross_sell_ids' => isset( $_POST['crosssell_ids'] ) ? array_map( 'intval', (array) wp_unslash( $_POST['crosssell_ids'] ) ) : array(),
  319. 'regular_price' => wc_clean( wp_unslash( $_POST['_regular_price'] ) ),
  320. 'sale_price' => wc_clean( wp_unslash( $_POST['_sale_price'] ) ),
  321. 'date_on_sale_from' => wc_clean( wp_unslash( $_POST['_sale_price_dates_from'] ) ),
  322. 'date_on_sale_to' => wc_clean( wp_unslash( $_POST['_sale_price_dates_to'] ) ),
  323. 'manage_stock' => ! empty( $_POST['_manage_stock'] ),
  324. 'backorders' => isset( $_POST['_backorders'] ) ? wc_clean( wp_unslash( $_POST['_backorders'] ) ) : null,
  325. 'stock_status' => wc_clean( wp_unslash( $_POST['_stock_status'] ) ),
  326. 'stock_quantity' => $stock,
  327. 'download_limit' => '' === $_POST['_download_limit'] ? '' : absint( wp_unslash( $_POST['_download_limit'] ) ),
  328. 'download_expiry' => '' === $_POST['_download_expiry'] ? '' : absint( wp_unslash( $_POST['_download_expiry'] ) ),
  329. 'downloads' => self::prepare_downloads(
  330. isset( $_POST['_wc_file_names'] ) ? wp_unslash( $_POST['_wc_file_names'] ) : array(),
  331. isset( $_POST['_wc_file_urls'] ) ? wp_unslash( $_POST['_wc_file_urls'] ) : array(),
  332. isset( $_POST['_wc_file_hashes'] ) ? wp_unslash( $_POST['_wc_file_hashes'] ) : array()
  333. ),
  334. 'product_url' => esc_url_raw( wp_unslash( $_POST['_product_url'] ) ),
  335. 'button_text' => wc_clean( wp_unslash( $_POST['_button_text'] ) ),
  336. 'children' => 'grouped' === $product_type ? self::prepare_children() : null,
  337. 'reviews_allowed' => ! empty( $_POST['comment_status'] ) && 'open' === $_POST['comment_status'],
  338. 'attributes' => $attributes,
  339. 'default_attributes' => self::prepare_set_attributes( $attributes, 'default_attribute_' ),
  340. )
  341. );
  342. if ( is_wp_error( $errors ) ) {
  343. WC_Admin_Meta_Boxes::add_error( $errors->get_error_message() );
  344. }
  345. /**
  346. * @since 3.0.0 to set props before save.
  347. */
  348. do_action( 'woocommerce_admin_process_product_object', $product );
  349. $product->save();
  350. if ( $product->is_type( 'variable' ) ) {
  351. $product->get_data_store()->sync_variation_names( $product, wc_clean( $_POST['original_post_title'] ), wc_clean( $_POST['post_title'] ) );
  352. }
  353. do_action( 'woocommerce_process_product_meta_' . $product_type, $post_id );
  354. }
  355. /**
  356. * Save meta box data.
  357. *
  358. * @param int $post_id
  359. * @param WP_Post $post
  360. */
  361. public static function save_variations( $post_id, $post ) {
  362. if ( isset( $_POST['variable_post_id'] ) ) {
  363. $parent = wc_get_product( $post_id );
  364. $parent->set_default_attributes( self::prepare_set_attributes( $parent->get_attributes(), 'default_attribute_' ) );
  365. $parent->save();
  366. $max_loop = max( array_keys( $_POST['variable_post_id'] ) );
  367. $data_store = $parent->get_data_store();
  368. $data_store->sort_all_product_variations( $parent->get_id() );
  369. for ( $i = 0; $i <= $max_loop; $i ++ ) {
  370. if ( ! isset( $_POST['variable_post_id'][ $i ] ) ) {
  371. continue;
  372. }
  373. $variation_id = absint( $_POST['variable_post_id'][ $i ] );
  374. $variation = new WC_Product_Variation( $variation_id );
  375. $stock = null;
  376. // Handle stock changes.
  377. if ( isset( $_POST['variable_stock'], $_POST['variable_stock'][ $i ] ) ) {
  378. if ( isset( $_POST['variable_original_stock'], $_POST['variable_original_stock'][ $i ] ) && wc_stock_amount( $variation->get_stock_quantity( 'edit' ) ) !== wc_stock_amount( $_POST['variable_original_stock'][ $i ] ) ) {
  379. /* translators: 1: product ID 2: quantity in stock */
  380. WC_Admin_Meta_Boxes::add_error( sprintf( __( 'The stock has not been updated because the value has changed since editing. Product %1$d has %2$d units in stock.', 'woocommerce' ), $variation->get_id(), $variation->get_stock_quantity( 'edit' ) ) );
  381. } else {
  382. $stock = wc_stock_amount( $_POST['variable_stock'][ $i ] );
  383. }
  384. }
  385. $errors = $variation->set_props(
  386. array(
  387. 'status' => isset( $_POST['variable_enabled'][ $i ] ) ? 'publish' : 'private',
  388. 'menu_order' => wc_clean( $_POST['variation_menu_order'][ $i ] ),
  389. 'regular_price' => wc_clean( $_POST['variable_regular_price'][ $i ] ),
  390. 'sale_price' => wc_clean( $_POST['variable_sale_price'][ $i ] ),
  391. 'virtual' => isset( $_POST['variable_is_virtual'][ $i ] ),
  392. 'downloadable' => isset( $_POST['variable_is_downloadable'][ $i ] ),
  393. 'date_on_sale_from' => wc_clean( $_POST['variable_sale_price_dates_from'][ $i ] ),
  394. 'date_on_sale_to' => wc_clean( $_POST['variable_sale_price_dates_to'][ $i ] ),
  395. 'description' => wp_kses_post( $_POST['variable_description'][ $i ] ),
  396. 'download_limit' => wc_clean( $_POST['variable_download_limit'][ $i ] ),
  397. 'download_expiry' => wc_clean( $_POST['variable_download_expiry'][ $i ] ),
  398. 'downloads' => self::prepare_downloads(
  399. isset( $_POST['_wc_variation_file_names'][ $variation_id ] ) ? $_POST['_wc_variation_file_names'][ $variation_id ] : array(),
  400. isset( $_POST['_wc_variation_file_urls'][ $variation_id ] ) ? $_POST['_wc_variation_file_urls'][ $variation_id ] : array(),
  401. isset( $_POST['_wc_variation_file_hashes'][ $variation_id ] ) ? $_POST['_wc_variation_file_hashes'][ $variation_id ] : array()
  402. ),
  403. 'manage_stock' => isset( $_POST['variable_manage_stock'][ $i ] ),
  404. 'stock_quantity' => $stock,
  405. 'backorders' => isset( $_POST['variable_backorders'], $_POST['variable_backorders'][ $i ] ) ? wc_clean( $_POST['variable_backorders'][ $i ] ) : null,
  406. 'stock_status' => wc_clean( $_POST['variable_stock_status'][ $i ] ),
  407. 'image_id' => wc_clean( $_POST['upload_image_id'][ $i ] ),
  408. 'attributes' => self::prepare_set_attributes( $parent->get_attributes(), 'attribute_', $i ),
  409. 'sku' => isset( $_POST['variable_sku'][ $i ] ) ? wc_clean( $_POST['variable_sku'][ $i ] ) : '',
  410. 'weight' => isset( $_POST['variable_weight'][ $i ] ) ? wc_clean( wp_unslash( $_POST['variable_weight'][ $i ] ) ) : '',
  411. 'length' => isset( $_POST['variable_length'][ $i ] ) ? wc_clean( $_POST['variable_length'][ $i ] ) : '',
  412. 'width' => isset( $_POST['variable_width'][ $i ] ) ? wc_clean( $_POST['variable_width'][ $i ] ) : '',
  413. 'height' => isset( $_POST['variable_height'][ $i ] ) ? wc_clean( $_POST['variable_height'][ $i ] ) : '',
  414. 'shipping_class_id' => wc_clean( $_POST['variable_shipping_class'][ $i ] ),
  415. 'tax_class' => isset( $_POST['variable_tax_class'][ $i ] ) ? wc_clean( $_POST['variable_tax_class'][ $i ] ) : null,
  416. )
  417. );
  418. if ( is_wp_error( $errors ) ) {
  419. WC_Admin_Meta_Boxes::add_error( $errors->get_error_message() );
  420. }
  421. $variation->save();
  422. do_action( 'woocommerce_save_product_variation', $variation_id, $i );
  423. }
  424. }
  425. }
  426. }