Created
May 25, 2021 12:14
-
-
Save slavapas/22f2e3019da2e690f8990fb583f75c56 to your computer and use it in GitHub Desktop.
WooCommerce - Get quantity of the active selected product variation in WooCommerce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action('woocommerce_before_add_to_cart_button', 'get_selected_variation_stock', 11, 0); | |
function get_selected_variation_stock() { | |
global $product, $wpdb; | |
// HERE set your custom message | |
$message_outofstock = __('My custom "out of stock" message'); | |
// Get the visible product variations stock quantity | |
$variations_data = array(); | |
$child_ids = $product->get_visible_children(); | |
$child_ids = implode(',', $child_ids); | |
$results = $wpdb->get_results(" | |
SELECT p.ID, pm.meta_value as stock_qty | |
FROM {$wpdb->prefix}posts as p | |
INNER JOIN {$wpdb->prefix}postmeta as pm ON p.ID = pm.post_id | |
WHERE p.post_type LIKE 'product_variation' | |
AND p.ID IN ($child_ids) AND pm.meta_key LIKE '_stock' | |
"); | |
foreach ($results as $result) { | |
// Set in an indexed array for each variation ID the corresponding stock qty | |
$variations_data[$result->ID] = $result->stock_qty; | |
} | |
?> | |
<script> | |
jQuery(document).ready(function($) { | |
var vData = <?php echo json_encode($variations_data); ?>, | |
stock = '.woocommerce-variation-availability > .stock'; | |
// Function that get the selected variation stock quantity and returns it | |
function getTheStockQty(a = vData) { | |
$.each(a, function(index, value) { | |
if (index == $('input.variation_id').val()) | |
return value; | |
}); | |
} | |
// Once loaded (if a variation is selected by default) | |
setTimeout(function() { | |
var stockQty = getTheStockQty(); | |
if (0 < $('input.variation_id').val() && $(stock).hasClass('out-of-stock')) { // OUT OF STOCK | |
// Output a custom message for "out of stock" | |
$(stock).text('<?php echo $message_outofstock; ?>'); | |
// Testing output in the browser JS console | |
console.log('(1)' + $(stock).html() + ' | Stock qty: ' + stockQty); | |
} else if (0 < $('input.variation_id').val()) { // IN STOCK | |
// Testing output in the browser JS console | |
console.log('(2)' + $(stock).html() + ' | Stock qty: ' + stockQty); | |
} | |
}, 300); | |
// On live selected variation | |
$('select').blur(function() { | |
var stockQty = getTheStockQty(); | |
if (0 < $('input.variation_id').val() && $(stock).hasClass('out-of-stock')) { // OUT OF STOCK | |
// Output a custom message for "out of stock" | |
$(stock).text('<?php echo $message_outofstock; ?>'); | |
// Testing output in the browser JS console | |
console.log('(1 live)' + $(stock).html() + ' | Stock qty: ' + stockQty); | |
} else if (0 < $('input.variation_id').val()) { // IN STOCK | |
// Testing output in the browser JS console | |
console.log('(2 live)' + $(stock).html() + ' | Stock qty: ' + stockQty); | |
} | |
}); | |
}); | |
</script> | |
<?php | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment