I have two custom field in a specific product. I wanna add each custom field after each variation name in cart and checkout page.
add_action('woocommerce_before_add_to_cart_button', 'action_before_add_to_cart_button', 20);
function action_before_add_to_cart_button() {
global $post, $product;
if (is_product() && $post->ID == 19652) {
?>
<div class="custom-fields">
<div>
<input class="form-control alt" name="first_custom_name" type="text"
placeholder="<?php esc_html_e('Name for First bottle'); ?>" required>
</div>
<div>
<input class="form-control alt" type="text" name="sec_custom_name"
placeholder="<?php esc_html_e('Name for Second bottle'); ?>">
</div>
</div>
<?php
}
}
This is the custom field for the product page, but I have no clue how to extract the data from the custom field to the cart order line. I have seen somewhere to update the meta function, but since the data is input by customers, data is updated in meta. What if a different variation is chosen? Does it have the same data? I want to add different data for different variations. Is this possible?
The following code, will save your custom fields, displaying them in cart, checkout, orders and email notifications.
Code goes in functions.php file of your child theme (or in a plugin). Tested and work.