WP SITES

3083 Coded Tutorials & 286 Plugins

Custom Button Link Per Product in WooCommerce

This PHP code enables you to add a custom button on your single product page with a custom link. You can add the link using WordPress custom fields or a custom field plugin like ACF.

  • Key used for WP custom fields is wp_button.
  • For ACF, use acf_button.

If ACF not active, WP custom fields will be used.

add_action( 'woocommerce_before_add_to_cart_form', 'custom_field_button' );
function custom_field_button() {

    $wp = get_post_meta( get_the_ID(), 'wp_button', true );
	
    $acf = class_exists('acf') ? get_field('acf_button') : '';
	
    $field = $acf ? $acf : $wp;
    
    if ( $field && is_singular('product') ) {
		
    printf( '<a href="%s" class="button" target="_blank" rel="”nofollow” noopener">' . __( 'Custom Button' ) . '</a>',  $field ); 
    
    }

}

The code enables you to open the link in the same window or new window and add the nofollow attribute to the link on a per product basis.

Use this file to import the ACF settings.

Works in any WordPress theme running WooCommerce.

= Related Solutions =

Leave a Reply

New Plugins