Load Gravity Forms via variation id of WooCommerce

I have a site that is using WooCommerce and Gravity forms. The product page(http://dvcstorefront.com/frameforfabric/product/freestanding-silicone-edge-graphic-frame/) uses a variable product as option to choose the size and the frame & graphic option. I want to load my Gravity Forms using the php code

echo do_shortcode(‘[gravityform id=9 title=false description=false]’);

when the user chooses the Frame with Graphic Option.
I was able to capture the variation id using JS via Ajax

jQuery(document).ready(function($) {
‘use strict’;
$( document ).on( “found_variation.first”, function ( e, v ) {
//alert( v.variation_id );
console.log(“inside JS “+ v.variation_id );
var varid = v.variation_id;

jQuery.ajax({
type : ‘POST’,
url : postvarid.ajax_url,
cache: false,
data : {
action : ‘my_action’,
var_id : varid
},
success : function( response ) {
console.log(response);
// console.log(varid);
}
});

} );

Problem is I cannot pass the value of the varid in my child-theme functions.php and display it using the hook

add_action( ‘woocommerce_before_add_to_cart_button’, ‘my_action’ );

function my_action() {

$passedvariationid = intval( $_POST[‘var_id’] );
if (($passedvariationid == ‘1186’ )){
echo do_shortcode(‘[gravityform id=9 title=false description=false]’);
}

}

How do i pass it correctly? I can display it in the console using the hook

add_action( ‘wp_ajax_nopriv_my_action ‘, ‘my_action’ );

wp_ajax_nopriv_my_action but could not get it to display in the product page when i use the hook

add_action( ‘woocommerce_before_add_to_cart_button’, ‘my_action’ );

What is wrong with my code? Or perhaps what is the best way to handle this?
Basically what i want to accomplish is load my Gravity forms based on the Variation Id of WooCommerce.

Read more here:: Load Gravity Forms via variation id of WooCommerce

Leave a Reply

Your email address will not be published. Required fields are marked *