helper
2 weeks ago
importers
2 weeks ago
list-tables
1 week ago
marketplace-suggestions
1 year ago
meta-boxes
1 week ago
notes
3 months ago
plugin-updates
2 years ago
reports
2 weeks ago
settings
2 weeks ago
views
2 weeks ago
class-wc-admin-addons.php
10 months ago
class-wc-admin-api-keys-table-list.php
3 years ago
class-wc-admin-api-keys.php
1 month ago
class-wc-admin-assets.php
2 weeks ago
class-wc-admin-attributes.php
2 weeks ago
class-wc-admin-brands.php
2 weeks ago
class-wc-admin-customize.php
6 years ago
class-wc-admin-dashboard-setup.php
2 weeks ago
class-wc-admin-dashboard.php
2 weeks ago
class-wc-admin-duplicate-product.php
2 weeks ago
class-wc-admin-exporters.php
1 year ago
class-wc-admin-help.php
2 years ago
class-wc-admin-importers.php
1 year ago
class-wc-admin-log-table-list.php
6 months ago
class-wc-admin-marketplace-promotions.php
1 month ago
class-wc-admin-menus.php
2 weeks ago
class-wc-admin-meta-boxes.php
2 months ago
class-wc-admin-notices.php
2 weeks ago
class-wc-admin-permalink-settings.php
5 years ago
class-wc-admin-pointers.php
3 years ago
class-wc-admin-post-types.php
2 weeks ago
class-wc-admin-profile.php
1 year ago
class-wc-admin-reports.php
6 months ago
class-wc-admin-settings.php
2 weeks ago
class-wc-admin-setup-wizard.php
4 days ago
class-wc-admin-status.php
1 year ago
class-wc-admin-taxonomies.php
2 months ago
class-wc-admin-upload-downloadable-product.php
2 years ago
class-wc-admin-webhooks-table-list.php
2 months ago
class-wc-admin-webhooks.php
1 month ago
class-wc-admin.php
2 weeks ago
wc-admin-functions.php
2 weeks ago
wc-meta-box-functions.php
1 year ago
woocommerce-legacy-reports.php
1 year ago
class-wc-admin-duplicate-product.php
427 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Duplicate product functionality |
| 4 | * |
| 5 | * @package WooCommerce\Admin |
| 6 | * @version 3.0.0 |
| 7 | */ |
| 8 | |
| 9 | use Automattic\WooCommerce\Enums\ProductStatus; |
| 10 | use Automattic\WooCommerce\Enums\ProductType; |
| 11 | use Automattic\WooCommerce\Internal\ProductGallery\ProductMediaGallery; |
| 12 | |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | if ( class_exists( 'WC_Admin_Duplicate_Product', false ) ) { |
| 18 | return new WC_Admin_Duplicate_Product(); |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * WC_Admin_Duplicate_Product Class. |
| 23 | */ |
| 24 | class WC_Admin_Duplicate_Product { |
| 25 | |
| 26 | /** |
| 27 | * Constructor. |
| 28 | */ |
| 29 | public function __construct() { |
| 30 | add_action( 'admin_action_duplicate_product', array( $this, 'duplicate_product_action' ) ); |
| 31 | add_filter( 'post_row_actions', array( $this, 'dupe_link' ), 10, 2 ); |
| 32 | add_action( 'post_submitbox_start', array( $this, 'dupe_button' ) ); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Show the "Duplicate" link in admin products list. |
| 37 | * |
| 38 | * @param array $actions Array of actions. |
| 39 | * @param WP_Post $post Post object. |
| 40 | * @return array |
| 41 | */ |
| 42 | public function dupe_link( $actions, $post ) { |
| 43 | global $the_product; |
| 44 | |
| 45 | if ( ! current_user_can( apply_filters( 'woocommerce_duplicate_product_capability', 'manage_woocommerce' ) ) ) { |
| 46 | return $actions; |
| 47 | } |
| 48 | |
| 49 | if ( 'product' !== $post->post_type ) { |
| 50 | return $actions; |
| 51 | } |
| 52 | |
| 53 | // Add Class to Delete Permanently link in row actions. |
| 54 | if ( empty( $the_product ) || $the_product->get_id() !== $post->ID ) { |
| 55 | $the_product = wc_get_product( $post ); |
| 56 | } |
| 57 | |
| 58 | if ( $the_product && ProductStatus::PUBLISH === $the_product->get_status() && 0 < $the_product->get_total_sales() ) { |
| 59 | $actions['trash'] = sprintf( |
| 60 | '<a href="%s" class="submitdelete trash-product" aria-label="%s">%s</a>', |
| 61 | get_delete_post_link( $the_product->get_id(), '', false ), |
| 62 | /* translators: %s: post title */ |
| 63 | esc_attr( sprintf( __( 'Move “%s” to the Trash', 'woocommerce' ), $the_product->get_name() ) ), |
| 64 | esc_html__( 'Trash', 'woocommerce' ) |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | $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' ) |
| 69 | . '" rel="permalink">' . esc_html__( 'Duplicate', 'woocommerce' ) . '</a>'; |
| 70 | |
| 71 | return $actions; |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Show the dupe product link in admin. |
| 76 | */ |
| 77 | public function dupe_button() { |
| 78 | global $post; |
| 79 | |
| 80 | if ( ! current_user_can( apply_filters( 'woocommerce_duplicate_product_capability', 'manage_woocommerce' ) ) ) { |
| 81 | return; |
| 82 | } |
| 83 | |
| 84 | if ( ! is_object( $post ) ) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | if ( 'product' !== $post->post_type ) { |
| 89 | return; |
| 90 | } |
| 91 | |
| 92 | $notify_url = wp_nonce_url( admin_url( 'edit.php?post_type=product&action=duplicate_product&post=' . absint( $post->ID ) ), 'woocommerce-duplicate-product_' . $post->ID ); |
| 93 | ?> |
| 94 | <div id="duplicate-action"><a class="submitduplicate duplication" href="<?php echo esc_url( $notify_url ); ?>"><?php esc_html_e( 'Copy to a new draft', 'woocommerce' ); ?></a></div> |
| 95 | <?php |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Duplicate a product action. |
| 100 | */ |
| 101 | public function duplicate_product_action() { |
| 102 | if ( empty( $_REQUEST['post'] ) ) { |
| 103 | wp_die( esc_html__( 'No product to duplicate has been supplied!', 'woocommerce' ) ); |
| 104 | } |
| 105 | |
| 106 | $product_id = isset( $_REQUEST['post'] ) ? absint( $_REQUEST['post'] ) : ''; |
| 107 | |
| 108 | check_admin_referer( 'woocommerce-duplicate-product_' . $product_id ); |
| 109 | |
| 110 | $product = wc_get_product( $product_id ); |
| 111 | |
| 112 | if ( false === $product ) { |
| 113 | /* translators: %s: product id */ |
| 114 | wp_die( sprintf( esc_html__( 'Product creation failed, could not find original product: %s', 'woocommerce' ), esc_html( $product_id ) ) ); |
| 115 | } |
| 116 | |
| 117 | $duplicate = $this->product_duplicate( $product ); |
| 118 | |
| 119 | // Hook rename to match other woocommerce_product_* hooks, and to move away from depending on a response from the wp_posts table. |
| 120 | do_action( 'woocommerce_product_duplicate', $duplicate, $product ); |
| 121 | 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.' ); |
| 122 | |
| 123 | // Redirect to the edit screen for the new draft page. |
| 124 | wp_redirect( admin_url( 'post.php?action=edit&post=' . $duplicate->get_id() ) ); |
| 125 | exit; |
| 126 | } |
| 127 | |
| 128 | /** |
| 129 | * Function to create the duplicate of the product. |
| 130 | * |
| 131 | * @param WC_Product $product The product to duplicate. |
| 132 | * @return WC_Product The duplicate. |
| 133 | */ |
| 134 | public function product_duplicate( $product ) { |
| 135 | if ( ! $product instanceof WC_Product ) { |
| 136 | wc_doing_it_wrong( __METHOD__, 'product_duplicate() expects a WC_Product instance', '10.5.0' ); |
| 137 | return new WC_Product(); |
| 138 | } |
| 139 | |
| 140 | /** |
| 141 | * Filter to allow us to exclude meta keys from product duplication.. |
| 142 | * |
| 143 | * @param array $exclude_meta The keys to exclude from the duplicate. |
| 144 | * @param array $existing_meta_keys The meta keys that the product already has. |
| 145 | * @since 2.6 |
| 146 | */ |
| 147 | $meta_to_exclude = array_filter( |
| 148 | apply_filters( |
| 149 | 'woocommerce_duplicate_product_exclude_meta', |
| 150 | array(), |
| 151 | array_map( |
| 152 | function ( $datum ) { |
| 153 | return $datum->key; |
| 154 | }, |
| 155 | $product->get_meta_data() |
| 156 | ) |
| 157 | ) |
| 158 | ); |
| 159 | |
| 160 | $duplicate = clone $product; |
| 161 | $duplicate->set_id( 0 ); |
| 162 | /* translators: %s contains the name of the original product. */ |
| 163 | $duplicate->set_name( sprintf( esc_html__( '%s (Copy)', 'woocommerce' ), $duplicate->get_name() ) ); |
| 164 | $duplicate->set_total_sales( 0 ); |
| 165 | if ( '' !== $product->get_sku( 'edit' ) ) { |
| 166 | $this->generate_unique_sku( $duplicate ); |
| 167 | } |
| 168 | if ( '' !== $product->get_global_unique_id( 'edit' ) ) { |
| 169 | $duplicate->set_global_unique_id( '' ); |
| 170 | } |
| 171 | $duplicate->set_status( ProductStatus::DRAFT ); |
| 172 | $duplicate->set_date_created( null ); |
| 173 | $duplicate->set_slug( '' ); |
| 174 | $duplicate->set_rating_counts( 0 ); |
| 175 | $duplicate->set_average_rating( 0 ); |
| 176 | $duplicate->set_review_count( 0 ); |
| 177 | |
| 178 | foreach ( $meta_to_exclude as $meta_key ) { |
| 179 | $duplicate->delete_meta_data( $meta_key ); |
| 180 | } |
| 181 | |
| 182 | /** |
| 183 | * This action can be used to modify the object further before it is created - it will be passed by reference. |
| 184 | * |
| 185 | * @since 3.0 |
| 186 | */ |
| 187 | do_action( 'woocommerce_product_duplicate_before_save', $duplicate, $product ); |
| 188 | |
| 189 | // Save parent product. |
| 190 | $duplicate->save(); |
| 191 | |
| 192 | if ( ProductMediaGallery::is_feature_enabled() ) { |
| 193 | ProductMediaGallery::copy_stored_video_gallery_items( $product, $duplicate ); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Duplicate children of a variable product. |
| 198 | * |
| 199 | * @since 2.7 |
| 200 | */ |
| 201 | if ( ! apply_filters( 'woocommerce_duplicate_product_exclude_children', false, $product ) && $product->is_type( ProductType::VARIABLE ) ) { |
| 202 | foreach ( $product->get_children() as $child_id ) { |
| 203 | $child = wc_get_product( $child_id ); |
| 204 | |
| 205 | if ( ! $child instanceof WC_Product ) { |
| 206 | wc_doing_it_wrong( __METHOD__, 'product_duplicate() expects product children to be WC_Product instances', '10.5.0' ); |
| 207 | continue; |
| 208 | } |
| 209 | |
| 210 | $child->read_meta_data(); |
| 211 | $child_duplicate = clone $child; |
| 212 | $child_duplicate->set_parent_id( $duplicate->get_id() ); |
| 213 | $child_duplicate->set_id( 0 ); |
| 214 | $child_duplicate->set_date_created( null ); |
| 215 | |
| 216 | // If we wait and let the insertion generate the slug, we will see extreme performance degradation |
| 217 | // in the case where a product is used as a template. Every time the template is duplicated, each |
| 218 | // variation will query every consecutive slug until it finds an empty one. To avoid this, we can |
| 219 | // optimize the generation ourselves, avoiding the issue altogether. |
| 220 | $this->generate_unique_slug( $child_duplicate ); |
| 221 | |
| 222 | if ( '' !== $child->get_sku( 'edit' ) ) { |
| 223 | $this->generate_unique_sku( $child_duplicate ); |
| 224 | } |
| 225 | if ( '' !== $child->get_global_unique_id( 'edit' ) ) { |
| 226 | $child_duplicate->set_global_unique_id( '' ); |
| 227 | } |
| 228 | |
| 229 | foreach ( $meta_to_exclude as $meta_key ) { |
| 230 | $child_duplicate->delete_meta_data( $meta_key ); |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * This action can be used to modify the object further before it is created - it will be passed by reference. |
| 235 | * |
| 236 | * @since 3.0 |
| 237 | */ |
| 238 | do_action( 'woocommerce_product_duplicate_before_save', $child_duplicate, $child ); |
| 239 | |
| 240 | $child_duplicate->save(); |
| 241 | } |
| 242 | |
| 243 | // Get new object to reflect new children. |
| 244 | $duplicate = wc_get_product( $duplicate->get_id() ); |
| 245 | } |
| 246 | |
| 247 | return $duplicate; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Get a product from the database to duplicate. |
| 252 | * |
| 253 | * @deprecated 3.0.0 |
| 254 | * @param mixed $id The ID of the product to duplicate. |
| 255 | * @return object|bool |
| 256 | * @see duplicate_product |
| 257 | */ |
| 258 | private function get_product_to_duplicate( $id ) { |
| 259 | global $wpdb; |
| 260 | |
| 261 | $id = absint( $id ); |
| 262 | |
| 263 | if ( ! $id ) { |
| 264 | return false; |
| 265 | } |
| 266 | |
| 267 | $post = $wpdb->get_row( $wpdb->prepare( "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} WHERE ID = %d", $id ) ); |
| 268 | |
| 269 | if ( isset( $post->post_type ) && 'revision' === $post->post_type ) { |
| 270 | $id = $post->post_parent; |
| 271 | $post = $wpdb->get_row( $wpdb->prepare( "SELECT {$wpdb->posts}.* FROM {$wpdb->posts} WHERE ID = %d", $id ) ); |
| 272 | } |
| 273 | |
| 274 | return $post; |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Generates a unique slug for a given product. We do this so that we can override the |
| 279 | * behavior of wp_unique_post_slug(). The normal slug generation will run single |
| 280 | * select queries on every non-unique slug, resulting in very bad performance. |
| 281 | * |
| 282 | * @param WC_Product $product The product to generate a slug for. |
| 283 | * @since 3.9.0 |
| 284 | */ |
| 285 | private function generate_unique_slug( $product ) { |
| 286 | global $wpdb; |
| 287 | |
| 288 | // We want to remove the suffix from the slug so that we can find the maximum suffix using this root slug. |
| 289 | // This will allow us to find the next-highest suffix that is unique. While this does not support gap |
| 290 | // filling, this shouldn't matter for our use-case. |
| 291 | $root_slug = preg_replace( '/-[0-9]+$/', '', $product->get_slug() ); |
| 292 | |
| 293 | $results = $wpdb->get_results( |
| 294 | $wpdb->prepare( "SELECT post_name FROM $wpdb->posts WHERE post_name LIKE %s AND post_type IN ( 'product', 'product_variation' )", $root_slug . '%' ) |
| 295 | ); |
| 296 | |
| 297 | // The slug is already unique! |
| 298 | if ( empty( $results ) ) { |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | // Find the maximum suffix so we can ensure uniqueness. |
| 303 | $max_suffix = 1; |
| 304 | foreach ( $results as $result ) { |
| 305 | // Pull a numerical suffix off the slug after the last hyphen. |
| 306 | $suffix = intval( substr( $result->post_name, strrpos( $result->post_name, '-' ) + 1 ) ); |
| 307 | if ( $suffix > $max_suffix ) { |
| 308 | $max_suffix = $suffix; |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | $product->set_slug( $root_slug . '-' . ( $max_suffix + 1 ) ); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Generates a unique sku for a given product. |
| 317 | * |
| 318 | * Appends a numeric suffix to the original SKU. For example, if the original SKU |
| 319 | * is "SKU-123", the duplicate will get "SKU-123-1". This preserves hyphens in the |
| 320 | * original SKU rather than treating them as suffix delimiters. |
| 321 | * |
| 322 | * @param WC_Product $product The product to generate a sku for. |
| 323 | * @return void |
| 324 | * @since 10.5.0 |
| 325 | * |
| 326 | * @internal For exclusive usage of WooCommerce core, backwards compatibility not guaranteed. |
| 327 | */ |
| 328 | private function generate_unique_sku( $product ) { |
| 329 | global $wpdb; |
| 330 | |
| 331 | $original_sku = $product->get_sku(); |
| 332 | |
| 333 | // If the product has no SKU, don't do anything. |
| 334 | if ( '' === $original_sku ) { |
| 335 | return; |
| 336 | } |
| 337 | |
| 338 | // If the source SKU is not used by any saved product, use it as-is. |
| 339 | // This preserves backward compatibility with the REST API duplicate endpoint, |
| 340 | // which lets callers set a custom SKU on the duplicate and expects that SKU |
| 341 | // to be applied directly when it is unique. |
| 342 | $source_sku_in_use = (bool) $wpdb->get_var( |
| 343 | $wpdb->prepare( |
| 344 | "SELECT COUNT(*) FROM {$wpdb->wc_product_meta_lookup} WHERE sku = %s", |
| 345 | $original_sku |
| 346 | ) |
| 347 | ); |
| 348 | |
| 349 | if ( ! $source_sku_in_use ) { |
| 350 | $product->set_sku( $original_sku ); |
| 351 | return; |
| 352 | } |
| 353 | |
| 354 | // Query existing SKUs that match the pattern: original SKU + hyphen + number. |
| 355 | $existing_skus = $wpdb->get_col( |
| 356 | $wpdb->prepare( |
| 357 | "SELECT lookup.sku |
| 358 | FROM {$wpdb->posts} as posts |
| 359 | INNER JOIN {$wpdb->wc_product_meta_lookup} AS lookup ON posts.ID = lookup.product_id |
| 360 | WHERE posts.post_type IN ( 'product', 'product_variation' ) |
| 361 | AND lookup.sku LIKE %s", |
| 362 | $wpdb->esc_like( $original_sku ) . '-%' |
| 363 | ) |
| 364 | ); |
| 365 | |
| 366 | // Find the maximum suffix. |
| 367 | $max_suffix = 0; |
| 368 | // +1 for the hyphen separator between original SKU and suffix. |
| 369 | $prefix_len = strlen( $original_sku ) + 1; |
| 370 | foreach ( $existing_skus as $existing_sku ) { |
| 371 | // Check if the SKU starts with our base and has a numeric suffix. |
| 372 | if ( stripos( $existing_sku, $original_sku . '-' ) === 0 ) { |
| 373 | $suffix_str = substr( $existing_sku, $prefix_len ); |
| 374 | // Only consider pure numeric suffixes. |
| 375 | if ( is_numeric( $suffix_str ) ) { |
| 376 | $suffix = intval( $suffix_str ); |
| 377 | if ( $suffix > $max_suffix ) { |
| 378 | $max_suffix = $suffix; |
| 379 | } |
| 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | // Set the new SKU with the next available suffix. |
| 385 | $new_sku = $original_sku . '-' . ( $max_suffix + 1 ); |
| 386 | $product_id = $product->get_id(); |
| 387 | |
| 388 | /** |
| 389 | * Gives plugins an opportunity to verify SKU uniqueness themselves. Filter added to keep backwards |
| 390 | * compatibility with `wc_product_has_unique_sku()`. |
| 391 | * See: https://github.com/woocommerce/woocommerce/pull/62628 |
| 392 | * |
| 393 | * @since 10.5.0 |
| 394 | * |
| 395 | * @param bool|null $has_unique_sku Set to a boolean value to short-circuit the default SKU check. |
| 396 | * @param int $product_id The ID of the current product. |
| 397 | * @param string $sku The SKU to check for uniqueness. |
| 398 | */ |
| 399 | $pre_has_unique_sku = apply_filters( 'wc_product_pre_has_unique_sku', true, $product_id, $new_sku ); |
| 400 | |
| 401 | if ( $pre_has_unique_sku ) { |
| 402 | /** |
| 403 | * Gives plugins an opportunity to verify SKU uniqueness themselves. Filter added to keep backwards |
| 404 | * compatibility with `wc_product_has_unique_sku()`. |
| 405 | * See: https://github.com/woocommerce/woocommerce/pull/62628 |
| 406 | * |
| 407 | * @since 10.5.0 |
| 408 | * |
| 409 | * @param bool|null $sku_found Set to a boolean value to short-circuit the default SKU check. |
| 410 | * @param int $product_id The ID of the current product. |
| 411 | * @param string $sku The SKU to check for uniqueness. |
| 412 | */ |
| 413 | $sku_found = apply_filters( 'wc_product_has_unique_sku', false, $product_id, $new_sku ); |
| 414 | |
| 415 | if ( ! $sku_found ) { |
| 416 | $product->set_sku( $new_sku ); |
| 417 | return; |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | // Fallback: use the standard unique SKU generator which loops until it finds a unique value. |
| 422 | $product->set_sku( wc_product_generate_unique_sku( $product_id, $original_sku, $max_suffix + 1 ) ); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | return new WC_Admin_Duplicate_Product(); |
| 427 |