On my website the checkout page containing "Have a coupon? Click here to enter your code" box. I don't want this field on checkout page.I applying coupon via URl. I want to remove this box from checkout page by CSS. I attached the screenshot of that box in my checkout page.
Is this possible?
I have already tried this:
function hide_coupon_field_on_checkout( $enabled ) {
if ( is_checkout() ) {
$enabled = false;
}
return $enabled;
}
add_filter( 'woocommerce_coupons_enabled', 'hide_coupon_field_on_checkout' );
But it disable coupons functionality in checkout page.
Any help will be appreciated.
You can use a custom function hooked in woocommerce_before_checkout_form action hook that will remove the notice coupon form without disabling coupon functionality in checkout page:
add_action( 'woocommerce_before_checkout_form', 'remove_checkout_coupon_form', 9 );
function remove_checkout_coupon_form(){
remove_action( 'woocommerce_before_checkout_form', 'woocommerce_checkout_coupon_form', 10 );
}
Code goes in function.php file of the active child theme (or active theme).
Tested and works.
This tag seems not having an unique id/class assigned. But something like this should be able to do it:
$(".showcoupon").closest(".woocommerce-info").hide();
You can override the coupon template file at wp-content/themes/yourtheme/woocommerce/checkout/form-coupon.php and comment wc_print_notice and set display of the form to none.
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
if ( ! wc_coupons_enabled() ) {
return;
}
if ( empty( WC()->cart->applied_coupons ) ) {
$info_message = apply_filters( 'woocommerce_checkout_coupon_message', __( 'Have a coupon?', 'woocommerce' ) . ' ' . __( 'Click here to enter your code', 'woocommerce' ) . '' );
//wc_print_notice( $info_message, 'notice' );
}
?>
<form class="checkout_coupon" method="post" style="display:none">
<p class="form-row form-row-first">
<input type="text" name="coupon_code" class="input-text" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" />
</p>
<p class="form-row form-row-last">
<input type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>" />
</p>
<div class="clear"></div>
</form>
Related
The bounty expires in 12 hours. Answers to this question are eligible for a +50 reputation bounty.
Nik7 is looking for an answer from a reputable source.
I try to auto-expand the coupon field on the WooCommerce checkout page. By default, the customer has to click "Do you have a coupon? Click here!". But we would like to have that field always visible. I tried with js but it is not working. However, I would like to have a pure PHP approach if this is possible.
add_action( 'wp_footer', 'woocommerce_show_coupon', 99 );
function woocommerce_show_coupon() {
echo '
<script type="text/javascript">
jQuery(document).ready(function($) {
$(\'.checkout_coupon\').show();
});
</script>
';
}
Does someone have an idea how I can do that in a smart way?
Cheers
//Adding CSS inline style to an existing CSS stylesheet
function mujuonly_add_inline_css() {
$mustlogin_custom_css = "
.woocommerce-form-coupon {
display:block !important;
}
";
//Add the above custom CSS via wp_add_inline_style
wp_add_inline_style( 'woocommerce-inline', $mustlogin_custom_css ); //Pass the variable into the main style sheet ID
}
add_action( 'wp_enqueue_scripts', 'mujuonly_add_inline_css' ); //Enqueue the CSS style
You can overridden by copying it to yourtheme/woocommerce/checkout/form-coupon.php the code below:
if ( ! wc_coupons_enabled() ) { // #codingStandardsIgnoreLine.
return;
}
?>
<form class="checkout_coupon woocommerce-form-coupon" method="post" style="display:block">
<p><?php esc_html_e( 'If you have a coupon code, please apply it below.', 'woocommerce' ); ?></p>
<p class="form-row form-row-first">
<label for="coupon_code" class="screen-reader-text"><?php esc_html_e( 'Coupon:', 'woocommerce' ); ?></label>
<input type="text" name="coupon_code" class="input-text" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" />
</p>
<p class="form-row form-row-last">
<button type="submit" class="button<?php echo esc_attr( wc_wp_theme_get_element_class_name( 'button' ) ? ' ' . wc_wp_theme_get_element_class_name( 'button' ) : '' ); ?>" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>"><?php esc_html_e( 'Apply coupon', 'woocommerce' ); ?></button>
</p>
<div class="clear"></div>
</form>
add_filter( 'woocommerce_coupons_enabled', '__return_true' );
add_filter( 'woocommerce_checkout_coupon_message', '__return_false' );
add_action( 'wp_head', 'expand_checkout_coupon_field' );
function expand_checkout_coupon_field() {
echo '<style>.checkout_coupon { display: block !important; }</style>';
}
One way to achieve this is to override the form-coupon.php template file that's responsible for rendering the coupon field on the checkout page. Here's how you can do it:
Create a new folder named woocommerce in your theme directory if it doesn't already exist.
Copy the form-coupon.php file from the wp-content/plugins/woocommerce/templates/checkout directory to the woocommerc directory in your theme.
Edit the form-coupon.php file and replace the code inside the div with the checkout_coupon class with the following code:
<div class="checkout_coupon">
<?php wc_print_notice( apply_filters( 'woocommerce_checkout_coupon_message', esc_html__( 'Have a coupon?', 'woocommerce' ) . ' ' . esc_html__( 'Click here to enter your code', 'woocommerce' ) . '' ), 'notice' ); ?>
<form class="checkout_coupon_form" method="post" style="display:block">
<p class="form-row form-row-first">
<input type="text" name="coupon_code" class="input-text" placeholder="<?php esc_attr_e( 'Coupon code', 'woocommerce' ); ?>" id="coupon_code" value="" />
</p>
<p class="form-row form-row-last">
<button type="submit" class="button" name="apply_coupon" value="<?php esc_attr_e( 'Apply coupon', 'woocommerce' ); ?>"><?php esc_html_e( 'Apply coupon', 'woocommerce' ); ?></button>
</p>
<div class="clear"></div>
</form>
</div>
This will show the coupon field by default on the checkout page without requiring the customer to click the "Do you have a coupon? Click here!" link.
Keep in mind that modifying the template files directly may be overwritten if WooCommerce is updated, so it's a good practice to create a child theme and modify the template file there instead.
I'm trying add/remove Wordpress search filter according to the page author of the page I'm currently on. The search bar is in the theme header and therefore independent of the page I am on. I would like to use the author ID of the current page to add a filter for the next search I would do in the search bar.
This is an e-commerce site. The search bar is in the header (independent of the page). I'm on the shop page. I'd like to search products (i.e. posts with the same author ID) related to the shop page I was on before the search action.
I have written the code for functions.php however it doesn't work, i.e "my_search_filter" is not added (nor deleted). What I'm missing?
function search_filter_by_page_author()
{
$author_id = get_the_author_meta('ID');
#echo $author_id;
if (in_array($author_id, array("1", "2"))
{
add_filter( 'pre_get_posts', 'my_search_filter' );
}
else
{
remove_filter( 'pre_get_posts', 'my_search_filter' );
}
}
add_action( 'wp_head', 'search_filter_by_page_author' );
function my_search_filter( $query )
{
if ( $query->is_search && !is_admin())
{
$query->set( 'author', '1, 2' );
}
return $query;
}
Here is an answer that may be what you're after. In my opinion, you should
Set a new searchform.php
Add your pre_get_posts (which by the way is an action, and not a filter)
searchform.php (example) - This goes in your theme or child theme root folder.
<?php
global $post;
$author_id = $post->post_author;?>
<form method="get" id="searchform" action="<?php echo esc_url( home_url( '/' ) ); ?>" role="search" class="searchform search">
<div class="form-group">
<input type="text" class="form-control" name="s" value="<?php echo esc_attr( get_search_query() ); ?>" id="s" placeholder="<?php esc_attr_e( 'Search …', 'text_domain' ); ?>"/>
<input type="hidden" name="author" value="<?php echo $author_id;?>">
</div>
<button type="submit" class="btn btn-default"><i class="fa fa-search"></i></button>
</form>
Search filter (put in functions.php)
add_action('pre_get_posts', 'search_filter_by_page_author');
function search_filter_by_page_author($query){
if ($query->is_search && !is_admin() && isset($_GET['author'])) {
$query->set('author', $_GET['author']);
}
return $query;
}
This may not do exactly what you're looking for, but I think you should be able to get a positive result from this as a starting point.
hello i have this code which is supposed to remove an item from an order
add_action( 'woocommerce_order_item_meta_end', 'display_remove_order_item_button', 10, 3 );
function display_remove_order_item_button( $item_id, $item, $order ){
// Avoiding displaying buttons on email notification
if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) ) return;
if( isset($_POST["remove_item_$item_id"]) && $_POST["remove_item_$item_id"] == 'Remove this item' ){
wc_delete_order_item( $item_id );
$order->calculate_totals();
}
echo '<form class="cart" method="post" enctype="multipart/form-data" style= "margin-top:12px;">
<input type="submit" class="button" name="remove_item_'.$item_id.'" value="Complete Cancellation" />
</form>';
}
But when i press complete cancellation page refresh but nothing is removed & i refresh again nothing is removed
what am i doing wrong?
There are multiple mistakes in your code like:
the condition $_POST["remove_item_$item_id"] == 'Remove this item' is always false.
You need to remove the item using a hook before page start to load, to get the refresh. If not you will not see that the item has been removed and you will need to reload the page once.
So try the following instead:
// Displaying the form fields (buttons and hidden fields)
add_action( 'woocommerce_order_item_meta_end', 'display_remove_order_item_button', 10, 3 );
function display_remove_order_item_button( $item_id, $item, $order ){
// Avoiding displaying buttons on email notification
if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) )
return;
echo '<form class="cart item-'.$item_id.'" method="post" style= "margin-top:12px;">
<input type="hidden" name="item_id" value="'.$item_id.'" />
<input type="hidden" name="order_id" value="'.$order->get_id().'" />
<input type="submit" class="button" name="remove_item_'.$item_id.'" value="Complete Cancellation" />
</form>';
}
// Processing the request
add_action( 'template_redirect', 'process_remove_order_item' );
function process_remove_order_item(){
// Avoiding displaying buttons on email notification
if( ! ( is_wc_endpoint_url( 'view-order' ) || is_wc_endpoint_url( 'order-received' ) ) )
return;
if( isset($_POST['item_id']) && isset($_POST['remove_item_'.$_POST['item_id']]) && isset($_POST['order_id'])
&& is_numeric($_POST['order_id']) && get_post_type($_POST['order_id']) === 'shop_order' ) {
// Get the WC_Order Object
$order = wc_get_order( absint($_POST['order_id']) );
// Remove the desired order item
if( is_a($order, 'WC_Order') && is_numeric($_POST['item_id']) ) {
$order->remove_item( absint($_POST['item_id']) );
$order->calculate_totals();
// Optionally display a notice
wc_add_notice( __('Order item removed successfully'), 'success' );
}
}
}
Code goes in function.php file of your active child theme (or active theme). Tested and work.
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);
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']