I'm trying to show one specific add-on shipping insurance product based on the WooCommerce SubTotal. My code ab-ends on line 10. I think the hide/show process needs a wrapper but I'm unsure how to code it.
I was given a suggestion but I don't know how to implement it...
The suggestion:
It looks like you're trying to use a mixed PHP / javascript approach based on hiding or showing fields.
Instead, I'd recommend hooking into the woocommerce_checkout_fields
at a priority greater than 10 (like 20) -- this would let you get all fields from the $checkout_fields['add_ons']
array, and then you can "unset" some of them based on the cart subtotal (which is significantly easier to grab via PHP instead of javascript).
Anyone who can help... Thank you tons in advance. My original try at it...
<?php
function wc_shipping_insurance_chooser() {
// Set variables
$fifty = 50;
$one_hundred = 100;
$two_hundred = 200;
if (WC()->cart->total >$fifty && WC()->cart->total <$one_hundred ) {
If( is_cart() ) {
// Show Insurance cost for $50 - $100
$( '#wc_checkout_add_ons_10_field' ).show();
$( '#wc_checkout_add_ons_11_field' ).hide();
$( '#wc_checkout_add_ons_12_field' ).hide();
$( '#wc_checkout_add_ons_13_field' ).hide();
$( '#wc_checkout_add_ons_14_field' ).hide();
$( '#wc_checkout_add_ons_15_field' ).hide();
} else {
$( '#wc_checkout_add_ons_10_field' ).hide();
$( '#wc_checkout_add_ons_11_field' ).hide();
$( '#wc_checkout_add_ons_12_field' ).hide();
$( '#wc_checkout_add_ons_13_field' ).hide();
$( '#wc_checkout_add_ons_14_field' ).hide();
$( '#wc_checkout_add_ons_15_field' ).hide();
}
} elseif (WC()->cart->total >$one_hundred && WC()->cart->total <$two_hundred ) {
If( is_cart() ) {
// Show Insurance cost for $100-$199
$( '#wc_checkout_add_ons_10_field' ).hide();
$( '#wc_checkout_add_ons_11_field' ).show();
$( '#wc_checkout_add_ons_12_field' ).hide();
$( '#wc_checkout_add_ons_13_field' ).hide();
$( '#wc_checkout_add_ons_14_field' ).hide();
$( '#wc_checkout_add_ons_15_field' ).hide();
} else {
$( '#wc_checkout_add_ons_10_field' ).hide();
$( '#wc_checkout_add_ons_11_field' ).hide();
$( '#wc_checkout_add_ons_12_field' ).hide();
$( '#wc_checkout_add_ons_13_field' ).hide();
$( '#wc_checkout_add_ons_14_field' ).hide();
$( '#wc_checkout_add_ons_15_field' ).hide();
}
} else {
$( '#wc_checkout_add_ons_10_field' ).hide();
$( '#wc_checkout_add_ons_11_field' ).hide();
$( '#wc_checkout_add_ons_12_field' ).hide();
$( '#wc_checkout_add_ons_13_field' ).hide();
$( '#wc_checkout_add_ons_14_field' ).hide();
$( '#wc_checkout_add_ons_15_field' ).hide();
}
}
add_action( 'woocommerce_checkout_process', 'wc_shipping_insurance_chooser' );
add_action( 'woocommerce_before_cart' , 'wc_shipping_insurance_chooser' );
?>