I am having products on backorder in a woocommerce store.
I am trying to create an error message if the value of the quantity input field is a higher/exceeds the products stock - see image below.
I also want this to go away if the customer goes below current stock.
If possible I also want the error to show in the cart page as well.
This is what I got this far:
function woocommerce_stock_now() {
global $woocommerce, $product;
?>
<script>
jQuery(function ($) {
var stocknow = <?php echo $qty = $product->get_stock_quantity()(); ?>;
var qtyinput = $('[name=quantity]').val();
var errormessagestock = '<p class="errormessagestock">'(stocknow.value - qtynow.value) . ' items are on backorder and will have a little longer delivery time.</p>';
$('#qtyinput').html(this.value);
$('[name=quantity]').change(function () {
if (qtyinput.value > $stocknow) {
$('stock').html(errormessagestock);
}
});
console.log("qtynow", this.value);
});
</script>
<?php
}