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:

JavaScript

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:

JavaScript

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