WC_Admin_Duplicate_Product::product_duplicate()
Function to create the duplicate of the product.
Method of the class: WC_Admin_Duplicate_Product{}
Hooks from the method
Return
WC_Product
. The duplicate.
Usage
$WC_Admin_Duplicate_Product = new WC_Admin_Duplicate_Product(); $WC_Admin_Duplicate_Product->product_duplicate( $product );
- $product(WC_Product) (required)
- The product to duplicate.
WC_Admin_Duplicate_Product::product_duplicate() WC Admin Duplicate Product::product duplicate code WC 7.5.1
public function product_duplicate( $product ) { /** * Filter to allow us to exclude meta keys from product duplication.. * * @param array $exclude_meta The keys to exclude from the duplicate. * @param array $existing_meta_keys The meta keys that the product already has. * @since 2.6 */ $meta_to_exclude = array_filter( apply_filters( 'woocommerce_duplicate_product_exclude_meta', array(), array_map( function ( $datum ) { return $datum->key; }, $product->get_meta_data() ) ) ); $duplicate = clone $product; $duplicate->set_id( 0 ); /* translators: %s contains the name of the original product. */ $duplicate->set_name( sprintf( esc_html__( '%s (Copy)', 'woocommerce' ), $duplicate->get_name() ) ); $duplicate->set_total_sales( 0 ); if ( '' !== $product->get_sku( 'edit' ) ) { $duplicate->set_sku( wc_product_generate_unique_sku( 0, $product->get_sku( 'edit' ) ) ); } $duplicate->set_status( 'draft' ); $duplicate->set_date_created( null ); $duplicate->set_slug( '' ); $duplicate->set_rating_counts( 0 ); $duplicate->set_average_rating( 0 ); $duplicate->set_review_count( 0 ); foreach ( $meta_to_exclude as $meta_key ) { $duplicate->delete_meta_data( $meta_key ); } /** * This action can be used to modify the object further before it is created - it will be passed by reference. * * @since 3.0 */ do_action( 'woocommerce_product_duplicate_before_save', $duplicate, $product ); // Save parent product. $duplicate->save(); // Duplicate children of a variable product. if ( ! apply_filters( 'woocommerce_duplicate_product_exclude_children', false, $product ) && $product->is_type( 'variable' ) ) { foreach ( $product->get_children() as $child_id ) { $child = wc_get_product( $child_id ); $child_duplicate = clone $child; $child_duplicate->set_parent_id( $duplicate->get_id() ); $child_duplicate->set_id( 0 ); $child_duplicate->set_date_created( null ); // If we wait and let the insertion generate the slug, we will see extreme performance degradation // in the case where a product is used as a template. Every time the template is duplicated, each // variation will query every consecutive slug until it finds an empty one. To avoid this, we can // optimize the generation ourselves, avoiding the issue altogether. $this->generate_unique_slug( $child_duplicate ); if ( '' !== $child->get_sku( 'edit' ) ) { $child_duplicate->set_sku( wc_product_generate_unique_sku( 0, $child->get_sku( 'edit' ) ) ); } foreach ( $meta_to_exclude as $meta_key ) { $child_duplicate->delete_meta_data( $meta_key ); } /** * This action can be used to modify the object further before it is created - it will be passed by reference. * * @since 3.0 */ do_action( 'woocommerce_product_duplicate_before_save', $child_duplicate, $child ); $child_duplicate->save(); } // Get new object to reflect new children. $duplicate = wc_get_product( $duplicate->get_id() ); } return $duplicate; }