I'm still learning jQuery.
I have this form:
<form class="simple-checkout" enctype="multipart/form-data" method="POST" action="<?php echo admin_url('admin-ajax.php'); ?>">
<input type="hidden" name="add-to-cart" value="<?php echo htmlspecialchars($getid); ?>"/>
<input type="number" name="quantity" min="1" max=<?= $available_stock ?> value="1"/>
<input type="hidden" id="thwepof_product_fields" name="thwepof_product_fields" value="order_date">
<input type="hidden" id="order_date" name="order_date" value="<?php echo htmlspecialchars($get_day); ?>">
<input type="hidden" name="action" value="create_ajax_checkout"/>
<input type="submit" value="Add to cart"/>
</form>
I add this to this function:
add_action('wp_ajax_create_ajax_checkout', 'create_ajax_checkout');
function create_ajax_checkout()
{
wp_send_json_success($_POST);
}
And use this js file to handle the Ajax request:
jQuery(document).ready(function ($) {
$('.simple-checkout').ajaxForm({
success: function (response) {
console.log(response);
//alert("Success");
},
error: function (response) {
console.log(response);
alert("Error!");
}
});
});
My question is that the $_POST['quantity']
has a max=<?= $available_stock ?>
. I would like to reduce that default max value by the quantity of $_POST['quantity']
.
So if the default quantity is 30 and you add 2 pieces to your cart then I would like to set the maximum available value to 28.
I'm still learning jQuery.
I have this form:
<form class="simple-checkout" enctype="multipart/form-data" method="POST" action="<?php echo admin_url('admin-ajax.php'); ?>">
<input type="hidden" name="add-to-cart" value="<?php echo htmlspecialchars($getid); ?>"/>
<input type="number" name="quantity" min="1" max=<?= $available_stock ?> value="1"/>
<input type="hidden" id="thwepof_product_fields" name="thwepof_product_fields" value="order_date">
<input type="hidden" id="order_date" name="order_date" value="<?php echo htmlspecialchars($get_day); ?>">
<input type="hidden" name="action" value="create_ajax_checkout"/>
<input type="submit" value="Add to cart"/>
</form>
I add this to this function:
add_action('wp_ajax_create_ajax_checkout', 'create_ajax_checkout');
function create_ajax_checkout()
{
wp_send_json_success($_POST);
}
And use this js file to handle the Ajax request:
jQuery(document).ready(function ($) {
$('.simple-checkout').ajaxForm({
success: function (response) {
console.log(response);
//alert("Success");
},
error: function (response) {
console.log(response);
alert("Error!");
}
});
});
My question is that the $_POST['quantity']
has a max=<?= $available_stock ?>
. I would like to reduce that default max value by the quantity of $_POST['quantity']
.
So if the default quantity is 30 and you add 2 pieces to your cart then I would like to set the maximum available value to 28.
Share Improve this question edited Feb 8, 2018 at 9:54 Syscall 19.8k10 gold badges43 silver badges59 bronze badges asked Feb 8, 2018 at 9:34 DonSiteDonSite 732 silver badges7 bronze badges3 Answers
Reset to default 4Give your field an ID (for example "myField") and then change the attribute value like this:
$('#myField').attr('max', '28');
And to get the value of your field, just don't pass a second parameter:
var myFieldVar = $('#myField').attr('max');
you can use set it by following code in your success function
$("input[type='number']").attr('max',response.quantity);
To get the current value
var currentValue = $('#idOfNumberField').attr('max');
To update with new Value :
$('#idOfNumberField').attr('max', newValue);