Created
November 3, 2023 00:51
-
-
Save BruceMcKinnon/0565f447af1fa237635f9f12bb3b0084 to your computer and use it in GitHub Desktop.
Add ACF CPT products on-the-fly to a Gravity Form
This file contains hidden or 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
// | |
// Shop Order Form - Allow shop products to be added to the GF on the fly. | |
// | |
// Products are defined in an ACF CPT. These products may be purchased via a Gravity Form. | |
// The base form should have all fields required, except without products. | |
// The product fields will be added immediately above the Shipping field. | |
// | |
add_filter( 'gform_pre_render_7', 'populate_shop_products' ); | |
add_filter( 'gform_pre_validation_7', 'populate_shop_products' ); | |
add_filter( 'gform_pre_submission_filter_7', 'populate_shop_products' ); | |
add_filter( 'gform_admin_pre_render_7', 'populate_shop_products' ); | |
function populate_shop_products( $form ) { | |
// Is the person a logged in member? | |
$is_a_member = is_valid_user(); | |
// Grab a list of ACF Shop Products | |
$product_query_args = [ | |
'post_type' => 'shop-product', | |
'post_status' => 'publish', | |
'posts_per_page' => -1, | |
'orderby' => 'menu_order, date', | |
'order' => 'ASC' | |
]; | |
$starting_field_id = 0; | |
foreach( $form['fields'] as $field ) { | |
if( $field->id > $starting_field_id ) { | |
$starting_field_id = $field->id; | |
} | |
} | |
$starting_field_id++; | |
$product_query = new WP_Query($product_query_args); | |
if ($product_query->have_posts()) { | |
while ($product_query->have_posts()) { | |
$product_query->the_post(); | |
$product_id = get_the_ID(); | |
$product_name = get_the_title($product_id); | |
$image_url = get_field( 'image', $product_id ); | |
$member_only = get_field( 'members_only', $product_id ); | |
$product_price = get_field( 'price', $product_id ); | |
//fb_log('product '.$product_id.': '.$product_name.' = '.$product_price); | |
//fb_log('member_only:'.bool2str($member_only).' is_member:'.bool2str($is_a_member)); | |
$show_product = true; | |
if ( !( ($member_only) && (!$is_a_member) ) ) { | |
$new_product['type'] = 'product'; | |
$new_product['id'] = $starting_field_id; | |
$new_product['label'] = $product_name; | |
$new_product['inputType'] = 'singleproduct'; | |
$new_product['basePrice'] = floatval( $product_price ); | |
$new_product['enableCalculation'] = true; | |
$new_product['disableQuantity'] = false; | |
$new_product['defaultValue'] = 0; | |
$new_product['inputs'] = array( | |
array( | |
'id' => $starting_field_id.'.1', | |
'label' => $product_name, | |
'name' => 'param_product' | |
), | |
array( | |
'id' => $starting_field_id.'.2', | |
'label' => 'Price', | |
'name' => 'param_price' | |
), | |
array( | |
'id' => $starting_field_id.'.3', | |
'label' => 'Quantity', | |
'name' => 'param_qty' | |
), | |
); | |
$new_field = GF_Fields::create( $new_product ); | |
// Where is the shipping field | |
$shipping_field_position = -1; | |
foreach( $form['fields'] as $field ) { | |
$shipping_field_position++; | |
if ( $field->type == 'shipping' ) { | |
break; | |
} | |
} | |
// Insert the new field before the Shippinng field | |
if ( $shipping_field_position >= 0 ) { | |
$fields_before = array_slice( $form['fields'], 0, $shipping_field_position ); | |
$field_after = array_slice( $form['fields'], $shipping_field_position, count($form['fields'])-1 ); | |
$fields_before[] = $new_field; | |
} | |
$form['fields'] = array_merge( $fields_before, $field_after ); | |
} | |
} | |
} | |
return $form; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment