Skip Navigation

[Resolved] CRED commerce order link to created post

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 3 replies, has 2 voices.

Last updated by Beda 2 years, 6 months ago.

Assigned support staff: Beda.

Author
Posts
#1223153

I have a CRED commerce form which adds an event (custom post). I am was not able to find any link between the post created and WooCommerce order. I tried the following code to add order number/id to the created post custom field but its not working,

/* add order number to post */
add_action('cred_commerce_after_payment_completed','add_order_number',10,1);
function add_order_number($data){
	$order_number = $data['transaction_id']; //WooCommerce Order ID associated to the buying process
	$post_id = $data['extra_data'][0]['cred_post_id']; //Post ID that was created/edited with the form
	update_post_meta( $post_id, 'wpcf-order-number', $order_number);
}

I also want to know if there is a way to get name and email id entered during checkout and save it in custom fields of the created post?

Thanks,
Sasank

#1223336

Things added during the checkout are not part of Toolset but of WooCommerce, and you should use their API to get that data.
The Toolset Commerce API hooks are only for Toolset's forms, they will not help you to manipulate WooCommerce Data, or get that data.
The Toolset Commerce API hooks are hooks that allow you to place your (custom) code at the right moment of the Form's life/actions.
This is shown here:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/
For example, cred_commerce_add_product_to_cart will be fired whenever the product is added to the cart.
But it doesn't allow you to manipulate that product or its order, for that, you need WooCommerce's API.

In the Forms API, you usually will have the Form data (forms ID), product Data (Product ID) and post data (Post ID of the post created)
If you know the Product ID you can eventually use that in a WooCommerce API call to get the order connected to it.
Note that Product Data is only available on cred_commerce_add_product_to_cart, cred_commerce_before_add_to_cart and in form of an associative array that also holds transaction_id, user_id, and product_id on the cred_commerce_after_order_completed, cred_commerce_after_payment_completed, cred_commerce_after_payment_refunded, cred_commerce_after_send_notifications and cred_commerce_after_payment_cancelled.
The order ID is never part of this.

The hook you use, cred_commerce_after_payment_completed https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_payment_completed, has only the cred_product_id (Product ID), according to our Documentation, hence transaction is not part of it.
Even if, transaction ID is not the order ID.
It mentions there that other data included is internal to Forms and shall not be used.

You could hewever use transaction ID (order ID) on cred_commerce_after_order_completed, https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed

You could use some custom code to get the Order ID by Product ID if you need to do that on other hooks or submit a feature request to Toolset's Product Manager to suggest it
https://stackoverflow.com/questions/43664819/get-all-orders-ids-from-a-product-id
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

Name and Email of who places the order can maximally be caught on cred_commerce_after_order_completed, https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_order_completed, where you can use the User ID variable, which is the ID of the user that ordered the item.
Then use WordPress API to get the Email of that User (if it's an user on your system)
There is otherwise no method in Forms API to get the email and name of that private data of the user/client.
You can try to grab it from the $_POST of the form, or use WooCommerce's API if they offer any for it.
However this would require custom code.

Thanks!

#1223338

I am not sure why CRED commerce dosnt link the post created with WooCommerce order. I see many have requested the feature in past.

Anyway, can you help me out with the custom code I mentioned? How do I get order number and update it in custom field of post created?

Thanks,
Sasank

#1223340

You could (and should, if you want this implemented) suggest this here:
https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

Only then, the product manager can see your need for this and implement it given the number of requests is high enough.

As mentioned, the order number is available in the Toolset API on the mentioned hooks.
Please see https://toolset.com/forums/topic/cred-commerce-order-link-to-created-post/#post-1223336
The Hook that features the order ID is cred_commerce_after_order_completed.

I can unfortunately not help with the custom code above that, I can give examples on how to use Toolset's API - but not create ready to go codes on Toolset API or even third-party API; please see https://toolset.com/toolset-support-policy/ related to this.

However, with the hook mentioned you can get it directly, on the one Hook Toolset offers it.
Or, the custom code example as mentioned in my previous post (leading to https://stackoverflow.com/questions/43664819/get-all-orders-ids-from-a-product-id) can help to achieve this.

If you need personalized assistance on Custom Code, we can suggest to contact a Certified Contractor from this list:
https://toolset.com/contractors/

Thank you for understanding!