0

Let's say I need to add a custom code so visitors can visit the product's external site. That way they can visit the product's site.

What I want to achieve is as follows:

Add a custom code after the add to cart button; The link to the custom code needs to be set in each product edition. Thanks

<script type="text/javascript">
function importHotmart(){ 
    var imported = document.createElement('script'); 
    imported.src = 'https://static.hotmart.com/checkout/widget.min.js'; 
    document.head.appendChild(imported); 
    var link = document.createElement('link'); 
    link.rel = 'stylesheet'; 
    link.type = 'text/css'; 
    link.href = 'https://static.hotmart.com/css/hotmart-fb.min.css'; 
    document.head.appendChild(link);    } 
importHotmart(); 

 </script> 
 a onclick="return false;" href="*" class="hotmart-fb hotmart__button-checkout">Buy now</a

to woocommerce store

3
  • <script type="text/javascript"> function importHotmart(){ var imported = document.createElement('script'); imported.src = 'static.hotmart.com/checkout/widget.min.js'; document.head.appendChild(imported); var link = document.createElement('link'); link.rel = 'stylesheet'; link.type = 'text/css'; link.href = 'static.hotmart.com/css/hotmart-fb.min.css'; document.head.appendChild(link); } importHotmart(); </script> <a onclick="return false;" href="*" class="hotmart-fb hotmart__button-checkout">Buy now</a> to woocomerce store Mar 11 at 10:19
  • i want add this code to each product page but the code has diffrent link for each product to make checkout external, i want add this code with different link for each products, please help me how i can do that (href="***********") Mar 14 at 7:52
  • this code up has a poup checkout link, for each product, I have a different checkout link I change it I code up, I want to add the code up for each product with a different checkout link for each product Mar 14 at 9:36

1 Answer 1

0

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.

// UPDATE

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(){
    add_meta_box(
        'my_metabox_id',
        'My Meta Box Title',
        'my_metabox_callback',
        'product',
        'normal',
        'high'
    );
}
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.

7
  • I have different link in code for each product, please how i can do that Mar 12 at 9:11
  • The way I say is for each product. every product edit page has an input box that can contain external url of that product. and then in single product page you can call the post meta related to external url. I will edit the code above.
    – wprah
    Mar 13 at 16:38
  • did you get it? or you want a complete answer?
    – wprah
    Mar 13 at 16:41
  • i want add this code to each product page but the code has different link for each product to make checkout external, i want add this code with different link for each products, please help me how i can do that (href="***********") Mar 14 at 7:52
  • Yes, I want a complete answer. Mar 14 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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