Last active
March 6, 2021 00:27
-
-
Save lucasstark/69f36f51fc2fec4165cbf23c49a6dc23 to your computer and use it in GitHub Desktop.
Attach a Gravity Form to all WooCommerce products in a category
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'woocommerce_gforms_get_product_form_data', 'woocommerce_gforms_get_product_form_data_for_category', 10, 2 ); | |
function woocommerce_gforms_get_product_form_data_for_category( $default_data, $product_id ) { | |
$products_to_exclude = array(); | |
//uncomment the following line and modify the 10 to a product ID you need to exclude from having this form. | |
//you can add more if needed. | |
//$products_to_exclude[] = 10; | |
if ( in_array( $product_id, $products_to_exclude ) ) { | |
return $default_data; | |
} | |
if ( ! empty( $default_data ) ) { | |
return $default_data; | |
} else { | |
$configuration = array( | |
'hoodies' => 1, //the form ID for the category hoodies | |
'accessories' => 3, //the form ID for the category accessories | |
'boxer-shorts' => 10 //the form ID for the category boxer-shorts | |
); | |
foreach ( $configuration as $category_slug => $form_id ) { | |
if ( is_object_in_term( $product_id, 'product_cat', $category_slug ) ) { | |
return array( | |
'id' => $form_id, | |
'bulk_id' => 0, | |
'display_title' => '', | |
'display_description' => '', | |
'disable_woocommerce_price' => 'no', | |
'price_before' => '', | |
'price_after' => '', | |
'disable_calculations' => 'no', | |
'disable_label_subtotal' => 'no', | |
'disable_label_options' => 'no', | |
'disable_label_total' => 'no', | |
'disable_anchor' => 'no', | |
'label_subtotal' => 'Subtotal', | |
'label_options' => 'Options', | |
'label_total' => 'Total', | |
'use_ajax' => 'no', | |
'enable_cart_edit' => 'no', | |
'enable_cart_edit_remove' => 'yes', | |
'keep_cart_entries' => 'no', | |
'send_notifications' => 'no', | |
'enable_cart_quantity_management' => 'no', | |
'cart_quantity_field' => '', | |
'update_payment_details' => 'no', | |
'display_totals_location' => 'after' | |
); | |
} | |
} | |
} | |
return $default_data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
great!!! thanks!!!