Search code examples
wordpresshook-woocommercegravity-forms-plugin

Show woocommerce product after gravity form submission


the below code will print the products which fall under the same category, that is, $field_two will contain the category.

add_action( 'gform_after_submission_1', 'access_entry_via_field', 10, 2 );

function access_entry_via_field( $entry, $form ) {

    $field_one = $_POST['input_1'];
    $field_two = $_POST['input_6'];

    $items = array("age"=>"$field_one", "skin_type"=>"$field_two");

    $args = array(
        "category" => array("$field_two"),
    );

    $products = wc_get_products($args);

    var_dump($products); exit();

}

Once the gravity form is submitted, How to show the products ??


Solution

  • In order to achieve this you have to change your confirmation message of the form where you get the values of the entries to use them after to filter the results

    add_filter( 'gform_confirmation_1', 'custom_confirmation_message', 10, 4 );
    function custom_confirmation_message( $confirmation, $form, $entry, $ajax ) {
       $field_one = $entry["1.1"];
       $field_two = $entry["1.6"];
    
       $items = array("age"=>"$field_one", "skin_type"=>"$field_two");
    
       $args = array(
           "category" => array("$field_two"),
        );
        $products = wc_get_products($args);
    
        $confirmation .= 'Thanks for contacting us! We will get in touch with you shortly.';
        $confirmation .= $products;
    
    return $confirmation;
    }