I have a Woocommerce site, and I use Gravity Forms to further expand each order.
I am coding a management tool that consumes both APIs to make some statistics and other administration tools.
I can get a list of the Gravity Forms entries, and also a list of the orders. The problem I have is that I don't know how can I get the entry that is related to a particular order.
Is there a way to do this?
have you tried with the woocomerce history plugin or fetching the raw metadata out the item¿?
wc_get_order_item_meta($order_item_id, "_gravity_forms_history");
wc_get_order_item_meta($order_item_id, "_gravity_form_data");
keep in mind that this will require a new endpoint to be created is not put of the box.
The last time I worked with the WooCommerce Gravity Forms Product Addons (a year or so ago) it did not store the order ID in the entry (would have to happen after the entry is created and after the order is created), or the entry ID in the order. The latter probably makes more sense but both would require custom code.
Again, it's been some time since I worked with the add-on. I'd ping WC support and see if they any tips on implementing support for this.
This is where I found the link between WooCommerce and the gravity forms product addon:
In the database, find the order in the table your_table_prefix_posts, and grab its ID. I was filtering for the post_type "shop_order."
In the table your_table_prefix_woocommerce_order_items, find the ID just found and filter for "line_item" in the "order_item_type" column, and grab the "order_item_id."
Use that "order_item_id" to find the order's meta in the table your_table_prefix_woocommerce_order_itemmeta.
All of the order's gravity forms data is in there. It looks like there is no actual tie between what woo does and gravity, except that the form is filled out and it data is grabbed and stuck into your_table_prefix_woocommerce_order_itemmeta. I cannot find anything that ties a particular order to a particular gf entry, but you can get the data from Woo, and at least use that to search GF entries.
I was able to do this using Gravity Forms, Gravity Forms Product Addons, and Woocommerce using a three step process:
STEP 1: Get GF Entry ID/s as the entry is made and store it in a session. This happens before checkout is complete and sometimes before the user is logged in.
add_action( 'gform_after_submission', 'blb_get_lead_entry_id', 10, 2 );
function blb_get_lead_entry_id( $entry, $form ) {
$meta_value = $entry['id'];
//session array with all the entries because GF creates 2 entries each time for validation purposes apparently
if (!isset($_SESSION['entryclump'])) {
$_SESSION['entryclump'] = array();
}
$_SESSION['entryclump'][] = $meta_value;
//also an array with just the current entry which will end up be the later of the two entries added by GF
$_SESSION[ 'gf-entry-id' ] = $meta_value;
}
STEP 2: Include the entry you just gathered ($_SESSION[ 'gf-entry-id' ]) with the Cart Item Meta and then save it.
In this case, i am saving a field called "_gf_entry_ID" which will get added to the woocommerce_order_itemmeta table with the correct order_item_id and the later of the two GF entries as the meta_value.
//add cart item data
add_filter( 'woocommerce_add_cart_item_data', 'blb_add_gfentry_to_cart_data', 10, 3 );
function blb_add_gfentry_to_cart_data( $cartItemData, $productId, $variationId ) {
$entryid=$_SESSION[ 'gf-entry-id' ];
$cartItemData['GFentryID'] = $entryid;
return $cartItemData;
unset($_SESSION[ 'gf-entry-id' ]);
}
//add cart item data: session stuff
add_filter( 'woocommerce_get_cart_item_from_session', 'blb_cart_item_session', 10, 3 );
function blb_cart_item_session( $cartItemData, $cartItemSessionData, $cartItemKey ) {
if ( isset( $cartItemSessionData['GFentryID'] ) ) {
$cartItemData['GFentryID'] = $cartItemSessionData['GFentryID'];
}
return $cartItemData;
}
//save the data
add_action( 'woocommerce_add_order_item_meta', 'blb_save_gfentry', 10, 3 );
function blb_save_gfentry( $itemId, $values, $key ) {
if ( isset( $values['GFentryID'] ) ) {
wc_add_order_item_meta( $itemId, '_gf_entry_ID', $values['GFentryID'] );
}
}
STEP 3 (optional): Update the GF Form to reflect the correct created_by user ID. Now that checkout is complete and the user is logged in, we can do that.
function blb_woocommerce_thankyou( $order_id ) {
//Current User
$currentUserID = wp_get_current_user()->ID;
//GF Entry Array for Order
$order = new WC_Order( $order_id );
$items = $order->get_items();
$order_item_ids = array();
$gf_entry_ids = array();
foreach ( $items as $key=>$item ) {
$gf_entry_ids[] = $item['item_meta']['_gf_entry_ID'][0];
}
//First real quick clear all the entries in the entry clump (in case the user was already logged in)
//This is important because GF creates two forms for product add ons with Woocommerce and we only want one to show up in the list and edit plugin
$entryclump = $_SESSION[ 'entryclump' ];
foreach ( $entryclump as $entry ) {
global $wpdb;
$wpdb->update('wp_rg_lead', array('created_by' => null), array('id' => $entry));
}
//Update wp_rg_lead
if (($currentUserID!=0) && (isset($_SESSION[ 'entryclump' ])) ) {
foreach ( $gf_entry_ids as $gf_entry_id ) {
global $wpdb;
$wpdb->update('wp_rg_lead', array('created_by' => $currentUserID), array('id' => $gf_entry_id));
} //foreach
} //if
unset($_SESSION[ 'entryclump' ]);
};
add_action( 'woocommerce_thankyou', 'blb_woocommerce_thankyou', 10, 1 );
Related
This is my current recent order
I wish to add another column "Tracking Number" and it will show woocommerce "note to customer" inside.
result is like : Display last WooCommerce admin order note in customers order history
The difference is without clicking view order and my customer can get to known their tracking number.
But I totally no idea how this work because not familiar with php..
hope to make this done and learn something.
Thanks!
the result will
You Need to create a new column in My order page first
function order_note_in_column( $columns ) {
$new_columns = array();
foreach ( $columns as $key => $name ) {
$new_columns[ $key ] = $name;
// Your Column Name : Change Tracking Number with the Column Heading you Want
if ( 'order-status' === $key ) {
$new_columns['track-number'] = __( 'Tracking Number', 'textdomain' );
}
}
return $new_columns;
}
add_filter( 'woocommerce_my_account_my_orders_columns', 'order_note_in_column' );
Once you create a new column now the second step is to display Data in the column.keep type as internal to show private note, In this way customer notes will not be displayed at frontend
function order_note_value_in_column( $order ) {
//Get Notes by order ID & Here keep type as internal to show private note. In this way customer notes will not be displayed at frontend
$note = wc_get_order_notes([
'order_id' => $order->get_id(),
'type' => 'internal',
]);
// Displaying the latest Note. If no tracking number entered then order status will be displayed in column
print_r($note[0]->content);
}
add_action( 'woocommerce_my_account_my_orders_column_track-number', 'order_note_value_in_column' );
code goes in functions.php tested & works
I'm trying to make an SQL query to run upon cancellation of a booking, via a custom plugin, updating custom user meta data.
Here's my code:
function wporg_callback() {
global $wpdb;
$wpdb->query("UPDATE usermeta SET meta_value = 15 WHERE umeta_id = 131");
}
add_action('woocommerce_bookings_cancelled_booking', 'wporg_callback');
But It's not working.
Is there something wrong with the query? Is the right action not being used?
Edit - I Also tried the following without success:
add_action('woocommerce_bookings_cancelled_booking', 'wporg_callback');
function wporg_callback( $booking_id ) {
// Add/Update custom user meta data
update_user_meta( 2, 'download_credits', 17 );
}
Updated:
The correct hook to be used is woocommerce_booking_cancelled (a composite hook) that will allow you to retrieve the user ID and to Add/update custom user meta data like below:
add_action('woocommerce_booking_cancelled', 'booking_cancelled_transition_to_callback', 10, 2 );
function booking_cancelled_transition_to_callback( $booking_id, $booking ) {
// Get the user ID from the Booking ID
$user_id = get_post_field ('post_author', $booking_id);
$download_credits = get_post_meta( $booking_id, '_booking_cost', true );
// Add/Update custom user meta data
update_user_meta( $user_id, 'download_credits', $download_credits );
}
Code goes in functions.php file of your active child theme (or theme). Tested and works.
How to get the booking data (from the booking ID displayed in the order):
Go to your database under wp_postmeta table to get the desired meta_key from the post_id (that is the booking ID)
Use that meta key in get_post_meta() WordPress function like:
$meta_value = get_post_meta($booking_id, 'the_meta_key', true);
Notes:
An SQL query is not needed to add/update custom user meta data, as you can use instead the dedicated Wordpress function update_user_meta().
I couldn't make it work using woocommerce_bookings_cancelled_booking hook.
WooCommerce Bookings action and filters hooks developer documentation
We are working on a little project, and cannot get it to work properly. Quick info: someone fills out a form, info sends to sql database and creates a wordpress post type product (using zapier)
We are trying to make wordpress get the new post's excerpt, find the row, where the first column would have same value as the excerpt and then in that row look for a column with certain name to see whether it is null or not. If not null, we want get that value of field and assign this value as post type product's category.
Ex: If value is glasses, products category would also be glasses. We tried this code (using $wpdb ) , however it did not work properly, and as we are relatively new to php and would really appreciate your insight...
add_action( 'xmlrpc_call', 'prepare_woocommerce_check' );
function prepare_woocommerce_check( $action ) {``if ( 'wp.newPost' === $action ) {
add_action( 'save_post', 'validate_woocommerce_product' );}}
function validate_woocommerce_product( $product_id ) {
remove_action( 'save_post', 'validate_woocommerce_product' );
$product = wc_get_product( $product_id )
$product->set_regular_price(22);
$myexcerpt = apply_filters('the_excerpt', get_the_excerpt($product_id));
global $wpdb;
$result =$wpdb-> get_results("SELECT select FROM info WHERE clienid == $myexcerpt");
if ($result) {
function wp_insert_term() {
wp_insert_term($result, "product_cat");
}
add_action('init','wp_insert_term');
$product->save();
}
I have Suppliers which have their own products. WP backend is almost redesigned and there I have a page(with form) where 'admin' can add new supplier and I need to create unique a reference number for each supplier when form will be submitted.
Plus, I have a "Sort by" dropdown and one of the sorting options is by "Reference number".
At first, I thought to use the POST ID as reference number, but don't think that this can be best solution, as POST IDs will be different when some posts will be removed. Also I was thinking to use uniqid() function with some digit limit and only digits.
What is best to reach this? Any ideas?
You can specify a new custom meta field for (i.e. supplier_id) and create a function which ensure that this supplier_id is unique. This function will be executed every time when a supplier form submitted.
The action hook save_post is triggered whenever a post or page is created or updated. So we can use it for this purpose.
From the documentation:
save_post is an action triggered whenever a post or page is created or updated, which could be from an import, post/page edit form, xmlrpc, or post by email. The data for the post is stored in $_POST, $_GET or the global $post_data, depending on how the post was edited. For example, quick edits use $_POST.
Since this action is triggered right after the post has been saved, you can easily access this post object by using get_post($post_id)
Example:
function save_supplier_id( $post_id, $post, $update ) {
$post_type = get_post_type($post_id);
if ( "supplier" != $post_type ) return;
if ( isset( $_POST['supplier_id'] ) ) {
$my_supplier_id = $_POST['supplier_id'];
if ( ! is_int( $my_supplier_id ) ) $my_supplier_id = 1;
$all_other_suppliers = get_posts(array(
'posts_per_page' => -1,
'post_type' => 'supplier',
'post__not_in' => array( $post_id )
));
$all_other_ids = array_map( function( $supplier ) { return $supplier->ID; }, all_other_suppliers );
if ( count( $all_other_ids ) && in_array( $my_supplier_id, $all_other_ids ) ) {
// ID is already in use by another supplier, let's create an new one
$my_supplier_id = max( $all_other_ids ) + 1;
}
update_post_meta( $post_id, 'supplier_id', $my_supplier_id ) );
}
}
add_action( 'save_post', 'save_supplier_id', 10, 3 );
Explantion:
The format for supplier_idis simple a consecutive number. If the provided id is not an integer, we set it to 1. Now we get all other supplier id's and check if the give id no occur twice. If so, the we get the max id and increase it by 1.
Gravity Forms Product Add-ons for Woocommerce displays the form inputs of a user in the cart and checkout. I want to display these form fields in the user's Account page with their orders. I've been trying for a week and can't seem to get the right php code put together. How can I do this?
My code as it stands is like so:
$form_id = RGFormsModel::get_form_id('Domains');
$form = GFFormsModel::get_form_meta($form_id);
$field = GFFormsModel::get_field($form, 1);
$leads = RGFormsModel::get_leads($form['id']);
foreach($leads as $lead)
{
foreach($field as $field_id)
{
$value = rgar($lead, (string) $field_id);
echo $value;
}
}
This returns all entries for the field I want, however, I only want to return the entry that was submitted with that particular product. Help?
Here's a quick example for clarity on what I'm looking for. A user buys a shirt and selects size large from a Gravity Form that was attached to the product. On the cart and checkout pages, it says beneath the product title "Size: Large". I want to add that same text to the "My Account" page with their order.
The way I solved a similar problem may not be the best in coding practices but it works. It takes custom data associated with the order from Gravity Forms and displays it under the shipping address in WooCommerce individual order page:
<?php
add_action('woocommerce_admin_order_data_after_shipping_address', 'ips_show_signs_in_admin_under_shipping', 10, 1);
/*
function to put in functions.php of the theme to add custom gravityforms data to customer order page in admin area.
*/
function ips_show_signs_in_admin_under_shipping($order)
{
global $woocommerce, $post;
$order_items = $order->get_items(apply_filters('woocommerce_admin_order_item_types', array( 'line_item')));
foreach($order_items as $item_id => $item)
{
if ($metadata = $order->has_meta($item_id)) //not a comparison, so one equal sign. so if there is any data in there it would assign it and thus making it true, if no data it would be false
{
foreach($metadata as $meta)
{
// Skip hidden woocommerce core fields just in case
if (in_array($meta['meta_key'], apply_filters('woocommerce_hidden_order_itemmeta', array(
'_qty',
'_tax_class',
'_product_id',
'_variation_id',
'_line_subtotal',
'_line_subtotal_tax',
'_line_total',
'_line_tax',
)))) continue;
if (is_serialized($meta['meta_value'])) continue; // Skip serialised meta since we dont need it incase its in there
$meta['meta_key'] = esc_attr($meta['meta_key']); //just to make sure there is no surprises in there
$meta['meta_value'] = esc_textarea($meta['meta_value']); // using a textarea check for surprises (sql injection)
if ("Signpost Address" === $meta['meta_key']) //here comes the custom data from gravity forms
{
echo $meta['meta_value']; //this is what i was after from gravity forms collected with order
}
} //closes --- foreach($metadata as $meta)
} //closes --- if ($metadata = $order->has_meta($item_id))
} //closes --- foreach($order_items as $item_id => $item)
} //closes --- function
?>
Edit: This code searches the order meta data
I have added the above code in my functions.php file and unfortunately this hasn't worked for me. I have looked all over for a solution to this and this seems to exactly be what I'm after. Do i simply replace "Signpost Address" with say "Blanket Size" if that were one of my gravity form options?