Conditional WooCommerce Confirmation/Order Received Page based on Product or Category?

I have two distinct products in a WooCommerce cart. One is a ticket, the other is a fee that must be paid before the user can upload a file (which is being handled by a Gravity Form I’ve created).

Currently, if I put the link to the page with the Gravity Form on the Order Received page, if someone purchases just a ticket, they would see this link and it would be confusing.

Read More

Is there a way to either have unique confirmation pages once a purchase is complete based on the product purchased?

If not, are there conditional tags or some kind of hook or filter that would only show the link to the Gravity Form on the Order Received page if the “fee product” is purchased (possibly based on product ID or category ID)?

I did find this code snippet:

https://sozot.com/how-to-hook-into-woocommerce-to-trigger-something-after-an-order-is-placed/

But when I try this:

add_action('woocommerce_payment_complete', 'custom_process_order', 10, 1);
function custom_process_order($order_id) {
    $order = new WC_Order( $order_id );
    $myuser_id = (int)$order->user_id;
    $user_info = get_userdata($myuser_id);
    $items = $order->get_items();
    foreach ($items as $item) {
    if ($item['product_id']==154) {
echo '<h3>If you are submitting a script, please <a href="http://www.ashlandnewplays.org/wp/submit-a-script/step-2/">click here to submit your script</a>.</h3>';
        }
    }
    return $order_id;
}

Nothing gets displayed on the Order Details screen. Oddly enough, if I try and use wp_redirect and force a redirect the page, it “works”, but it breaks the page and causes some strange embedding of the site within the checkout page.

Related posts

Leave a Reply

1 comment

  1. After beating on this incessantly, I finally found what I was looking for, so I thought I’d post it. In my case, I wanted to customize the Order Detail/Confirmation page with a link to a form, but only when a certain product is purchased.

    If you want something similar, put this in the order_details.php template:

    global $woocommerce;
    
    $order = new WC_Order( $order_id );
    
    /* This two lines above should already exist, but I have them here so you can 
    see where to put the code */
    
    foreach($order->get_items() as $item) {
        $_product = get_product($item['product_id']);
        if ($item['product_id']==154) {
            // Do what you want here..replace the product ID with your product ID
            }
        }
    

    I also tested this by inserting a wp_redirect within the if statement and it seemed to work great, so I imagine you could customize this code with a bunch of if/elseif statements that redirect to different landing pages depending on the product purchased! Hopefully this helps someone!