Skip to content
Advertisement

Add variable product downloadable files based on variation attributes in WooCommerce

I am trying to check if a product variation is currently in the cart, and add dynamic content to the email & thank you pages based on which variable products are present.

The dynamic content would be generated via custom file input fields (ACF) added to the product on the admin side.

I’ve managed to put together the following code:

add_filter( 'woocommerce_get_order_item_totals', 'add_new_rows', 10, 3 );
function add_new_rows( $total_rows, $order, $tax_display  ) {
    $domain = 'woocommerce'; // The text domain (for translations)
    $count = 1;
    foreach( $order->get_items() as $item_id => $item ){
        
        $variation_id = false;
        $variation_id = $item->get_variation_id();

        //Product Info
        $product = $item->get_product();
        $title=$product->get_title();
        $parent_id=$product->get_parent_id();
        $mp3 = get_field('mp3', $parent_id);
        $wav = get_field('wav', $parent_id);
            
            if($product->is_type('variation')){
                // Get the variation attributes
                $variation_attributes = $product->get_variation_attributes();
                // Loop through each selected attributes
                foreach($variation_attributes as $attribute_taxonomy => $term_slug){
                    $taxonomy = str_replace('attribute_', '', $attribute_taxonomy );
                    // The name of the attribute
                    $attribute_name = get_taxonomy( $taxonomy )->labels->singular_name;
                    // The term name (or value) for this attribute
                    $attribute_value = get_term_by( 'slug', $term_slug, $taxonomy )->name;
                    if ($attribute_value = 'Basic' ){
                        // Return Basic output
                        $total_rows['Basic '.$count]['label'] = __( "$title MP3", 'woocommerce' ); // The row shipping label
                        $total_rows['Basic '.$count]['value'] = "<a href='$mp3' download>Download MP3</a>";
                    }
                    if ($attribute_value = 'Premium' ){
                        // Return Basic AND Premium output
                        $total_rows['Basic '.$count]['label'] = __( "$title MP3", 'woocommerce' ); // The row shipping label
                        $total_rows['Basic '.$count]['value'] = "<a href='$mp3' download>Download MP3</a>";
                        $total_rows['Premium '.$count]['label'] = __( "$title WAV", 'woocommerce' ); // The row shipping label
                        $total_rows['Premium '.$count]['value'] = "<a href='$wav' download>Download Wav</a>";
                    }
                }
            }
     $count += 1;
    }
    return $total_rows;
}

Resulting in…

Output

As you can see it is currently displaying all the proper links and labels.. however test item 1 should only be providing an MP3 link as it is a Basic product whereas products with ‘Premium’ selected like item 2 would provide the MP3 & WAV.

I believe my issue is that the if statements are only checking if the attributes/variables are present and not if they’re actually the active ones.

Any help would be appreciated.

Advertisement

Answer

The main problem comes from your if statements where you are using = (set a value) instead of == or === (compare a value).

So use instead the following revisited and simplified code:

add_filter( 'woocommerce_get_order_item_totals', 'add_new_rows', 10, 3 );
function add_new_rows( $total_rows, $order, $tax_display  ) {
    $domain = 'woocommerce'; // The text domain (for translations)
    $count  = 0;

    foreach( $order->get_items() as $item ){
        if ( $item->get_variation_id() > 0 ) {
            $product = $item->get_product();
            $title   = $product->get_name();

            $mp3 = get_field('mp3', $item->get_product_id());
            $wav = get_field('wav', $item->get_product_id());

            $count++;

            // Loop through each selected attributes
            foreach( $product->get_variation_attributes() as $attribute => $term_slug ){
                if ( 'Basic' === $term_slug ) {
                    // Return Basic output
                    $total_rows['Basic '.$count]['label'] = __( "$title MP3", 'woocommerce' ); // The row shipping label
                    $total_rows['Basic '.$count]['value'] = "<a href='$mp3' download>Download MP3</a>";
                }
                elseif ( 'Premium' === $term_slug ) {
                    // Return Basic AND Premium output
                    $total_rows['Basic '.$count]['label'] = __( "$title MP3", 'woocommerce' ); // The row shipping label
                    $total_rows['Basic '.$count]['value'] = "<a href='$mp3' download>Download MP3</a>";
                    $total_rows['Premium '.$count]['label'] = __( "$title WAV", 'woocommerce' ); // The row shipping label
                    $total_rows['Premium '.$count]['value'] = "<a href='$wav' download>Download Wav</a>";
                }
            }
        }
    }
    return $total_rows;
}

Code goes in functions.php file of the active child theme (or active theme). it should better work.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement