1

Trying to create a simple function that will display a text note above the "add to cart" button on any page displaying a downloadable product in Woo Commerce. I can get it to work for simple products but not for variations. Can anyone help me adjust this code to work for variable products? Thanks so much.

Here's what I have:

//add text note to product page for all downloadable products
add_action( 'woocommerce_before_add_to_cart_button' , 'append_download_note' );
function append_download_note() {
  global $product;
    if ( $product->is_downloadable() )  {
      echo '<p>Please note: a link to your downloadable product will be 
      available on the purchase confirmation page.</p>';
  }
}
1

For variable product, it's not the product that you need to test but the variations :

add_action( 'woocommerce_before_add_to_cart_button' , function () {

    global $product;


    $downloadable = FALSE;


    if ("simple" === $product->get_type()) {

        $downloadable = $product->is_downloadable();

    } elseif ("variable" === $product->get_type()) {

        $variations = $product->get_available_variations();

        foreach ($variations as $variation) {

            if ($variation["is_downloadable"]) {
                $downloadable = TRUE;
                break;
            }

        }

    }



    if ($downloadable) {
          echo '<p>Please note: a link to your downloadable product will be 
          available on the purchase confirmation page.</p>';
    }


});
  • Hey mmm--your code is money. Thank you so much, I owe you a beer. Great solution, I've learned some key things here. You have no idea how many hours I was spinning my wheels on this problem. – Megunticook Jul 24 '18 at 19:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.