Last active
November 24, 2016 01:29
-
-
Save pablo-sg-pacheco/248a32e35747f6814cb3 to your computer and use it in GitHub Desktop.
Shows out of stock message when a variable product has all variations out of stock
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
<?php | |
//Shows out of stock message when a variable product has all variations out of stock | |
add_filter('wp_footer', function(){ | |
if(is_product()){ | |
global $product; | |
if($product->is_type('variable')){ | |
if(!$product->is_in_stock()){ | |
?> | |
<script> | |
jQuery(window).load(function(){ | |
var variations = jQuery('.variations_form.cart').data('product_variations'); | |
var $form = jQuery('.variations_form.cart'); | |
var variation = variations[0]; | |
$form.find( 'input[name="variation_id"], input.variation_id' ) | |
.val( variation.variation_id ) | |
.change(); | |
$form.trigger( 'found_variation', [ variation ] ); | |
}); | |
</script> | |
<?php | |
} | |
} | |
} | |
},10); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment