Skip Navigation

[Resolved] get post id created by cred commerce

This thread is resolved. Here is a description of the problem and solution.

Problem: I would like to get the ID of the post created by a Form that uses Forms Commerce.

Solution: Use the cred_commerce_after_payment_completed API to access the order number:

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
}

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/

0% of people find this useful.

This support ticket is created 6 years, 3 months ago. There's a good chance that you are reading advice that it now obsolete.

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.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

Author
Posts
#1097333

How to get post id created by cred commerce?

#1097364

CRED Commerce does not create Posts.
It connects Toolset Forms, to WooCommerce Products.

Then, the Toolset Form which creates either Posts or Users, will not let you do so until a certain WooCommerce product is bought.

Now, if you need to have the Post ID of the Post that Toolset Forms generates, then it depends a little where you need this.

For example, you can access it with the Toolset Forms API at any moment of the Forms' life, or later during the Toolset Commerce Form process:
https://toolset.com/documentation/programmer-reference/cred-api/
https://toolset.com/documentation/programmer-reference/cred-commerce-api/

For example, when you submit the Form, the Post ID is in the variable $post_id of cred_save_data()
https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

Later, during the checkout process, for example, after payments are complete (cred_commerce_after_payment_completed()), you can access that Post ID in the $data['cred_post_id']:
https://toolset.com/documentation/programmer-reference/cred-commerce-api/#cred_commerce_after_payment_completed

#1097427

Hello Beda,

Thank you for your reply.

I already tried ( after payments are complete ) with no luck to get to the created post so I can display it within the order information.

I'm moving the setup to a live subdomain so I can grant you access to see the problem as I'm currently using docker container on my local machine to do the development.

Thank you,

Ahmed

#1097940

Hi, try this variation instead:

add_action( 'cred_commerce_after_payment_completed', 'my_hook', 10, 1 );
function my_hook( $data ) {
    $post_id = $data['extra_data'][0]['cred_post_id'];
}

The cred_post_id variable should be defined in the first item in the extra_data array. If that does not work, I'll be glad to take a look once you have the test site up and running. Please provide login credentials in the private reply fields here.

#1100646

Here's how to access the cred_post_id in your cred_commerce_after_payment_completed callback:

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

If that does not work as expected please add an error log statement so we can inspect the data parameter:

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
error_log(print_r($data, true));

Then turn on server logs and complete the payment once more. Let me know what you find in the logs. If you're not familiar with server logs I can show you how to activate them temporarily. Go in your wp-config.php file and look for define(‘WP_DEBUG’, false);. Change it to:

define('WP_DEBUG', true);

Then add these lines, just before it says 'stop editing here':

ini_set('log_errors',TRUE);
ini_set('error_reporting', E_ALL);
ini_set('error_log', dirname(__FILE__) . '/error_log.txt');

When the code is triggered, a file called error_log.txt will be created in your site's root directory. Send me its contents.

#1100777

Hello Christian,

I already provided the credentials for the ftp and site access. Are you able to debug it from you end?

#1100820

I have added this code:

    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-confirmation-number', $order_number);
    }

Then I ran several tests. Here you can see the Booking I created:
hidden link

Here is the corresponding Order:
hidden link

You can see in the Booking post that the confirmation number is set to the corresponding Order ID. In these tests, I used the "Check payment" payment gateway to process payments. I chose products that cost some amount (not free products). After Checkout, I went into wp-admin and marked the Order to be "Processing". This triggered the payment complete action, and the confirmation number was added. Then I marked the Order "Complete". This triggered the Booking post to be published.

Please let me know if I've misunderstood what you want to accomplish.

#1100872

Hello Christian,

Thank you for your support. A good move to show the confirmation number.
But, unfortunately by disabling:

 //WC()->cart->add_to_cart( $product_id, $numberdays );

the quantity of the product in the cart will not update to reflect the actual number of days so the amount is not calculated based on the number of days.

#1101633
WC()->cart->add_to_cart( $product_id, $numberdays );

This code is incompatible with the Commerce Forms API cred_commerce_add_product_to_cart because it adds a product to the cart programmatically, bypassing Commerce Forms internal processes. This API should return a single product ID, which is then added to the cart by Commerce Forms. So the problem is that you need to modify the quantity of the product in the cart based on the number of days, is that correct? We can do that with another hook and a hidden field that stores the product ID in the booking post. I have changed the code and added a cred_commerce_after_add_to_cart hook to set the quantity programmatically:

/*  ====================================================    */
/*  To Add The Booked / Pack Original Post to the Cart      */
/*  ====================================================    */
    add_filter('cred_commerce_add_product_to_cart','add_custom_product_to_cart', 10, 3 );
    function add_custom_product_to_cart( $product_id, $form_id, $post_id ) {
        /*  ====================================================================    */
        if ( $form_id == 12997 ) {
            $packid = toolset_get_relationship('product-pack');

            if ( $packid ) {
                $product_id = $packid;
                global $post;
                $product_id = $post->ID;
                error_log("product id is : " . $product_id);
            }

            //WC()->cart->add_to_cart( $product_id );
        }
        /*  ====================================================================    */
        if ( $form_id == 12996 ) {
            $bookid = toolset_get_relationship('product-booking');

            /*  get booking fields   */
            $date_from   = get_post_meta($post_id, 'wpcf-booking-from', true);
            $date_to     = get_post_meta($post_id, 'wpcf-booking-to',   true);
            //$numberdays  = get_post_meta($post_id, 'wpcf-booking-days', true);

            if ( $bookid ) {
                $product_id = $bookid;
                global $post;
                $product_id = $post->ID;
                error_log("product id is : " . $product_id);
            }

            //WC()->cart->add_to_cart( $product_id, $numberdays );

            /*  set booked fields   */
            update_post_meta( $post_id, 'wpcf-booked-from',  $date_from );
            update_post_meta( $post_id, 'wpcf-booked-to',    $date_to );
			update_post_meta( $post_id, '_cred_product_id', $product_id);
        }
        /*  ====================================================================    */
		return $product_id;
    }

/*  ====================================================    */
/*  Modify the quantity added to the Cart by Commerce Form      */
/*  ====================================================    */
add_action( 'cred_commerce_after_add_to_cart', 'forms_commerce_modify_booking_qty', 10, 2 );
function forms_commerce_modify_booking_qty( $form_id, $post_id ) {
     if ( $form_id == 12996 ) {
	 	$numberdays  = get_post_meta($post_id, 'wpcf-booking-days', true);
		$product_id = get_post_meta($post_id, '_cred_product_id', true);
		foreach (WC()->cart->get_cart() as $cart_item_key => $values) {
            if ($values['product_id'] == $product_id) {
				WC()->cart->set_quantity($cart_item_key, $numberdays, true);
				break;
			}
		}
	 }
}
#1101682
Screen Shot 2018-09-06 at 11.32.34 AM.png
Screen Shot 2018-09-06 at 11.31.48 AM.png
Screen Shot 2018-09-06 at 11.31.30 AM.png

Hello Christian,

I tried to make a booking after the modification you kindly made.
The quantity still not updating in the cart.

#1101703

Oh sorry, I left a comment in the code. It should be working now.

#1101712

Cool It displays the quantity.

Is there any possibility to display the id of the created new booking on the frontend cart / checkout page?

I already placed a view on the cart layout

Thank you,

#1101724

The checkout page isn't managed by Toolset, so I don't have a cut-and-paste solution that will allow you to modify that page. It may take some assistance from the WooCommerce support team or community. If you are able to figure out how to insert some content into the WooCommerce Checkout template using shortcodes, you could create a View of Booking posts, filtered by post status "Pending", filtered by post author where the author of the post is the current User. Order the View by post date, in descending order, and show only 1 result. In the Loop, output the post title using a shortcode. Then insert this View in the cart Layout or the checkout page template.

#1101726

Hello Christian,

Thank you for instant reply.
I already created a layout for the cart page and added a view to display the booking information.
The only missing part is to inject the newly created post id so it displays this exact same post. [ So, I can delete that post if the customer didn't process the payment]

How to create a shotcode that makes use of the id within the ( cred_commerce_add_product_to_cart ) without breaking the code?

#1101728

Sorry but I don't understand what you're asking. Like I was saying before, create a View of Booking posts, filtered by post author and post status, in reverse chronological order, and only show one result. Then in the Loop you can output the post ID or post title or whatever you want to show. Can you explain why this does not work for what you need?