Radu
Radu

Reputation: 45

Conditional WooCommerce Order Received text on based on Product or Category?

For Woocommerce, I found this piece of code but I need to make it conditional on product id:

add_action( 'woocommerce_thankyou', 'custom_thankyou_text', 1, 0);
function custom_thankyou_text(){
    echo '<p class="thankyou-custom-text">If you do not see the download button(s) below, please refresh the page. Processing can take a few minutes.</p>';
}

How can I show a specific WooCommerce thank you page text based on the products they purchased?

Also I found this conditions example that would fit (without the redirection, which I don't need):

add_action( 'template_redirect', 'wc_custom_redirect_after_purchase' );
function wc_custom_redirect_after_purchase() {
    if ( ! is_wc_endpoint_url( 'order-received' ) ) return;

    // Define the product IDs in this array
    $product_ids = array( 37, 25, 50 ); // or an empty array if not used
    // Define the product categories (can be IDs, slugs or names)
    $product_categories = array( 'clothing' ); // or an empty array if not used
    $redirection = false;

    global $wp;
    $order_id =  intval( str_replace( 'checkout/order-received/', '', $wp->request ) ); // Order ID
    $order = wc_get_order( $order_id ); // Get an instance of the WC_Order Object

    // Iterating through order items and finding targeted products
    foreach( $order->get_items() as $item ){
        if( in_array( $item->get_product_id(), $product_ids ) || has_term( $product_categories, 'product_cat', $item->get_product_id() ) ) {
            $redirection = true;
            break;
        }
    }

    // Make the custom redirection when a targeted product has been found in the order
    if( $redirection ){
        wp_redirect( home_url( '/your-page/' ) );
        exit;
    }
}

Is there a way to combine the two for the needed result?

Upvotes: 1

Views: 1630

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253949

This can be done easily in the example below, where you will have to define the targeted product IDs and a Product category (for testing). So this example will display a custom message:

  • for specific product IDs
  • for specific product categories
  • for all other cases

The code:

add_action( 'woocommerce_thankyou', 'custom_thankyou_text', 1, 1);
function custom_thankyou_text( $order_id ){
    // HERE Define Your product IDs below
    $product_id1 = 30;
    $product_id2 = 40;

    // HERE Define Your product category (ID, slug or name)
    $category = array('clothing');

    // Get the WC_Order object (an instance)
    $order = wc_get_order( $order_id );
    $product_ids = array();
    $has_category = false;

    // Loop through the order items
    foreach( $order->get_items() as $item ){
        // PRODUCT ID: Store the product ID in an array
        $product_ids[] = $item->get_product_id(); 

        // PRODUCT CATEGORY
        if( has_term( $category, 'product_cat', $item->get_product_id() ) )
            $has_category = true;
    }
    // For first product ID 
    if( in_array( $product_id1, $product_ids ) ){
        echo '<p class="thankyou-custom-text">Custom message for Product ID .'.$product_id1.'</p>';
    }
    // For Second product ID 
    elseif( in_array( $product_id2, $product_ids ) ){
        echo '<p class="thankyou-custom-text">Custom message for Product ID .'.$product_id1.'</p>';
    }
    // For product category 
    elseif( $has_category ){
        echo '<p class="thankyou-custom-text">Custom message for Product Category.</p>';
    } 
    // For all other cases
    else {
        echo '<p class="thankyou-custom-text">If you do not see the download button(s) below, please refresh the page. Processing can take a few minutes.</p>';
    }
}

Code goes in function.php file of the active child theme (or active theme).

Tested and works

Upvotes: 1

Related Questions