2

I'm to go check if a product added to cart and purchased is in an order. I.e when a user purchase a variable product along with other variable products, i want to check if a specific variable product is added and purchased. So that i can dynamically show some information on the Thankyou Page and email sent to both the admin and customer.

I have tried to use "Checking that a specific attribute value is used in a cart Item (product variation)" answer code, but this doesn't work on Thankyou page once the product is purchased.

What I am trying is to display a dynamic content if products in the order has an attribute value of "Custom", as well as display an additional table row to the order table if there is a product in the order having the attribute value "order", with the following code:

function add_custom_row_to_order_table( $total_rows, $myorder_obj  ) {
     if ( is_attr_in_cart('custom') ) {
            $cmeasuement = __( 'Yes', 'domain' );
      }else{
            $cmeasuement = __( 'No', 'domain' );
     }

    $total_rows['el_custom'] = array(
       'label' => __('Custom Required?', 'domain'),
       'value'   => $cmeasuement,
    );

    return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'add_custom_row_to_order_table', 10, 2 );

But I keep Getting "No" (see the screenshot below) and the reason is because the is_attr_in_cart('custom') function is not detecting if the attribute is in the order. Help in the right direction to getting it to detect if an order has a product with specific attribute value.

enter image description here

Any help is appreciated.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399

1 Answers1

2

To make it work with WooCommerce orders you need another custom conditional function (where $attribute_value is the slug value of a product attribute that you are targeting):

function is_attr_in_order( $order, $attribute_value ){
    $found = false; // Initializing

    // Loop though order items
    foreach ( $order->get_items() as $item ){
        // Only for product variations
        if( $item->get_variation_id() > 0 ){
            $product = $item->get_product(); // The WC_Product Object
            $product_id = $item->get_product_id(); // Product ID

            // Loop through product attributes set in the variation
            foreach( $product->get_attributes() as $taxonomy => $term_slug ){
                // comparing attribute parameter value with current attribute value
                if ( $attribute_value === $term_slug ) {
                    $found = true;
                    break;
                }
            }
        }
        if($found) break;
    }

    return $found;
}

Now this conditional function will work in your code on order received page (thankyou), adding an additional row to total table with "Yes" if the product attribute is found or "No" if not:

add_filter( 'woocommerce_get_order_item_totals', 'add_custom_row_to_order_table', 10, 3 );
function add_custom_row_to_order_table( $total_rows, $order, $tax_display  ) {
    $domain    = 'woocommerce'; // The text domain (for translations)
    $term_slug = 'custom'; // <==  The targeted product attribute slug value

    $total_rows['custom'] = array(
       'label' => __( "Custom Required?", $domain ),
       'value'   => is_attr_in_order( $order, $term_slug ) ? __( "Yes", $domain ) : __( "No", $domain ),
    );

    return $total_rows;
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.


If you need to only target Order received page you will use inside your hooked function this condition:

if( is_wc_endpoint_url('order-received') ) {
    // The code comes here
}
return $total_rows; // The final filter return outside
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399