Ask your WordPress questions! Pay money and get answers fast! (more info)

Woocommerce Coupon "Individual use only" default=checked WordPress

  • SOLVED

When adding a new coupon, I want the "Individual use only" checkbox to be checked by default:

checked="checked"

A function to add in functions.php would be best.

I found that the file class-wc-meta-box-coupon-data.php is the one generating the code.

Answers (4)

2014-10-13

zebra webdesigns answers:

Can you check the id of the checkbox and let me know.


FirstEntertainment comments:

<input id="individual_use" class="checkbox" type="checkbox" value="yes" name="individual_use">


zebra webdesigns comments:

Hello Fishentertainment

I just made a light weight plugin for you.
you can even view the code. this will only apply to the show coupon page and wont affect any other pages

[[LINK href="https://www.dropbox.com/s/p968tk0ek2ur14x/custom-coupon-checkbox.zip?dl=0"]]https://www.dropbox.com/s/p968tk0ek2ur14x/custom-coupon-checkbox.zip?dl=0[[/LINK]]


zebra webdesigns comments:

if you add the code without condition then it will be added to all the admin pages. so in my plugin I restricted it to the particular post type alone.
and whenever you dont need it you can disable the plugin. it will much effective and no complex code is involved.


zebra webdesigns comments:

if you need the exact code alone,
then try this one. and use it in your functions file.
this will only load for the shop coupon page, not to other post pages where as load-post-new.php loads to all the new post pages


function custom_coupon_checkbox(){
ob_start();
if (isset($_GET['post_type']) ){
$check_page = $_GET['post_type'];
if($check_page == "shop_coupon"){
?>
<script>
jQuery(document).ready(function(){
jQuery("#individual_use").attr("checked","checked");
});
</script>
<?php
}
}
$output = ob_get_contents();
ob_end_clean();
echo $output;
}
add_action('admin_head', 'custom_coupon_checkbox');

2014-10-13

Dbranes answers:

You can try for example:

add_action( 'load-post-new.php', function(){
add_action( 'woocommerce_coupon_data_panels', function(){
?>
<script>
jQuery( document ).ready( function () {
jQuery( "#individual_use" ).attr( 'checked', 'checked' );
});
</script>
<?php
});
});

Tested and works (see attached screenshot)


FirstEntertainment comments:

Yes this is a good idea. Please try this.. if you can give me a code that works I will award you the cash + have some more work for if you are interested =)


Dbranes comments:

I just tested it, and it seems to work.


FirstEntertainment comments:

Great. I got some code from John Cotton and made this:

add_action( 'load-post-new.php', function(){
add_action( 'woocommerce_coupon_data_panels', function(){
?>
<script>
jQuery(document).ready(function(){
jQuery("body.post-new-php input#individual_use").attr("checked","checked");
});
</script>
<?php
});
});

This makes it only check when adding new, an important aspect.


Dbranes comments:

Great, but you don't need both the "load-post-new.php" action and the ".post-new-php selector".

The <em>woocommerce_coupon_data_panels</em> hook is only fired for the editor view for the "shoup_coupon" post types in the backend.

2014-10-13

John Cotton answers:

There's no filter on that unfortunately so the only sensible way to do it would be with some javascript:


<script>
$('body.post-new-php input#individual_use').prop('checked', true);
</script>

The selectors used above mean that it will only have effect on a "create new" coupon.

You could inject that js in the admin header or - if you have a custom admin js file already, just place it in there.


FirstEntertainment comments:

Great. Good to think about making it only work on adding new. I got code to add into functions from Dbranes.

Then I added your stuff..

add_action( 'load-post-new.php', function(){
add_action( 'woocommerce_coupon_data_panels', function(){
?>
<script>
jQuery(document).ready(function(){
jQuery("body.post-new-php input#individual_use").attr("checked","checked");
});
</script>
<?php
});
});

2014-10-13

Arnav Joy answers:

can you share file "class-wc-meta-box-coupon-data.php"


FirstEntertainment comments:

That file is found in woocommerce/includes/admin/meta-boxes/

You 100% need to have woowommcerce latest version to be able to get a fix for this.


Arnav Joy comments:

create a new js file named as 'custom-admin.js' and place it in js folder of your theme

and then in this file write following



jQuery(document).ready(function($){
$('#individual_use').prop('checked', true);
})


in functions.php of the theme write following


function load_custom_wp_admin_style() {
wp_enqueue_script( 'custom-admin', get_stylesheet_directory_uri() . '/js/custom-admin.js' );

}
add_action( 'admin_enqueue_scripts', 'load_custom_wp_admin_style' );