gform_after_submission does not fire after form is submitted - wordpress

I have tested my custom function, which works correctly on it's own when the form id and entry id are provided. But it never seems to fire when a form is submitted.
I am using the action add_action( 'gform_after_submission_14', 'after_submission', 10, 2 );
My form id is 14.
How can I troubleshoot this or correct the issue?
My code is:
add_action( 'gform_after_submission_14', 'after_submission', 10, 2 );
function after_submission($entry, $form){
global $wpdb;
include $_SERVER['DOCUMENT_ROOT'] . '/custom_config.php';
$data = RGFormsModel::get_lead($entry);
$eid = $data['id'];
$user_id = $data['created_by'];
$hotel = $data['33'];
$flight = $data['39'];
$car = $data['38'];
$parking = $data['37'];
$entertainment = $data['36'];
$other = $data['35'];
if($hotel!=''):
mysqli_query($link,"INSERT INTO reimbursements ( user_id, eid, amount, type ) VALUES ( '$user_id', '$eid', '$hotel', 'hotel' )");
print_r($link);
endif;
if($flight!=''):
mysqli_query($link,"INSERT INTO reimbursements ( user_id, eid, amount, type ) VALUES ( '$user_id', '$eid', '$flight', 'flight' )");
endif;
if($car!=''):
mysqli_query($link,"INSERT INTO reimbursements ( user_id, eid, amount, type ) VALUES ( '$user_id', '$eid', '$car', 'car' )");
endif;
if($parking!=''):
mysqli_query($link,"INSERT INTO reimbursements ( user_id, eid, amount, type ) VALUES ( '$user_id', '$eid', '$parking', 'parking' )");
endif;
if($entertainment!=''):
mysqli_query($link,"INSERT INTO reimbursements ( user_id, eid, amount, type ) VALUES ( '$user_id', '$eid', '$entertainment', 'entertainment' )");
endif;
if($other!=''):
mysqli_query($link,"INSERT INTO reimbursements ( user_id, eid, amount, type ) VALUES ( '$user_id', '$eid', '$other', 'other' )");
endif;
mysqli_close($link);
}

