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.
Related
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.
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>
I'm creating my first options page and i'm trying to upload images for a slider, I don't know how many images are going to be in the slider so I will need to add more with a + button, It will automatically show one text input an upload button and a + button to start off with, should I require more i'll click the + button which will then add another text input, upload button a + button and a - button.
I've got this working to a point, it still needs a little help but it's getting there http://jsfiddle.net/vs8p5/5/
So far if I upload an image and click the + button, it will give me the option to upload another, this is working great and the images upload to wordpress.
Now for the issue.
I'm using this part of code to retrieve the data from wordpress
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) {
echo "<script type='text/javascript'>alert('yes it has');</script>";
}
This tells me that if there is a value then echo the alert
Now, working form that code above I've
function kandibox_hero_upload_image_link_callback($args) {
$hero_options = get_option( 'hero_options' ); ?>
<div id="upload_image_sets"> <?php
$hero_options = get_option ( 'hero_options' );
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) { ?>
<div id="clonedInput1" class="clonedInput">
<input id="upload_image_link_1" type="text" size="36" name="hero_options[upload_image_link_1]" value="<?php echo $hero_options['upload_image_link_1']; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div> <?php
}
else { ?>
<div id="clonedInput1" class="clonedInput">
<input id="upload_image_link_1" type="text" size="36" name="hero_options[upload_image_link_1]" value="<?php echo $hero_options['upload_image_link_1']; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div>
<?php } ?>
</div> <?php
}
What i'm trying to do here is echo a form with a value, if nothing exists, echo a blank form, this seems to work although I don't think it's correct.
The main issue is that it will only echo the first form, when you click the + buttons, the form's id, name, and value script increments.
So now the question.
How can I get the form to look for all the $hero_options['upload_image_link_...'] and echo out a form for each if it exists? then echo out 1 blank form if nothing exists?
I'm pretty sure i've covered everything you might need but if i've missed something, let me know and i'll add it.
I've followed about 20 tutorials old and new and come up with this.
To add the information to the wordpress database, i'm using the following code.
function register_hero_options() {
add_settings_section(
'hero_settings_section', // ID used to identify this section and with which to register options
__( 'hero Options', 'kandibox' ), // Title to be displayed on the administration page
'kandibox_hero_options_callback', // Callback used to render the description of the section
'hero_options' // Page on which to add this section of options
);
add_settings_field(
'show_hero_options', // ID used to identify the field throughout the theme
__( 'hero', 'kandibox' ), // The label to the left of the option interface element
'kandibox_toggle_hero_callback', // The name of the function responsible for rendering the option interface
'hero_options', // The page on which this option will be displayed
'hero_settings_section' // The name of the section to which this field belongs
);
$hero_options = get_option ( 'hero_options' );
if( isset( $hero_options['show_hero_options'] ) && $hero_options[ 'show_hero_options' ] ) {
add_settings_field(
'hero_size',
__( 'Size', 'kandibox' ),
'kandibox_hero_size_callback',
'hero_options',
'hero_settings_section'
);
add_settings_field(
'hero_background',
__( 'Background', 'kandibox' ),
'kandibox_hero_background_callback',
'hero_options',
'hero_settings_section'
);
add_settings_field(
'upload_image_links',
__( 'Upload Image', 'kandibox' ),
'kandibox_hero_upload_image_link_callback',
'hero_options',
'hero_settings_section'
);
}
register_setting(
'hero_options',
'hero_options'
);
}
add_action( 'admin_init', 'register_hero_options' );
$hero_options = get_option( 'hero_options' );
$count=count($hero_options);
$totalimg=$count-4; ?>
<div id="upload_image_sets"> <?php
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) {
for($i=1;$i<=$totalimg;$i++){ ?>
<div id="clonedInput1" class="clonedInput">
<input type="text" size="36" name="hero_options[upload_image_link_<?=$i ?>]" value="<?php echo $hero_options['upload_image_link_'.$i]; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div> <?php
}}
?>
ok i juss wrote a rough code for you might this will help you. this is according to the array you have go. there might be some better ways of doing it.
$hero_options = get_option( 'hero_options' );
$count=count($hero_options);
$totalimg=$count-4; ?>
<div id="upload_image_sets"> <?php
if( isset( $hero_options['upload_image_link_1'] ) && $hero_options[ 'upload_image_link_1' ] ) {
for($i=1;$i<=$totalimg;$i++){ ?>
<div id="clonedInput1" class="clonedInput">
<input type="text" size="36" name="hero_options[upload_image_link_<?=$i ?>]" value="<?php echo $hero_options['upload_image_link_'.$i]; ?>" />
<input id="show_upload_image_link_button_1" class="button upload_images" type="button" value="Upload Image" />
<div class="actions">
<button class="clone">Clone</button>
<button class="remove">Remove</button>
</div>
</div> <?php
}}
?>
</div> <?php
Updated the code so now it's correct.
I was using the code below as a Plugin to create a "Options Menu Page" for wordpress:
add_action('admin_init', 'cardin_options_init' );
add_action('admin_menu', 'cardin_options_add_page');
function cardin_options_init(){
register_setting( 'cardin_options_options', 'cardin_options');
}
function cardin_options_add_page() {
add_options_page('Cardin Options', 'Cardin Options', 'manage_options', 'cardin_options', 'cardin_options_do_page');
}
function cardin_options_do_page() {
?>
<div class="wrap">
<div id="icon-options-general" class="icon32"><br></div>
<h2>Cardin Options</h2>
<form method="post" action="options.php">
<?php settings_fields('cardin_options_options'); ?>
<?php $options = get_option('cardin_options'); ?>
<table class="form-table">
<tr valign="top"><th scope="row">Information</th>
<td><input type="text" name="cardin_options[information]" value="<?php echo $options['information']; ?>" /></td>
</tr>
</table>
<input type="submit" class="button-primary" name="submit" value="<?php _e('Save Changes') ?>" />
</form>
</div>
<?php
}
It's working how it should, but, later I decided to create my Own Plugin Menu Page, not a 'Settings' sub-menu, then, I changed the line below:
add_options_page('Cardin Options', 'Cardin Options', 'manage_options', 'cardin_options', 'cardin_options_do_page');
to:
add_menu_page('Cardin Options', 'Cardin Options', 'manage_options', 'cardin_options', 'cardin_options_do_page');
It worked, but when i click "Save Changes" the "Settings Updated" message doesn't display anymore. What should I do to make it display again?
Thanks alot in advance and sorry for the bad english.
<?php if($_POST['oscimp_hidden'] == 'Y') { ?>
<div id="message" class="updated">
<p><strong><?php _e('Settings saved.') ?></strong></p>
</div>
edit:
heres a guide: http://net.tutsplus.com/tutorials/wordpress/creating-a-custom-wordpress-plugin-from-scratch/
search for: if($_POST['oscimp_hidden'] == 'Y') {
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']