Created
March 17, 2025 08:53
-
-
Save plugin-republic/e6eb8e8903eba2c4e94dbd35435140a1 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 | |
add_action( 'wp_footer', function() { | |
?> | |
<style> | |
.pewc-has-extra-fields .pewc-item-products-radio-list[data-product-quantity-select="1"] .pewc-radio-wrapper .pewc-radio-list-label-wrapper { | |
display: none !important; | |
} | |
.pewc-has-extra-fields .pewc-item-products-radio-list[data-product-quantity-select="1"] .pewc-child-quantity-field-select { | |
width: 50px !important; | |
margin-left: auto; | |
} | |
.pewc-has-extra-fields .pewc-item-products-radio-list[data-product-quantity-select="1"] .pewc-radio-list-desc-wrapper { | |
padding-left: 0 !important; | |
} | |
</style> | |
<script> | |
jQuery(document).ready(function($) { | |
$('.pewc-item').change(function(e) { | |
pewc_update_quantity_dropdowns(); | |
}); | |
function pewc_update_quantity_dropdowns() { | |
let groups_fields = []; | |
let quantity_dropdowns_total = 0; | |
$('.pewc-group-wrap:not(.pewc-form-field-clone) .pewc-item[data-product-quantity-select="1"]').each( function(i, el ) { | |
let group_id = $(el).closest('.pewc-group-wrap').attr('data-group-id'); | |
if ( ! groups_fields[group_id] ) { | |
groups_fields[group_id] = []; | |
} | |
groups_fields[group_id].push( $(el) ); | |
quantity_dropdowns_total++; | |
}); | |
if ( quantity_dropdowns_total ) { | |
let min; | |
let max; | |
let select_placeholder = '0'; | |
groups_fields.forEach( function( fields, group_id ) { | |
fields.forEach( function( field ) { | |
let $qty_field = field.find('.pewc-child-quantity-field.pewc-independent-quantity-field'); | |
$qty_field.hide(); | |
let dropdown_field = 'pewc-child-quantity-field-select-' + field.attr('data-field-id'); | |
let $select = $('<select class="pewc-child-quantity-field-select ' + dropdown_field +' pewc-independent-quantity-field"></select>'); | |
min = field.attr('data-min-products-select') != '' ? parseInt( field.attr('data-min-products-select') ) : 1; | |
max = field.attr('data-max-products-select') != '' ? parseInt( field.attr('data-max-products-select') ) : 2; | |
$select.append( '<option value="0">'+ select_placeholder +'</option>' ); | |
for ( let i = min; i <= max; i++ ) { | |
$select.append( '<option value="' + i + '">' + i + '</option>' ); | |
} | |
if ( field.find('.'+dropdown_field).length == 0 ) { | |
field.find('.pewc-radio-list-desc-wrapper').after( $select ); | |
} | |
}); | |
}); | |
$('.pewc-child-quantity-field-select').change( function( e ) { | |
let disable_items = $(this).closest('.pewc-item').attr('data-product-disable-items') == '1' ? true : false; | |
let qty = $(this).val(); | |
$(this).closest('.pewc-item').find('.pewc-child-quantity-field').val( qty ); | |
if ( ! disable_items ) { | |
return; | |
} | |
// $(this).closest('.pewc-group-wrap').find('.pewc-child-quantity-field-select').prop('disabled', (qty > 0) ); | |
$(this).closest('.pewc-radio-list-wrapper').find('.pewc-child-quantity-field-select').prop('disabled', (qty > 0) ); | |
$(this).prop('disabled', false); | |
// $(this).closest('.pewc-group-wrap').find('.pewc-radio-form-field').prop('checked', false); | |
$(this).closest('.pewc-radio-list-wrapper').find('.pewc-radio-form-field').prop('checked', false); | |
$(this).parent().find('.pewc-radio-form-field').prop('checked', (qty > 0) ); | |
}); | |
$('.pewc-radio-form-field').change( function(e) { | |
let select = $(this).closest('.pewc-radio-wrapper').find('.pewc-child-quantity-field-select'); | |
let qty = select.val(); | |
$(this).closest('.pewc-group-wrap').find('.pewc-radio-form-field').prop('checked', false); | |
$(this).prop('checked', true); | |
$(this).closest('.pewc-item').find('.pewc-child-quantity-field').val( qty ); | |
}); | |
} | |
} | |
pewc_update_quantity_dropdowns(); | |
}); | |
</script> | |
<?php | |
} ); | |
add_filter( 'pewc_filter_item_attributes', function( $attributes, $item ) { | |
$quantity_dropdown = apply_filters( 'product_quantity_select', false, $item ); | |
if ( ! $quantity_dropdown ) { | |
return $attributes; | |
} | |
$attributes['data-product-quantity-select'] = $quantity_dropdown; | |
$attributes['data-product-disable-items'] = apply_filters( 'product_quantity_disable_items', true, $item ); | |
$attributes['data-min-products-select'] = apply_filters( 'min_products_select', 1, $item ); | |
$attributes['data-max-products-select'] = apply_filters( 'max_products_select', 2, $item ); | |
return $attributes; | |
}, 10, 2 ); | |
add_filter( 'product_quantity_select', function( $quantity_dropdown, $item ) { | |
if ( $item['field_type'] == 'products' && $item['products_layout'] == 'radio-list' && $item['products_quantities'] == 'independent' ) { | |
$quantity_dropdown = true; | |
} | |
return $quantity_dropdown; | |
}, 10, 2 ); | |
add_filter( 'product_quantity_disable_items', function( $quantity_dropdown_disable_items, $item ) { | |
// $quantity_dropdown_disable_items = false; | |
return $quantity_dropdown_disable_items; | |
}, 10, 2 ); | |
add_filter( 'min_products_select', function( $min_products, $item ) { | |
// $min_products = 2; | |
return $min_products; | |
}, 10, 2 ); | |
add_filter( 'max_products_select', function( $max_products, $item ) { | |
// $max_products = 5; | |
return $max_products; | |
}, 10, 2 ); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment