I have a function that replaces the add to cart button and adds specific content on downloadable products. There are two type of downloadable products; 'Free Samples' and 'Activity Recipes'. These keywords are added as product tags and used to determine which is what when querying downloadable product. Which sounds simple enough, but my issues is that when you add more than one tag to a downloadable product, my code below is not picking up on the tag I need it to and then display the relevant code.
When printing the value of the variable $product_tag = null
, so this tells me something is wrong with $product_tag = get_term($tag)->name;
, but I cannot figure out what it needs to be or even if that is the issue.
So my question is how do I display specific content on downloadable products depending on if it has a specific tag in it's array of tags.
global $product;
if ( $product->is_downloadable('yes') ) {
if ( $product->get_price() > 0 ) {
do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' );
} else {
$downloads = $product->get_downloads();
$tags = $product->tag_ids;
foreach($tags as $tag) {
$product_tag = get_term($tag)->name;
}
foreach( $downloads as $key => $download ) {
if ($product_tag == 'Activity Recipes') {
echo '
<h4>Step 1</h4>
<a href="' . esc_url( $download['file'] ) . '" target="blank" class="btn btn-secondary ">Download FREE template</a>
<hr />
<h4>Step 2 (Optional)</h4>
<p>Choose your products below as needed to help bring the activity to life:</p>
<a class="btn btn-secondary" href="#details">Choose products</a>
';
} else if ($product_tag == 'Free Samples') {
echo '<p class="download-link"><a href="' . esc_url( $download['file'] ) . '" target="blank" class="btn btn-secondary ">Download FREE ' . $download['name'] . '</a></p>';
}
}
}
} else {
do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' );
}
My Answer:
To display specific content on downloadable products depending on if it has a specific tag in its array of tags, you can modify your code as follows:
php
global $product;
if ( $product->is_downloadable('yes') ) {
if ( $product->get_price() > 0 ) {
do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' );
} else {
$tags = wp_get_post_terms( $product->get_id(), 'product_tag', array( 'fields' => 'names' ) );
foreach( $tags as $tag ) {
if ($tag == 'Activity Recipes') {
echo '
Step 1
Download FREE template
Step 2 (Optional)
Choose your products below as needed to help bring the activity to life:
Choose products
';
} else if ($tag == 'Free Samples') {
$downloads = $product->get_downloads();
foreach( $downloads as $key => $download ) {
echo '';
}
}
}
}
} else {
do_action( 'woocommerce_' . $product->get_type() . '_add_to_cart' );
}
In this updated code:
1. We are using `wp_get_post_terms` function to get the product tags.
2. We are iterating through each tag and checking if it matches 'Activity Recipes' or 'Free Samples'.
3. Depending on the tag, we are displaying the specific content for that tag.
4. If the product is not downloadable, we are still displaying the default add to cart button.
Make sure to replace `'product_tag'` with the actual taxonomy name of your product tags if it's different.
Rate this post
5 of 5 based on 2384 votesComments