The problem was here:
$data = RGFormsModel::get_lead($entry);
$entry is already an array that is in use.
So rather than define them again as I did:
$eid = $data['id'];
I should have used:
$eid = $entry['id'];
The bigger part of this solution was figuring out the ability to troubleshoot it.
For that, I did the following:
Add GFCommon::log_debug( __METHOD__ . '(): running.' ); to the top of the function.
Enable logging as explained here (https://docs.gravityforms.com/logging-and-debugging/#enabling-logging)
View the error log of 'Gravity Forms Core' at 'Forms->Settings->Logging' This requires that 'logging' is check marked at the time the test is run.

Related

show wp_postmeta in woocommerce order dashboard

i try to show a specific meta_value based on post_id and meta_key in woocommerce order dashboard:
First i create a new column:
function mb_set_order_note_column( $columns ) {
$columns['parcel_delivery'] = __('DHL','TEXTDOMAIN');
return $columns;
}
add_filter( 'manage_shop_order_posts_columns', 'mb_set_order_note_column', 99 );
After i have tried to get the specific value from wp_postmeta database
function mb_show_order_note_columns( $column_name, $post_id ) {
switch ( $column_name ) {
case 'parcel_delivery':
$order = new WC_Order( $post_id );
$delivery = get_post_meta( $post_id, '_parcel_delivery_opted_in' );
print $delivery ;
break;
}
}
add_action( 'manage_shop_order_posts_custom_column' , 'mb_show_order_note_columns', 10, 2 );
this shows only the customer notes from wp_post database. how to change to get specific value...
Thx
I see your custom meta key is _parcel_delivery_opted_in. So to get this value from wp_postmeta with order id and meta key,
$delivery = get_post_meta( $post_id, '_parcel_delivery_opted_in', true ); // like _billing_email meta
3rd parameter true will return single value, if you omit third parameter an array will be returned.

Bring colors of SKUS products that are similar Woocommerce

I am listing similar sku on the product page and would like to get their colors as well.
E.g;
1-) Sku code : 21.127.21 Color : Red
2-) Sku code : 21.127.23 Color : Blue
3-) Sku code : 21.127.24 Color : Black
I probably need to update my query but I'm not sure
Following my code: functions.php
function wc_get_products_sku_img( $sku_excerpt ) {
global $wpdb;
// Get all product Ids and skus (standard objects) from a sku excerpt
return $wpdb->get_results( $wpdb->prepare( "
SELECT p.ID as id, pm.meta_value as sku
FROM {$wpdb->prefix}posts p
INNER JOIN {$wpdb->prefix}postmeta pm
ON p.ID = pm.post_id
WHERE p.post_type = 'product'
AND p.post_status = 'publish'
AND pm.meta_key = '_sku'
AND pm.meta_value LIKE '%s'
", '%'.$sku_excerpt.'%' ) );
}
global $product;
$results = wc_get_product_skus('21.127.');
foreach ( $results as $result ) {
$product_id = $result->id;
$product_sku = $result->sku;
$product = wc_get_product( $product_id );
echo 'Sku Code :'. $product_sku;
}
Thanks in advance
I figured out what the problem is, I'd like to share the solution in case someone needs it.
I joined the color property in my result value and called it.
global $product;
$results = wc_get_product_skus('21.127.');
foreach ( $results as $result ) {
$product_id = $result->id;
$product_sku = $result->sku;
$product_color = $result->pa_color;
$product = wc_get_product( $product_id );
echo 'Sku Code :'. $product_sku;
echo 'Sku Color :'. $product_color;
}

Show users to other users who bought the product in Woocommerce

I am searching around but is it possible in Woocommerce to show a list of users that has bought the product in the frontend?
So on the product page i wanna show a list of users that also has bought that product.
Oke i have fixed it with a little help of this question.
So what i do i put this in a function:
function retrieve_orders_ids_from_a_product_id( $product_id ) {
global $wpdb;
// Define HERE the orders status to include in <== <== <== <== <== <== <==
$orders_statuses = "'wc-completed'";
# Get All defined statuses Orders IDs for a defined product ID (or variation ID)
return $wpdb->get_col( "
SELECT DISTINCT woi.order_id
FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim,
{$wpdb->prefix}woocommerce_order_items as woi,
{$wpdb->prefix}posts as p
WHERE woi.order_item_id = woim.order_item_id
AND woi.order_id = p.ID
AND p.post_status IN ( $orders_statuses )
AND woim.meta_key IN ( '_product_id', '_variation_id' )
AND woim.meta_value LIKE '$product_id'
ORDER BY woi.order_item_id DESC"
);
}
and then i use this to display their "display name" and a link to the profile:
$product_id = $product->get_id(); // Put here the product ID.
$orders_ids = retrieve_orders_ids_from_a_product_id( $product_id );
$orderID_array = array_unique($orders_ids);
echo '<ul>';
foreach ( $orderID_array as $orderID ) {
$order = wc_get_order( $orderID );
$profile_name = get_the_author_meta( 'display_name', $order->get_user_id() );
$profile_link = get_author_posts_url($order->get_user_id());
echo '<li>' . $profile_name . '</li>';
}
echo '</ul>';
Not sure if your users would appreciate this, but I believe that that WooCommerce offers a hook to fetch orders.
$orders->get_items();
You could loop through this and get the user() information. Obviously you would need to make sure that information that isn't "personal", so no email etc. and only the first name for example.
Check this out:
https://www.businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/

Woocommerce Admin Product Search Not working

I ran into the issue that woocommerce product search in admin not working and giving result as No product found though there are lots of products with my search input.
Also i haven't used relevancy search plugin so there will be no any issue.
I set debug true in wp-congig.php and its throwing one error related to wp_meta with printing whole query.
Also tried disable theme and try with default theme also tried to disable every plugin one by one but still no luck.
I was experiencing the same thing and adding the following code block to my functions.php file fixed it immediately:
function m_request_query( $query_vars ) {
global $typenow;
global $wpdb;
global $pagenow;
if ( 'product' === $typenow && isset( $_GET['s'] ) && 'edit.php' === $pagenow ) {
$search_term = esc_sql( sanitize_text_field( $_GET['s'] ) );
// Split the search term by comma.
$search_terms = explode( ',', $search_term );
// If there are more terms make sure we also search for the whole thing, maybe it's not a list of terms.
if ( count( $search_terms ) > 1 ) {
$search_terms[] = $search_term;
}
// Cleanup the array manually to avoid issues with quote escaping.
array_walk( $search_terms, 'trim' );
array_walk( $search_terms, 'esc_sql' );
$meta_key = '_sku';
$post_types = array( 'product', 'product_variation' );
$query = "SELECT DISTINCT posts.ID as product_id, posts.post_parent as parent_id FROM {$wpdb->posts} posts LEFT JOIN {$wpdb->postmeta} AS postmeta ON posts.ID = postmeta.post_id WHERE postmeta.meta_key = '{$meta_key}' AND postmeta.meta_value IN ('" . implode( "','", $search_terms ) . "') AND posts.post_type IN ('" . implode( "','", $post_types ) . "') ORDER BY posts.post_parent ASC, posts.post_title ASC";
$search_results = $wpdb->get_results( $query );
$product_ids = wp_parse_id_list( array_merge( wp_list_pluck( $search_results, 'product_id' ), wp_list_pluck( $search_results, 'parent_id' ) ) );
$query_vars['post__in'] = array_merge( $product_ids, $query_vars['post__in'] );
}
return $query_vars;
}
add_filter( 'request', 'm_request_query', 20 );
Shout-out to mircian for the snippet.

How to filter BuddyPress member loop by ACF field?

PHP newb here, looking for some guidance. I am working with BuddyPress and Advanced Custom Fields (ACF). I have an ACF field 'new_user' with a value of true/false. I am trying to filter my BuddyPress Members Loop to only display users with a value of 'new_user' = true.
There are 2 code samples here.
The standard BP Members Loop. My thought here, is how do I first query my users by ACF ‘new_user’ = true and then start the bp member loop?:
if ( bp_has_members() ) :
// some code goes here
endif;
while ( bp_members() ) : bp_the_member();
//OUTPUT MEMBERS LIST HERE
endwhile;
This is a BP function to filter by Buddypress extended user fields. The idea here I believe is to replace the code in the middle specific to xprofile_get_field with the proper ACF code:
function my_custom_ids( $field_name, $field_value = '' ) {
if ( empty( $field_name ) )
return '';
global $wpdb;
$field_id = xprofile_get_field_id_from_name( $field_name );
if ( !empty( $field_id ) )
$query = "SELECT user_id FROM " . $wpdb->prefix . "bp_xprofile_data WHERE field_id = " . $field_id;
else
return '';
if ( $field_value != '' )
$query .= " AND value LIKE '%" . $field_value . "%'";
/*
LIKE is slow. If you're sure the value has not been serialized, you can do this:
$query .= " AND value = '" . $field_value . "'";
*/
$custom_ids = $wpdb->get_col( $query );
if ( !empty( $custom_ids ) ) {
// convert the array to a csv string
$custom_ids_str = 'include=' . implode(",", $custom_ids);
return $custom_ids_str;
}
else
return '';
}
Of course, I am open to solving this in another way as well. I hope this is clear.

Resources