Search code examples
phpwordpresswoocommercegravity-forms-plugin

How to pass array values from a function and populate on-the-fly a temporary Shop page?


friends.

I'm a newb dev building a website using Wordpress, WooCommerce, Gravity Forms, JetEngine and JetWooBuilder. The service is online photo printing, with the ability to upload user photos and choose between black or white paper.

I have the following situation:

  • Users upload files thru Gravity Forms.

  • Function hooked to gform_confirmation gets those files and generates a new product for each file. Function returns array of new products ids. (Function runs on Snippets plugin)

  • Here comes the trouble: Users must be able to select a color variation before adding the generated products to the cart. But how?

Possible scenarios:

  1. Auto-add new products to cart and allow for variation selection directly on cart. Could not make that work using my image swatches on cart.

  2. Show custom boolean/switcher field on form page (either before submitting files, on the same page of the file upload form; or after submitting the files, on a second page of the form) and then somehow map that field to the correct variation and add to cart. I guess this could work but seems too much for my capabilities.

  3. Use the product IDs that the function is returning and populate a temporary Shop page with these products, so the user can set the desired variation and add products to the cart. (hence the title of this post) This would work since the Shop page already has everything set up as far as styling, variation swatches and so on.

But how do I get those array values to do that? Could I pass thru an URL redirect after the function runs? And how to tell the Shop page to behave correctly?

I am using JetWooBuilder for a custom Shop page, so any solution that could include it would be the perfect match. But of course I am open to any help that comes on my way.

Thank you everyone in advance!

Below I leave that function for reference:

add_action( 'gform_confirmation', 'create_new_products_for_every_upload', 10, 4 );
 
function create_new_products_for_every_upload( $confirmation, $form, $entry, $ajax ) {

// Get uploaded-file ids from Media Library and loop thru
    $file_ids = gp_media_library()->get_file_ids_by_entry( $entry, $form );
    $fresh_prints_ids = array(); // Declare array to collect new product id's
    if( $file_ids ) {
            foreach( $file_ids as $field_id => $_file_ids ) {
            foreach( $_file_ids as $file_id ) {
        
// Clone product template for each uploaded file
    $duplicate_product = new WC_Admin_Duplicate_Product;
    $new_product = $duplicate_product -> product_duplicate( wc_get_product( '3552' ) );
    $new_product_id = $new_product->get_id(); // Get new product ID
    $fresh_prints_ids[] = $new_product_id;

// Get current user details
    $current_user = wp_get_current_user(); // Get WP_User object
    $display_name = $current_user->display_name; // Get Display name

// Update new product
    $new_product->set_status( 'private' );
    $new_product->set_name( 'Photo by '.$display_name );
    $new_product->set_image_id( $file_id );
    $new_product->save();

            }
        }
    }
return $fresh_prints_ids;
return $confirmation;
}

Footnote: I am generating a new product for every uploaded file to allow users to set and order different quantities for each upload. Also this way users can order different variations of the same upload and they will display nicely separated on the cart. I found that this approach allows me to use as much as possible of the core WooCommerce capabilities, without the need for much further customization when dealing with the cart and checkout system. For security reasons, the products are private and direct access to media files are blocked thru a plugin. Image files stored on Gravity Forms folders are auto-erased after the products are created.


Solution

  • I couldn't really "pass the values", but I ended up using the following workaround:

    Before the foreach loop:

    • Create new temporary category using wp_insert_term.
    • Set "Unique ID field" value from form to category name and slug.

    Inside the foreach loop:

    • Assign temporary category to newly created products using wp_set_object_terms.

    On "Confirmation" settings page of the Gravity form:

    • Redirect to site_url/category/{Unique_ID} using merge-tags to pull value from form entry.

    This allowed me to present the user with a temporary Shop page using the same JetWoo Builder templates I already had set up, which was just what I needed. I still have some work to do, namely on bulk adding presented products to cart with one click and then cleaning up after all the process is done.