Skip to content

Instantly share code, notes, and snippets.

@claygriffiths
Last active June 27, 2020 21:32
Show Gist options
  • Save claygriffiths/25d3279068ebe2aceb2c1db492aa2b9c to your computer and use it in GitHub Desktop.
Save claygriffiths/25d3279068ebe2aceb2c1db492aa2b9c to your computer and use it in GitHub Desktop.
WooCommerce Gravity Forms Product Add-ons: Exclude Product
<?php
/**
* Note: See https://gravitywiz.com/documentation/how-do-i-install-a-snippet/ for details on how to install snippets.
*
* This snippet allows you to prevent product fields from being added to the price of a product added to the WooCommerce
* cart via Gravity Forms Product Add-ons
*/
function gwiz_exclude_product_from_wc_product_addons( $product_name ) {
add_filter( 'gform_product_info', function ( $product_info ) use ( $product_name ) {
$products = $product_info['products'];
if ( ! empty( $products ) && is_array( $products ) ) {
$product_info['products'] = array_filter( $products, function ( $product ) use ( $product_name ) {
return $product['name'] !== $product_name;
} );
}
return $product_info;
} );
}
/**
* Call gwiz_exclude_product_from_wc_product_addons() for every product you wish to exclude. It should match the
* product name.
*
* In the case of choice-based products, you must specify each choice.
*/
gwiz_exclude_product_from_wc_product_addons( 'Product Name' );
//gwiz_exclude_product_from_wc_product_addons( 'Second Choice' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment