Home > Software design >  Duplicate product in Woocommerce using Wordpress built-in duplicate URL
Duplicate product in Woocommerce using Wordpress built-in duplicate URL

Time:04-05

I'm using the link provided in the Woocommerce Products page to generate a URL which should duplicate a given product. The wpnonce has be created, and I'm also doing that. I can see a nonce being passed to the url, but I'm still getting the error:

The link you followed has expired. Please try again.

I've tried swapping the "action" name to: woocommerce_duplicate_product duplicate duplicate_product product_duplicate

I'm starting to believe the problem is not the action name. The problem must be coming from somewhere else. Scoured the web looking for answers, dug through the Woocommerce literature, looked through the Wordpress PHP files but from what I can tell, this should be working.

Here's the code I'm using:

    $nonce_duplicate_action = wp_create_nonce('woocommerce_product_duplicate');
    
    $duplicate_url = "https://www.mywebsite.com/store/wp-admin/edit.php?post_type=product&action=duplicate_product&post=47447&_wpnonce=" . $nonce_duplicate_action;
    

CodePudding user response:

Example taken from the WC Product Duplicate class WC_Admin_Duplicate_Product

$post = get_post(47447);
if ($post instanceof WP_Post) {
    $duplicate_url = '<a href="' . wp_nonce_url( admin_url( 'edit.php?post_type=product&action=duplicate_product&amp;post=' . $post->ID ), 'woocommerce-duplicate-product_' . $post->ID ) . '" aria-label="' . esc_attr__( 'Make a duplicate from this product', 'woocommerce' )
            . '" rel="permalink">' . esc_html__( 'Duplicate', 'woocommerce' ) . '</a>';
}
  • Related