How to add an order notes field in Woocommerce cart page? - php

I would like to add a notes field in the Woocommerce cart page under Woocommerce cart coupon area. This field should be something similar to Woocommerce checkout page "Order Notes" field where the customer can add some notes.
So far I have this code that indicates my desired location:
add_action ('woocommerce_after_cart_table','add_content_below_cart_coupon');
function add_content_below_cart_coupon () {
echo 'this will show below the cart cuopon';
}
How can I add a notes field in this area so these customer notes would also appear in order details at checkout page?
Thanks!

I solved this problem but a little hacky, I suggest to put it in a plugin
/**
* Add the order_comments field to the cart
**/
add_action('woocommerce_cart_collaterals', 'order_comments_custom_cart_field');
function order_comments_custom_cart_field() {
echo '<div id="cart_order_notes">';
?>
<div class="customer_notes_on_cart">
<label for="customer_notes_text"><?php _e('Order notes','woocommerce'); ?></label>
<textarea id="customer_notes_text"></textarea>
</div>
<?php
}
/**
* Process the checkout and overwriting the normal button
*
*/
function woocommerce_button_proceed_to_checkout() {
$checkout_url = wc_get_checkout_url();
?>
<form id="checkout_form" method="POST" action="<?php echo $checkout_url; ?>">
<input type="hidden" name="customer_notes" id="customer_notes" value="">
<a href="#" onclick="document.getElementById('customer_notes').value=document.getElementById('customer_notes_text').value;document.getElementById('checkout_form').submit()" class="checkout-button button alt wc-forward">
<?php _e( 'Proceed to checkout', 'woocommerce' ); ?></a>
</form>
<?php
}
// getting the values in checkout again
add_action('woocommerce_checkout_before_customer_details',function(){
?>
<script>
jQuery( document ).ready(function() {
jQuery('#order_comments' ).val("<?php echo sanitize_text_field($_POST['customer_notes']); ?>");
});
</script>
<?php
});

Related

Custom form fields in cart and get the data in checkout on Woocommerce

Adding From,To and Message fields in cart page before checkout.I have added some code in cart.php file but after adding that code the cart page is displaying blank.
/**
* Add the order_comments field to the cart
**/
add_action('woocommerce_cart_collaterals',
'order_comments_custom_cart_field');
function order_comments_custom_cart_field() {
echo '<div id="cart_order_notes">';
?>
<div class="customer_notes_on_cart">
<label for="customer_notes_text"><?php _e('Order notes','woocommerce'); ?>
</label>
<textarea id="customer_notes_text"></textarea>
</div>
<?php
}
/**
* Process the checkout and overwriting the normal button
*
*/
function woocommerce_button_proceed_to_checkout() {
$checkout_url = wc_get_checkout_url();
?>
<form id="checkout_form" method="POST" action="<?php echo $checkout_url;
?>">
<input type="hidden" name="customer_notes" id="customer_notes" value="">
<a href="#" onclick="document.getElementById('customer_notes').value=document.getElementById('customer_notes_text').value;document.getElementById('checkout_form').submit()" class="checkout-button button alt wc-forward">
<?php _e( 'Proceed to checkout', 'woocommerce' ); ?></a>
</form>
<?php
}
// getting the values in checkout again
add_action('woocommerce_checkout_before_customer_details',function(){
?>
<script>
jQuery( document ).ready(function() {
jQuery('#order_comments' ).val("<?php echo
sanitize_text_field($_POST['customer_notes']); ?>");
});
</script>
<?php
});
In cart.php i have added this code at the bottom before closing the form tag as well as after the form tag.But i am getting a blank page after adding this piece of code in cart.php.
In the same format i am trying to get those from,to and message fields.
The following code will post from a custom textarea field in cart page the imputed text value to checkout order notes field:
// Add the order_comments field to the cart
add_action( 'woocommerce_cart_collaterals', 'order_comments_custom_cart_field' );
function order_comments_custom_cart_field() {
?>
<div class="customer_notes_on_cart" style="clear:both;">
<label for="customer_notes_text"><?php _e("Order notes", "woocommerce") ?></label>
<textarea id="customer_notes_text"></textarea></div>
<?php
}
// Process the checkout and overwriting the normal button
add_action( 'woocommerce_proceed_to_checkout', 'change_proceed_to_checkout', 15 );
function change_proceed_to_checkout() {
remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
?>
<form id="checkout_form" method="POST" action="<?php echo wc_get_checkout_url(); ?>">
<input type="hidden" name="customer_notes" id="customer_notes" value="">
<button type="submit" class="checkout-button button alt wc-forward" style="width:100%;"><?php
esc_html_e( 'Proceed to checkout', 'woocommerce' ) ?></button>
</form>
<?php
}
// Jquery script for cart and checkout pages
add_action('wp_footer', 'customer_notes_jquery' );
function customer_notes_jquery() {
?>
<script>
jQuery(function($) {
<?php // For cart
if( is_cart() ) : ?>
$('#customer_notes_text').on( 'blur', function(){
$('#customer_notes').val($(this).val());
});
<?php // For checkout
elseif( is_checkout() && ! is_wc_endpoint_url() ) : ?>
$('#order_comments' ).val("<?php echo sanitize_text_field($_POST['customer_notes']); ?>");
<?php endif; ?>
});
</script>
<?php
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
I think is better no change "proceed to checkout" form, is better to store vars in localstorage when data in field changed, and get it after, when user is in checkout form.
function order_comments_custom_cart_field() {
echo '<div id="cart_order_notes">';
?>
<div class="customer_notes_on_cart">
<label for="customer_notes_text"><?php _e('Order notes','woocommerce'); ?>
</label>
<textarea id="customer_notes_text"></textarea>
</div>
<script>
jQuery(document).ready(function (jQuery) {
jQuery("#customer_notes_text").on('change', function () {
localStorage.setItem(jQuery(this).attr('id'), this.val());
});
});
</script>
<?php
}
Then you can get it with
LocalStore.getItem(item);
Don't forget to destroy element after obtain it, with
LocalStorage.removeItem(item);

How to enable/disable custom meta-box in WordPress?

I want to enable and disable custom meta-box on click of a button in my plugin. This button will be on the settings page of the plugin. This is what I've done so far:
function wpplugin_settings_page_markup()
{
if(!current_user_can('manage_options'))
{
return;
}
?>
<div class="wrap">
<h1><?php esc_html_e(get_admin_page_title()); ?></h1>
<?php
?>
<form method="post" action="">
<input type="checkbox" id="postcb" name="postcheck" value="Post">
<label id='postcbid' name="labelpostcheck" for='postcb'>
Post
</label>
<input type="submit" value="submit" name="submit_btn">
</form>
</div>
<?php
function cd_meta_box_add()
{
$multi_posts=array('post', 'page');
foreach ($multi_posts as $multi_post) {
add_meta_box(
'my-meta-box-id', //id
'Custom Meta Box', //title
'cd_meta_box_cb', //callback
$multi_post, //post type
'normal', //position
'high' //priority
);
}
}
add_action('add_meta_boxes', 'cd_meta_box_add');
function cd_meta_box_cb()
{
echo'<b> This is Custom meta box </b>';
}
}
This code does not display the meta-box but I want the meta-box to be added only when that checkbox is checked, and removed when checkbox is not checked. Can anyone please help me achieve that? Thanks!

Remember HTML dropdown value if registration page reloads

I have a WordPress page where the default login/signup page is disabled and I let users register via a WooCommerce login/signup page with a bunch of custom fields added through my theme's functions.php.
One of those fields is a dropdown field that lets users select their country.
If a user misses something in the registration form and the page reloads (with an error message telling the user what was missing), the text fields are pre-populated with their previous text. The dropdown menu, however, reverts back to its neutral option instead of remembering its previous value.
My code looks like this:
<?php add_filter( 'woocommerce_register_form', 'adding_custom_registration_fields' );
function adding_custom_registration_fields($fields) {
$countries_obj = new WC_Countries();
$countries = $countries_obj->__get('countries'); ?>
<div class="form-row form-row-wide">
<label for="reg_billing_country"><?php _e( 'Country', 'woocommerce' ); ?> <span class="required">*</span></label>
<select class="country_select sk-required" data-sk-tooltip="Required" name="billing_country" id="reg_billing_country">
<option value=""> <?php _e('Select country','my-theme'); ?></option>
<?php foreach ($countries as $key => $value): ?>
<option value="<?php echo $key?>"><?php echo $value?></option>
<?php endforeach; ?>
</select>
</div>
I want the dropdown field to remember its value if the page reloads, just like the text fields do. Can I use html to encourage the browser to cache the value?
I tried
var selectedItem = sessionStorage.getItem("country_select_session");
$('#reg_billing_country').val(selectedItem);
$('#reg_billing_country').change(function() {
var dropVal = $(this).val();
sessionStorage.setItem("country_select_session", dropVal);
});
However, the value is only retained if I manually reload the page using F5. If the page reloads by clicking the "submit" button with an incomplete form, the dropdown menu reverts back to its default value, ignoring the script
I got it to work - turns out the script was not placed properly and thus did not get executed. Thanks to #mplungjan for pointing it out.
I added the script mentioned above right before the </body> tag with this code in my functions.php:
add_action( 'wp_footer', function () { ?>
<script language="javascript" type="text/javascript">
var selectedItem = sessionStorage.getItem("country_select_session");
$('#reg_billing_country').val(selectedItem);
$('#reg_billing_country').change(function() {
var dropVal = $(this).val();
sessionStorage.setItem("country_select_session", dropVal);
});
</script>
<?php } );
With the wp_footer action hook, the script got placed and executed properly.

How to add a custom empty cart button on configurable product page:Magento

I have a configurable product with two option and i want to add an empty cart button on that page to empty the items:
i have copied the button from cart page and embed it but its not worked.
please suggest a way to add a button.
<button type="submit" name="update_cart_action" value="empty_cart" title="<?php echo $this->__('Empty Cart'); ?>" class="button2 btn-empty" id="empty_cart_button"><span><span><?php echo $this->__('Empty Cart'); ?></span></span></button>
You will need to create a new controller and then add the action of the controller to the button you just created.
Then create a model to empty the $quote which is the current items. Thats the basic theory.
Inchoo created something similar to what you want check it out here.
If you look at the updatePostAction function of Mage_Checkout_CartController, you should indeed be able to do that the way you want it. But right now your button is not linked to a form, so it does nothing.
But you will also need a valid form key for that to work.
Extract of Mage_Checkout_CartController :
public function updatePostAction()
{
if (!$this->_validateFormKey()) {
$this->_redirect('*/*/');
return;
}
$updateAction = (string)$this->getRequest()->getParam('update_cart_action');
switch ($updateAction) {
case 'empty_cart':
$this->_emptyShoppingCart();
break;
case 'update_qty':
$this->_updateShoppingCart();
break;
default:
$this->_updateShoppingCart();
}
$this->_goBack();
}
So this should be something working :
<form action="<?php echo $this->getUrl('checkout/cart/updatePost') ?>" method="post">
<?php echo $this->getBlockHtml('formkey'); ?>
<button type="submit" name="update_cart_action" value="empty_cart" title="<?php echo $this->__('Empty Cart'); ?>" class="button2 btn-empty" id="empty_cart_button"><span><span><?php echo $this->__('Empty Cart'); ?></span></span></button>
</form>

PHP function to output a form value?

I'm using this piece of code to add a form to a Wordpress taxonomy:
function albums_taxonomy_add_new_meta_field() {
// this will add the custom meta field to the add new term page
?>
<div class="form-field">
<label for="term_meta[custom_term_meta]"><?php _e( 'Example meta field', 'albums' ); ?></label>
<input type="text" name="term_meta[custom_term_meta]" id="term_meta[custom_term_meta]" value="">
<p class="description"><?php _e( 'Enter a value for this field','albums' ); ?></p>
</div>
<?php
}
add_action( 'albums_add_form_fields', 'albums_taxonomy_add_new_meta_field', 10, 2 );
The value is saving fine. However, how can I output on my template the value user filled out in the form ? What's the php function to use this value in front-end ?
Thanks.
Just taking a guess, on the page where the form is being redirected to try:
echo $_POST['term_meta']['custom_term_meta']

Categories