Is it possible to have a "download" button for listings that are free downloads?

Hi there! I have a marketplace set up in which some items listed are downloads. When the price is set to $0 or not filled in, the front end will only show the “reply to listing” button. But I would like there to be a “claim free download” button in this case. Is it possible to do that with a code snippet?

Hi @shoplocalcraft,

I think you should be able to achieve this without any custom solutions!

Here’s how to set it up:

  1. Create the Attachment Attribute
  • Go to HivePress > Listings > Attributes
  • Add a new attribute (e.g., “Free Download”)
  • Set Field Type to Attachment
  • Mark it as Editable (so vendors can upload files)
  • Leave it not required (to keep it optional)
  1. Configure the Display
  • In the Display section, assign it to your desired area (e.g., “Page Primary”)
  • In the Format field, use something like this to create a button:
    <a href="%value%" target="_blank" class="button button--large button--primary hp-button--wide">Claim Free Download</a>

This will render as a styled button with your custom text instead of showing the raw file link.

Note:

  • The %value% placeholder will be replaced automatically with the attachment URL.
  • You can customize the button styling by adjusting the CSS classes or adding inline styles.
  • Vendors will upload the file when creating/editing their listing, and it will display as a button on the listing page.
    This is a clean, no-code solution that keeps your free downloads organized and user-friendly!

I hope this helps!

Cheers,
Chris :victory_hand:

This is such a cool solution! However, I suspect I will run into a couple of problems. I have file attachments required specifically for the Digital Downloads category (using a code snippet), and I want my vendors to be able to charge for their files if they prefer. How might I tackle making it possible to add a Downloadable listing for free as well as leave it possible to add/update a price?

Hi @shoplocalcraft,

I think the most simple solution would be a second optional ‘free download’ attribute. In theory, your vendors could then even offer both a paid download and free ‘sample’ of sorts. Else, you could adjust your custom snippet to dynamically show attribute fields, or make the snippet an ‘either, or’ snippet pointing and both the free and paid attribute fields.

I hope that makes sense. I’m about to go to bed, but if you tell your favourite AI chat bot the attribute slug names and what you’re trying to achieve, you’ll likely be able to tweak this to your liking quite easily.

Let us know how you get on, and do share your solution if you come up with one. I’m sure others in the community will find it useful! :slight_smile:

Cheers,
Chris :victory_hand:

Hi,

Chris suggested a great solution. I think adding an attachment-type attribute called “Free Download” or something similar could be a good workaround. You could keep all the existing fields required for paid downloads and add this extra field for listings that offer free downloads.

I’d also like to clarify one thing in case I’ve misunderstood your requirements. HivePress has a built-in digital downloads feature that makes purchased attachments available for download in the Orders section once the order has been paid.

You can configure this feature under HivePress > Settings > Listings > Selling > Attachments. Enabling it adds an attachment field to the listing submission form, allowing vendors to upload the files they want to sell. Once a customer completes the purchase, the file becomes available for download in their order. As far as I understand, you’re currently using a code snippet to achieve this.

Hope this helps

P.S. Please note that we have to move this topic to the Marketplace forum category. Since it’s related to a premium extension, the forum category is restricted, please add the license key to the account settings or consider renewing support if you still need assistance related to the premium themes or extensions Renew Support | HivePress

To clarify, I am using the HivePress built-in digital downloads feature that makes purchased attachments available for download in the Orders section once the order has been paid. I am also using a code snippet that makes the file attachment required only for the specific category of Digital Downloads (because I also have categories with physical items).

Ideally, I would like to have Free Downloads for some listings, and I’d like to use the built-in HivePress digital downloads feature for this.

The problem is that when an item has no price or is priced at $0, the “buy” button doesn’t show up. I am looking for a code snippet that would force the “buy” button to show up even when the price is $0. Optionally, I could make the “buy” button say “claim free download.”

Thank you!

I did try to get a code snippet to work but it seems that HivePress strongly filters out my attempt!

This snippet DOES NOT work, for reference:

