0

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.

MrBamBam
  • 1
  • 2
  • As this issue occurs outside Netherlands, it can be a setting problem or if you got multiple Shipping zones, the problem is in your code (as you should need to have different rate IDs for each shipping zone) – LoicTheAztec Sep 26 '17 at 15:16
  • I think you're right on the money with me having multiple shipping zones. In other words, the best course of action would be to exclude those shipping zones from this code by some sort of if statement? – MrBamBam Sep 28 '17 at 08:26
  • You should set in arrays all similar shipping rates (or methods) from your shipping zones, like in this recent answer: [Hide specifics Flat Rates when Free Shipping is available in WooCommerce 3](https://stackoverflow.com/questions/46391064/hide-specifics-flat-rates-when-free-shipping-is-available-in-woocommerce-3/46394520#46394520)… Then you can use `in_array()` in your conditions. – LoicTheAztec Sep 28 '17 at 08:48
  • Thanks for the quick reply. Will try this tonight and I will post the results here. Thanks again for the reply! – MrBamBam Sep 28 '17 at 08:56
  • 1
    Your comment did the trick! Thanks a lot. Am I able to mark a comment as the answer to my question? – MrBamBam Oct 02 '17 at 15:15
  • So you should answer your own question may be (and accept your answer as the good one)… it can be useful for others. – LoicTheAztec Oct 02 '17 at 18:28

0 Answers0