I had a requirement in one of my latest project to add a product to woocommerce cart when submitting a Gravity form. ie an user customized product. Here it is how I achieved that.
use the gravity form pre submission hook.
add_action("gform_pre_submission", "cp_add_product_to_wc");
Then in the function get the gravity form filed values and push it to the woocommerce cart
function cp_add_product_to_wc($form){
global $woocommerce;
//Get your product details from gform
$product_id = $_POST["input_1"];
$nos = $_POST["input_3"];
$variationid = $_POST["input_3"];
$spec = array();
$spec['Dimension'] = $_POST["input_4"]; //user selected Dimension
$spec['Color'] = $_POST["input_5"]; //user selected color
$woocommerce->cart->add_to_cart( $product_id ); //simple product
//for product with variation other details
$woocommerce->cart->add_to_cart( $product_id, $nos, $variationid, $spec, null );
}
Happy Coding 🙂




