Skip Navigation

[Resolved] Duplicate WooCommerce Product on Frontend with Cred

This support ticket is created 6 years, 4 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 9 replies, has 2 voices.

Last updated by Nashaat 6 years, 3 months ago.

Assisted by: Luo Yang.

Author
Posts
#920044

i would like to duplicate product /post on the front end.

I am following this post: https://toolset.com/forums/topic/duplicate-cred-form-at-frontend/
it looks like this should work as Minesh has resolved this!

I made the same steps but when adding the function to code snippet it show me following:
403 Forbidden
A potentially unsafe operation has been detected in your request to this site.

i cant find out why. if this is not an ideal way to duplicate cred post. so what would be a better solution without using duplicate plugin or clone plugin, since the duplicate plugin redirect to the backend when duplicating and i want that user stay on frontend.

#920164

Hello,

Please add the custom codes into your theme/functions.php, and test again.
For the 403 error of code snippet plugin, it is out the range of Toolset support, please check it with the author of code snippet plugin:
https://wordpress.org/support/plugin/insert-php

Here is the document about Toolset form action hook:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

1) You will need to replace number 58 with the Toolset form ID
https://toolset.com/forums/topic/duplicate-cred-form-at-frontend/#post-333164

2) Those custom codes can duplicate the data in database table "wp_posts" and "wp_postmeta", it won't be able to copy the taxonomy settings.

#920927

Dont know why but it works now even in Code Snippet!
one more question is it possible to duplicate everything just the status of the post should change to pending? because if i duplicate a published post it duplicate it without Taxonomies and i still need to edit that post and prefer to keep it in Pending Status till i edit it.

here is what i did for other users.

Created "Add New content" form. and just added the submit button

[credform class="cred-form cred-keep-original"]
  [cred_field field="form_submit" value="Duplicate"]
[/credform]

then i have embed the Cred Form in my content Template

[cred_form form="duplicate-button"]
add_action('cred_save_data_713', 'duplicate_post', 10, 2);
function duplicate_post($post_id, $form_data) {
    // get data of original post
    $post = get_post( $form_data['container_id'], ARRAY_A );
    // update the new post with this data
    $post['ID'] = $post_id;
    $post['post_title'] = 'Copy of ' . $post['post_title'];
    wp_update_post( $post );
    // get fields of original post
    $fields = get_post_custom( $form_data['container_id'] );
    // update the new post with these fields
    foreach ($fields as $key => $values) {
        foreach ($values as $value) {   
            add_post_meta($post_id, $key, $value, false);
        }
    }
}

713 is the number of my cred form ID

#921048

For the new question:
one more question is it possible to duplicate everything just the status of the post should change to pending?
you need to add "post_status" parameter, for example:

...
$post['post_status'] = 'draft';
wp_update_post( $post );
...

More help:
https://codex.wordpress.org/Function_Reference/wp_update_post

#922825

Thanks Luo! I will try this and let you know if this works soon!

#922886

OK, please update this thread if you still need assistance.

#948204

I will reply after 18.07

#948271

OK, waiting for you update.

#951370

I have added this code and when duplicating the post it changes the post status to draft. but also the status of original post that was duplicated changes to draft..

this is the code

add_action('cred_save_data_713', 'duplicate_post', 10, 2);
function duplicate_post($post_id, $form_data) {
    // get data of original post
    $post = get_post( $form_data['container_id'], ARRAY_A );
    // update Post Status to DRAFT
    $post['post_status'] = 'draft';
    wp_update_post( $post );
    // update the new post with this data
    $post['ID'] = $post_id;
    $post['post_title'] = 'Copy of ' . $post['post_title'];
    wp_update_post( $post );
    // get fields of original post
    $fields = get_post_custom( $form_data['container_id'] );
    // update the new post with these fields
    foreach ($fields as $key => $values) {
        foreach ($values as $value) {   
            add_post_meta($post_id, $key, $value, false);
        }
    }
}
#951390

Update

I have solved the issue by deleting " wp_update_post( $post ); "
i had two times " wp_update_post( $post ); " in the code so thats why it was changing also the original post status. now only duplicated post has post status "draft"

final Code:

add_action('cred_save_data_713', 'duplicate_post', 10, 2);
function duplicate_post($post_id, $form_data) {
    // get data of original post
    $post = get_post( $form_data['container_id'], ARRAY_A );
    // update Post Status to DRAFT
    $post['post_status'] = 'draft';
    // update the new post with this data
    $post['ID'] = $post_id;
    $post['post_title'] = 'Copy of ' . $post['post_title'];
    wp_update_post( $post );
    // get fields of original post
    $fields = get_post_custom( $form_data['container_id'] );
    // update the new post with these fields
    foreach ($fields as $key => $values) {
        foreach ($values as $value) {   
            add_post_meta($post_id, $key, $value, false);
        }
    }
}