WC_Admin_Duplicate_Product

WC_Admin_Duplicate_Product Class.

Defined (1)

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

/includes/admin/class-wc-admin-duplicate-product.php  
  1. class WC_Admin_Duplicate_Product { 
  2.  
  3. /** 
  4. * Constructor. 
  5. */ 
  6. public function __construct() { 
  7. add_action( 'admin_action_duplicate_product', array( $this, 'duplicate_product_action' ) ); 
  8. add_filter( 'post_row_actions', array( $this, 'dupe_link' ), 10, 2 ); 
  9. add_action( 'post_submitbox_start', array( $this, 'dupe_button' ) ); 
  10.  
  11. /** 
  12. * Show the "Duplicate" link in admin products list. 
  13. * @param array $actions 
  14. * @param WP_Post $post Post object 
  15. * @return array 
  16. */ 
  17. public function dupe_link( $actions, $post ) { 
  18. if ( ! current_user_can( apply_filters( 'woocommerce_duplicate_product_capability', 'manage_woocommerce' ) ) ) { 
  19. return $actions; 
  20.  
  21. if ( 'product' !== $post->post_type ) { 
  22. return $actions; 
  23.  
  24. $actions['duplicate'] = '<a href="' . wp_nonce_url( admin_url( 'edit.php?post_type=product&action=duplicate_product&post=' . $post->ID ), 'woocommerce-duplicate-product_' . $post->ID ) . '" aria-label="' . esc_attr__( 'Make a duplicate from this product', 'woocommerce' ) 
  25. . '" rel="permalink">' . __( 'Duplicate', 'woocommerce' ) . '</a>'; 
  26.  
  27. return $actions; 
  28.  
  29. /** 
  30. * Show the dupe product link in admin. 
  31. */ 
  32. public function dupe_button() { 
  33. global $post; 
  34.  
  35. if ( ! current_user_can( apply_filters( 'woocommerce_duplicate_product_capability', 'manage_woocommerce' ) ) ) { 
  36. return; 
  37.  
  38. if ( ! is_object( $post ) ) { 
  39. return; 
  40.  
  41. if ( 'product' !== $post->post_type ) { 
  42. return; 
  43.  
  44. if ( isset( $_GET['post'] ) ) { 
  45. $notify_url = wp_nonce_url( admin_url( "edit.php?post_type=product&action=duplicate_product&post=" . absint( $_GET['post'] ) ), 'woocommerce-duplicate-product_' . $_GET['post'] ); 
  46. ?> 
  47. <div id="duplicate-action"><a class="submitduplicate duplication" href="<?php echo esc_url( $notify_url ); ?>"><?php _e( 'Copy to a new draft', 'woocommerce' ); ?></a></div> 
  48. <?php 
  49.  
  50. /** 
  51. * Duplicate a product action. 
  52. */ 
  53. public function duplicate_product_action() { 
  54. if ( empty( $_REQUEST['post'] ) ) { 
  55. wp_die( __( 'No product to duplicate has been supplied!', 'woocommerce' ) ); 
  56.  
  57. $product_id = isset( $_REQUEST['post'] ) ? absint( $_REQUEST['post'] ) : ''; 
  58.  
  59. check_admin_referer( 'woocommerce-duplicate-product_' . $product_id ); 
  60.  
  61. $product = wc_get_product( $product_id ); 
  62.  
  63. if ( false === $product ) { 
  64. /** translators: %s: product id */ 
  65. wp_die( sprintf( __( 'Product creation failed, could not find original product: %s', 'woocommerce' ), $product_id ) ); 
  66.  
  67. $duplicate = $this->product_duplicate( $product ); 
  68.  
  69. // Hook rename to match other woocommerce_product_* hooks, and to move away from depending on a response from the wp_posts table. 
  70. do_action( 'woocommerce_product_duplicate', $duplicate, $product ); 
  71. wc_do_deprecated_action( 'woocommerce_duplicate_product', array( $duplicate->get_id(), $this->get_product_to_duplicate( $product_id ) ), '3.0', 'Use woocommerce_product_duplicate action instead.' ); 
  72.  
  73. // Redirect to the edit screen for the new draft page 
  74. wp_redirect( admin_url( 'post.php?action=edit&post=' . $duplicate->get_id() ) ); 
  75. exit; 
  76.  
  77. /** 
  78. * Function to create the duplicate of the product. 
  79. * @param WC_Product $product 
  80. * @return WC_Product 
  81. */ 
  82. public function product_duplicate( $product ) { 
  83. // Filter to allow us to unset/remove data we don't want to copy to the duplicate. @since 2.6 
  84. $meta_to_exclude = array_filter( apply_filters( 'woocommerce_duplicate_product_exclude_meta', array() ) ); 
  85.  
  86. $duplicate = clone $product; 
  87. $duplicate->set_id( 0 ); 
  88. $duplicate->set_name( sprintf( __( '%s (Copy)', 'woocommerce' ), $duplicate->get_name() ) ); 
  89. $duplicate->set_total_sales( 0 ); 
  90. if ( '' !== $product->get_sku( 'edit' ) ) { 
  91. $duplicate->set_sku( wc_product_generate_unique_sku( 0, $product->get_sku( 'edit' ) ) ); 
  92. $duplicate->set_status( 'draft' ); 
  93. $duplicate->set_date_created( null ); 
  94. $duplicate->set_slug( '' ); 
  95. $duplicate->set_rating_counts( 0 ); 
  96. $duplicate->set_average_rating( 0 ); 
  97. $duplicate->set_review_count( 0 ); 
  98.  
  99. foreach ( $meta_to_exclude as $meta_key ) { 
  100. $duplicate->delete_meta_data( $meta_key ); 
  101.  
  102. // This action can be used to modify the object further before it is created - it will be passed by reference. @since 3.0 
  103. do_action( 'woocommerce_product_duplicate_before_save', $duplicate, $product ); 
  104.  
  105. // Save parent product. 
  106. $duplicate->save(); 
  107.  
  108. // Duplicate children of a variable product. 
  109. if ( ! apply_filters( 'woocommerce_duplicate_product_exclude_children', false ) && $product->is_type( 'variable' ) ) { 
  110. foreach ( $product->get_children() as $child_id ) { 
  111. $child = wc_get_product( $child_id ); 
  112. $child_duplicate = clone $child; 
  113. $child_duplicate->set_parent_id( $duplicate->get_id() ); 
  114. $child_duplicate->set_id( 0 ); 
  115.  
  116. if ( '' !== $child->get_sku( 'edit' ) ) { 
  117. $child_duplicate->set_sku( wc_product_generate_unique_sku( 0, $child->get_sku( 'edit' ) ) ); 
  118.  
  119. foreach ( $meta_to_exclude as $meta_key ) { 
  120. $child_duplicate->delete_meta_data( $meta_key ); 
  121.  
  122. // This action can be used to modify the object further before it is created - it will be passed by reference. @since 3.0 
  123. do_action( 'woocommerce_product_duplicate_before_save', $child_duplicate, $child ); 
  124.  
  125. $child_duplicate->save(); 
  126.  
  127. return $duplicate; 
  128.  
  129. /** 
  130. * Get a product from the database to duplicate. 
  131. * @deprecated 3.0.0 
  132. * @param mixed $id 
  133. * @return WP_Post|bool 
  134. * @see duplicate_product 
  135. */ 
  136. private function get_product_to_duplicate( $id ) { 
  137. global $wpdb; 
  138.  
  139. $id = absint( $id ); 
  140.  
  141. if ( ! $id ) { 
  142. return false; 
  143.  
  144. $post = $wpdb->get_row( $wpdb->prepare( "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} WHERE ID = %d", $id ) ); 
  145.  
  146. if ( isset( $post->post_type ) && 'revision' === $post->post_type ) { 
  147. $id = $post->post_parent; 
  148. $post = $wpdb->get_row( $wpdb->prepare( "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} WHERE ID = %d", $id ) ); 
  149.  
  150. return $post;