Add a custom code with custom link after add to cart for every product

You need to add custom meta box such as product_ext_url for product post type. Then use hook below to add your code in single product page.

add_action( 'woocommerce_after_add_to_cart_button', 'your_function' );

function your_function(){
  echo get_post_meta( $product_id, 'product_ext_url' );

In above function you will print external url of current product after add-to-cart button.


To add a custom meta box to WooCommerce, you can use the following steps:

Create a function to add the meta box:

function my_custom_meta_box(){
        'My Meta Box Title',
add_action( 'add_meta_boxes', 'my_custom_meta_box' );

Define the callback function for the meta box:

function my_metabox_callback( $post ){
    echo '<label for="my_custom_field">My Custom Field</label>';
    $value = get_post_meta( $post->ID, '_my_custom_field', true );
    echo '<input type="text" id="my_custom_field" name="my_custom_field" value="' . esc_attr( $value ) . '">';

This function will add a text input field for the custom field.

Save the data from the meta box:

function save_custom_meta_box( $post_id ){
    if( isset( $_POST['my_custom_field'] ) )
        update_post_meta( $post_id, '_my_custom_field', sanitize_text_field( $_POST['my_custom_field'] ) );
add_action( 'save_post', 'save_custom_meta_box' );

This function will save the custom field data when the product is updated or published.

That’s it! This should add a custom meta box to WooCommerce for the product post type.