add_filter(
    'hivepress/v1/blocks/listing_buy_form',
    function ( $block ) {
        // Access the current template context
        if ( ! isset( $block['template'] ) ) {
            return $block;
        }
        
        $listing = $block['template']->get_context( 'listing' );
        
        if ( $listing ) {
            // REPLACE THIS NUMBER with your specific HivePress category ID
            $target_category_id = YOUR_CATEGORY_ID; //I DID change this to the correct category!
            
            // Get all category IDs assigned to the current listing
            $assigned_categories = $listing->get_categories__id(); 
            
            // If the listing does not belong to the target category, skip processing
            if ( ! is_array( $assigned_categories ) || ! in_array( $target_category_id, $assigned_categories ) ) {
                return $block;
            }
            
            // Check the listing price
            $price = $listing->get_price();
            
            // If the price is missing or exactly 0, forcefully configure the button properties
            if ( empty( $price ) || 0 == $price ) {
                // Ensure the block type is set to part so it attempts to render
                $block['type'] = 'part';
                $block['path'] = 'listing/view/listing-buy-form';
                
                // Inject the customized button label string
                $block['attributes'] = array_merge(
                    isset( $block['attributes'] ) ? $block['attributes'] : [],
                    [
                        'data-label' => esc_html__( 'Claim Free Download', 'hivepress' ),
                    ]
                );
            }
        }
        
        return $block;
    },
    1000
);

(I changed YOUR_CATEGORY_ID with my Digital Downloads category ID of 53.)

I have my solution!

It required altering how the template is shown, but this works as intended.
(Change YOUR_CATEGORY_ID to the category/categories you want this to affect.)

add_filter(
    'hivepress/v1/templates/listing_view_page/blocks',
    function ( $blocks, $template ) {
        $listing = $template->get_context( 'listing' );
        
        if ( $listing ) {
            // REPLACE THIS NUMBER with your specific HivePress category ID
            $target_category_id = YOUR_CATEGORY_ID; 
            
            // Get all category IDs assigned to the current listing
            $assigned_categories = $listing->get_categories__id(); 
            
            // If the listing does not belong to the target category, skip processing entirely
            if ( ! is_array( $assigned_categories ) || ! in_array( $target_category_id, $assigned_categories ) ) {
                return $blocks;
            }
            
            // Check the listing price directly
            $price = $listing->get_price();
            
            // Only trigger this if the price is empty, null, or exactly 0
            if ( empty( $price ) || 0 == $price ) {
                
                // Get the real WooCommerce product linked to this HivePress listing
                global $wpdb;
                $product_id = $wpdb->get_var( $wpdb->prepare(
                    "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_type = 'product' LIMIT 1",
                    $listing->get_id()
                ) );

                // Fallback: If no child product is mapped, use the listing ID directly
                if ( ! $product_id ) {
                    $product_id = $listing->get_id();
                }
                
                // Construct a direct add-to-cart link targeting the WooCommerce product ID
                $claim_url = wc_get_checkout_url() . '?add-to-cart=' . $product_id;
                
                // Inject using your specific theme button design classes
                $blocks = hivepress()->helper->merge_trees(
                    [ 'blocks' => $blocks ],
                    [
                        'blocks' => [
                            'listing_actions_primary' => [
                                'blocks' => [
                                    'custom_free_download_button' => [
                                        'type'       => 'content',
                                        'content'    => '<div class="hp-form hp-form--listing-buy"><div class="hp-form__actions"><a href="' . esc_url( $claim_url ) . '" class="button button--large button--primary alt" style="display: inline-flex; width: 100%; text-align: center; justify-content: center; align-items: center;"><span>' . esc_html__( 'Claim Free Download', 'hivepress' ) . '</span></a></div></div>',
                                        '_order'     => 5,
                                    ],
                                ],
                            ],
                        ],
                    ]
                )['blocks'];
            }
        }
        
        return $blocks;
    },
    1000,
    2
);

Thanks for the details and for sharing your solution. It could be helpful to other users as well.

We’ll also consider adding support for listings with a price of 0 or allowing the Marketplace functionality to be disabled for specific categories where listings are free.