-
Notifications
You must be signed in to change notification settings - Fork 64
/
adds-stock-status-to-the-dropdown-on-product-pages-in-woocommerce.code-snippets.xml
42 lines (42 loc) · 2.15 KB
/
adds-stock-status-to-the-dropdown-on-product-pages-in-woocommerce.code-snippets.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<?xml version="1.0" encoding="UTF-8"?>
<!-- This is a code snippets export file generated by the Code Snippets WordPress plugin. -->
<!-- https://wordpress.org/plugins/code-snippets -->
<!-- To import these snippets a WordPress site follow these steps: -->
<!-- 1. Log in to that site as an administrator. -->
<!-- 2. Install the Code Snippets plugin using the directions provided at the above link. -->
<!-- 3. Go to 'Tools: Import' in the WordPress admin panel. -->
<!-- 4. Click on the "Code Snippets" importer in the list -->
<!-- 5. Upload this file using the form provided on that page. -->
<!-- 6. Code Snippets will then import all of the snippets and associated information contained in this file into your site. -->
<!-- 7. You will then have to visit the 'Snippets: All Snippets' admin menu and activate desired snippets. -->
<!-- generator="Code Snippets/2.9.4" created="2017-10-11 19:30" -->
<snippets>
<snippet scope="0">
<name>Adds stock status to the dropdown on product pages in WooCommerce</name>
<desc></desc>
<tags>stock, status, dropdown, woocommerce</tags>
<code>add_action( 'woocommerce_after_add_to_cart_form', 'dropdown_waitlist_label' );
function dropdown_waitlist_label() {
echo "
<script>
jQuery(document).ready(function($) {
var variation_data = $('form.variations_form').attr('data-product_variations');
var variation_data = JSON.parse(variation_data);
$('#pa_size > option').each(function() {
for (var i = 0; i < variation_data.length; i++) {
var variation = variation_data[i];
if ($(this).val() == variation.attributes.attribute_pa_size) {
if ( false == variation.is_in_stock ) {
$(this).text( variation.attributes.attribute_pa_size + '\u00A0\u00A0\u00A0–\u00A0\u00A0\u00A0\u00A0Out of Stock');
}
if ( variation.min_qty == 1 && variation.max_qty == 1 ) {
$(this).text( variation.attributes.attribute_pa_size + '\u00A0\u00A0\u00A0–\u00A0\u00A0\u00A0\u00A01 left in stock' );
}
}
}
});
});
</script>";
}</code>
</snippet>
</snippets>