Last active
June 30, 2023 05:51
-
-
Save PluginRepublicSupport/d7b051d9f0df990c8de5b72d58604fd7 to your computer and use it in GitHub Desktop.
stickermule.com interface (die cut stickers)
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 | |
add_filter( 'prefix_filter_field_option_name', function( $option_value, $key, $item, $product ) { | |
$qty_radio_field_id = 3153; // Quantity (Radio Group) field | |
if ( $item['field_id'] == $qty_radio_field_id ) { | |
$option_value .= '<span class="qty-discounted-price"> | |
</span>'; | |
$option_value .= '<span class="qty-percent-savings"> | |
</span>'; | |
} | |
return $option_value; | |
}, 10, 4 ); | |
add_action( "wp_footer", function() { | |
?> | |
<script> | |
jQuery(document).ready(function($) { | |
let qty_radio_field_id = 3153; // Quantity (Radio Group) field | |
if ( ! $('.pewc-field-'+qty_radio_field_id).length ) { | |
return; | |
} | |
let custom_qty_label = "Custom quantity"; // option label | |
// field_id property can be set so it overrides the search | |
let required_fields = [ | |
{ name: "Select a size", type: "radio", field_id: "" }, | |
{ name: "Width", type: "number", field_id: "" }, | |
{ name: "Height", type: "number", field_id: "" }, | |
{ name: "Size Selected", type: "calculation", field_id: "" }, | |
{ name: "Select a quantity", type: "radio", field_id: String(qty_radio_field_id) }, | |
{ name: "Quantity", type: "number", field_id: "" }, | |
{ name: "Quantity Selected", type: "calculation", field_id: "" } | |
]; | |
let qty_radio_field_offset = parseInt( $('.pewc-field-'+qty_radio_field_id).attr('class').split(" ").find(function(value, index, array) { | |
return value.search("pewc-field-count") != -1; | |
}).replace("pewc-field-count-", "") ); | |
let size_radio_field_offset = qty_radio_field_offset - 4; | |
$('[class*="pewc-field-count"').each(function(i) { | |
if ( i >= size_radio_field_offset && (i - size_radio_field_offset) < required_fields.length ) { | |
if ( required_fields[i - size_radio_field_offset].type == $(this).attr("data-field-type") | |
&& required_fields[i - size_radio_field_offset].field_id == "" ) { | |
required_fields[i - size_radio_field_offset].field_id = $(this).attr("data-field-id"); | |
} | |
} | |
} ); | |
// disable function if all required fields are not set | |
for ( let i = 0; i < required_fields.length; i++) { | |
if ( required_fields[i].field_id == "" ) { | |
console.log("fields are not set"); | |
return; | |
} | |
}; | |
// Size Selected (Calculation) | |
// Quantity Selected (Calculation) | |
$('.pewc-field-'+required_fields[3].field_id+', .pewc-field-'+required_fields[6].field_id).change(function() { | |
setTimeout(function() { | |
// Select a quantity (Radio Group) | |
$('.pewc-field-'+qty_radio_field_id+' input').each(function() { | |
let size = $('.pewc-field-'+required_fields[3].field_id).attr('data-field-value'); | |
let qty = $(this).attr('data-option-cost'); | |
let price, price_discount, discounted_price; | |
let is_custom_qty = false; | |
if ( $(this).attr('value') == custom_qty_label ) { | |
qty = $('.pewc-field-'+required_fields[6].field_id).attr('data-field-value'); | |
is_custom_qty = true; | |
} | |
$(this).parent().find('.qty-discounted-price').text(""); | |
$(this).parent().find('.qty-percent-savings').text(""); | |
if ( size != 0 && qty != 0 ) { | |
try { | |
let column = Object.keys( pewc_look_up_tables['sticker_price'] ).find(function(value, index, array) { | |
return ( parseFloat(value.trim()) >= size ); | |
}); | |
size = column.trim(); | |
let row = Object.keys( pewc_look_up_tables['sticker_price'][column] ).find(function(value, index, array) { | |
return ( parseFloat(value.trim()) >= qty ); | |
}); | |
qty = row.trim(); | |
let size_spaces_price = ''; | |
for ( let i = 0; i < 5; i++ ) { | |
if ( pewc_look_up_tables['sticker_price'][size_spaces_price + size] != undefined ) { | |
break; | |
} | |
size_spaces_price += ' '; | |
} | |
let size_spaces_discount = ''; | |
for ( let i = 0; i < 5; i++ ) { | |
if ( pewc_look_up_tables['sticker_price_discount'][size_spaces_discount + size] != undefined ) { | |
break; | |
} | |
size_spaces_discount += ' '; | |
} | |
price = pewc_look_up_tables['sticker_price'][size_spaces_price + size][qty]; | |
price_discount = pewc_look_up_tables['sticker_price_discount'][size_spaces_discount + size][qty]; | |
discounted_price_amount = price - (price * (price_discount / 100) ); | |
} catch (err) { | |
console.log("error: " + err); | |
price = 0; | |
price_discount = 0; | |
discounted_price_amount = 0; | |
} | |
if ( discounted_price_amount != 0 && ! isNaN(discounted_price_amount) ) { | |
if ( ! ( is_custom_qty && $('.pewc-field-'+qty_radio_field_id).attr('data-field-value') != custom_qty_label ) ) { | |
$(this).parent().find('.qty-discounted-price').text(pewc_vars.currency_symbol + discounted_price_amount); | |
} | |
} | |
if ( price_discount != 0 && price_discount != undefined ) { | |
if ( ! ( is_custom_qty && $('.pewc-field-'+qty_radio_field_id).attr('data-field-value') != custom_qty_label ) ) { | |
$(this).parent().find('.qty-percent-savings').text("Save " + price_discount + "%"); | |
} | |
} | |
} | |
}); | |
}, 500); | |
}); | |
$('.pewc-field-'+required_fields[3].field_id).trigger('change'); | |
$('.pewc-field-'+required_fields[6].field_id).trigger('change'); | |
}); | |
</script> | |
<?php | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment