Created
January 24, 2024 07:24
-
-
Save rajucs/0f1673f9d4c3592471480a1d1e1845b1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add Custom Product Note Field | |
add_action('woocommerce_single_product_summary', 'custom_product_note', 100); | |
function custom_product_note() { | |
echo '<br><div>'; | |
woocommerce_form_field('customer_note', array( | |
'type' => 'textarea', | |
'class' => array('my-field-class form-row-wide'), | |
'label' => __('Product note'), | |
'placeholder' => __('Add your note here, please…'), | |
'required' => false, | |
), ''); | |
echo '</div>'; | |
?> | |
<script type="text/javascript"> | |
jQuery(function ($) { | |
$('#customer_note').on('input blur', function () { | |
$('#product_note').val($(this).val()); | |
}); | |
}); | |
</script> | |
<?php | |
} | |
//Add a Hidden Field | |
add_action('woocommerce_before_add_to_cart_button', 'hidden_field_before_add_to_cart_button', 5); | |
function hidden_field_before_add_to_cart_button() { | |
echo '<input type="hidden" name="product_note" id="product_note" value="">'; | |
} | |
//Capture Product Note in Cart Item Data | |
add_filter('woocommerce_add_cart_item_data', 'add_product_note_to_cart_item_data', 20, 2); | |
function add_product_note_to_cart_item_data($cart_item_data, $product_id) { | |
if (isset($_POST['product_note']) && !empty($_POST['product_note'])) { | |
$product_note = sanitize_textarea_field($_POST['product_note']); | |
$cart_item_data['product_note'] = $product_note; | |
} | |
return $cart_item_data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment