First of all I would like to thank you for taking the time to read trough the question. The code often refers back to dutch words, please let me know if any translation is needed in order to understand the code.
The situation: I've wrote a custom function to unset certain shipping classes if certain conditions are met to make sure only one option would be visible for the customer.
Below is a description of the conditions and what I'm trying to accomplish:
- If someone orders two or more articles tagged with the shipping class "briefpost", the shipping rate should be equal to a "package".
- If an order has the shipping class "free shipping and insurance", add the free insurance to the cart.
Now I've written code that does just that. And it semi works. I've only introduced a bug where if you select a different shipping country (other then the Netherlands) during checkout, the checkout will be greyed out and you won't be able to complete the order.
I have been debugging the function to see where it all goes wrong and came to the conclusion that even without any lines of code it goes wrong with the add_filter part. As soon as this is introduced in the functions.php of my theme the checkout for other countries than the Netherlands are impossible to complete (It can even break the site for some users).
Now for some code examples below is the filter I used :
add_filter( 'woocommerce_package_rates', 'unset_shipping_when_free_is_available_in_zone', 100, 2 );
The actual function being called :
function unset_shipping_when_free_is_available_in_zone( $rates, $package ) {
global $woocommerce;
$shipping_class_target = briefpost;
$shipping_class_id = 58;
$shipping_class_package_id = 47;
$free_insurance = 2134;
$insurance_found = false;
$no_package = true;
$number_of_briefpost = 0;
foreach( WC()->cart->get_cart() as $cart_item_key => $values ) {
$_product = $values['data'];
$terms = get_the_terms($_product->get_id(), $shipping_class_target);
$shipping_class = $values['data']->get_shipping_class_id();
if ($shipping_class === $shipping_class_id) {
$rates['flat_rate:4']->label = 'Mailbox';
$number_of_briefpost += $values['quantity'];
} else if($shipping_class === $shipping_class_package_id) {
$rates['flat_rate:4']->label = 'Parcel';
$no_package = false;
} else if($_product->get_id() == $free_insurance) {
$insurance_found = true;
$prod_unique_id = WC()->cart->generate_cart_id( $_product->get_id() );
}
}
if ($number_of_briefpost > 1 && $no_package) {
$rates['flat_rate:4']->cost = $rates['flat_rate:4']->cost + 2.30;
$rates['flat_rate:4']->label = 'Parcel';
}
if (isset( $rates['free_shipping:13'] ) ) {
unset( $rates['flat_rate:4'] );
if( !$insurance_found ) {
WC()->cart->add_to_cart($free_insurance, 1);
}
}
if (isset( $rates['flat_rate:4'])) {
if($insurance_found) {
unset( WC()->cart->cart_contents[$prod_unique_id] );
}
}
return $rates;
}
All help is greatly appreciated!
Edit: I'm also open for suggestions on payed solutions that fit my exact needs. However in my search so far I've found none.