I am trying to pass a value with jquery to the SKU input field from woocommerce that is generated dynamically when you click on the quick edit button in wp-admin/edit.php?post_type=product, but for some reason is not pasting the value the field.
function generateRandomString($length = 10) {
$characters = '0123456789ABCDEFGHIJKMNOPQRSTUVWXYZ';
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, strlen($characters) - 1)];
}
return $randomString;
}
function quick_edit_sku($product) {
$string = generateRandomString(); // outputs a random string correctly
?>
<script>
jQuery(document).ready(function($) {
$('.editinline').on('click', function(event) {
var sku_val = $("input[name=_sku]").val(); // checks if there is a value
var random = '<?php echo $string ?>';
if (sku_val === '') { // if SKU field is empty than apply random string. Though this doesn't work quite well, only on the second time I click quick edit it returned correct
console.log(random);
$("input[name=_sku]").val(random);
} else {
console.log('Already has value SKU value');
}
});
});
</script>
<?php }
add_filter( 'admin_footer', 'quick_edit_sku' );
Button + Firebug http://postimg.org/image/oqdpkhqov/
$("input[name=_sku]").html(random);instead of$("input[name=_sku]").val(random);? in your if condition