woocommerce offtopic - Adding a second button next to the shop button Woocommerc

admin2025-06-02  3

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

So what my clients wants is on the shop page, on each product there to be a second button next to the shop button. This button should lead to a custom for each product pdf file, that the client can download wihtout the need to enter the exact product page itself. I am new to developing and this is a bit hard for me so if you guys and girls can help me i will be extremely thankful :)

Closed. This question is off-topic. It is not currently accepting answers.

Your question should be specific to WordPress. Generic PHP/JS/SQL/HTML/CSS questions might be better asked at Stack Overflow or another appropriate Stack Exchange network site. Third-party plugins and themes are off-topic for this site; they are better asked about at their developers' support routes.

Closed 6 years ago.

Improve this question

So what my clients wants is on the shop page, on each product there to be a second button next to the shop button. This button should lead to a custom for each product pdf file, that the client can download wihtout the need to enter the exact product page itself. I am new to developing and this is a bit hard for me so if you guys and girls can help me i will be extremely thankful :)

Share Improve this question asked Feb 25, 2019 at 11:11 TonyDTonyD 32 bronze badges 2
  • what is the shop button? Are you talking about add to cart or select options button? – Chinmoy Kumar Paul Commented Feb 25, 2019 at 11:14
  • yes, it is the add to cart button on the main shop page, where normaly all the products are displayed :) – TonyD Commented Feb 25, 2019 at 11:26
Add a comment  | 

1 Answer 1

Reset to default 0

Code is tested with 2019 theme. Open the functions.php file of your active theme and drop this code at end of the file.

add_action( 'woocommerce_after_shop_loop_item', 'wpse2019_add_pdf_download_button', 10 );
function wpse2019_add_pdf_download_button() {
    global $product;

    if( $product ) {
        printf( '<a href="%s" class="pdf-button button btn" role="button">%s</a>', "YOUR PDF LINK HERE", __( 'DWONLOAD PDF', 'text-domain' ) );
    }
}

default add to cart and select options buttons are adding via woocommerce_after_shop_loop_item hook (source: templates/archive-product.php). I use the same hook and add the 2nd custom button. You will replace YOUR PDF LINK HERE with your PDF link.

If you will add different PDF file for every product, you will use the custom field logic. You can use the WordPress native custom field option or some free plugin like ACF/PODs/CMB2 etc

Assume your custom field would be wc_product_pdf_file. Then you can try this code

add_action( 'woocommerce_after_shop_loop_item', 'wpse2019_add_pdf_download_button', 10 );
function wpse2019_add_pdf_download_button() {
    global $product;

    if( $product ) {
        $pdf_link = get_post_meta( $product->get_id(), 'wc_product_pdf_file', true );
        if( ! empty( $pdf_link ) ) {
            printf( '<a href="%s" class="pdf-button button btn" role="button">%s</a>', $pdf_link, __( 'DWONLOAD PDF', 'text-domain' ) );
        }
    }
}

get_post_meta function is fetching the post meta data. $product->get_id() is giving the current product ID.

Note: Before editing the file, you keep a backup of your theme.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748869303a314396.html

最新回复(0)