Search code examples
phpwordpresswoocommerceemail-notifications

WooCommerce : assign member to shipping zone


is there a way to assign a shipping zone to a member ?

I want to do that because I use WooCommerce for a food delivery site and I need to send an email to the closest Restaurant when someone makes an order

I was thinking about adding an email field in the shipping zone table like below (sorry my admin panel is in french)

shipping zone

Any idea to do that ?


Solution

  • Update (2019) - You should think that differently using woocommerce_email_recipient_new_order to add specific email recipients based on the shipping zone:

    add_filter( 'woocommerce_email_recipient_new_order', 'custom_email_recipient_new_order', 10, 2 );
    function custom_email_recipient_new_order( $recipient, $order ) {
        // Avoiding backend displayed error in Woocommerce email settings
        if ( ! is_a( $order, 'WC_Order' ) ) 
            return $recipient;
    
        // Get the shipping country and postcode
        $country_code = $order->get_shipping_country();
        $postcode     = $order->get_shipping_postcode();
    
        // Get All Zone IDs
        $zone_ids    = array_keys( array('') + WC_Shipping_Zones::get_zones() );
    
        // Loop through Zone IDs
        foreach ( $zone_ids as $zone_id ) {
            // Get the shipping Zone object
            $shipping_zone = new WC_Shipping_Zone($zone_id);
    
            // Loop through Zone locations
            foreach ( $shipping_zone->get_zone_locations() as $location ){
                if ( ( $location->type === 'postcode' && $location->code === $postcode ) 
                || ( $location->type === 'country' && $location->code === $country_code ) ) {
                    $the_zone_id   = $zone_id;
                    $the_zone_name = $shipping_zone->get_zone_name(); // (You can use the the zone name too)
                    break;
                } 
            }
            if($found) break;
        }
    
        if( $the_zone_id == 0 ) {
            $recipient .= ',' . 'james.collins@gmail.com';
        } elseif( $the_zone_id == 2 ) {
            $recipient .= ',' . 'jean.dubreuil@gmail.com';
        } elseif( $the_zone_id == 3 ) {
            $recipient .= ',' . 'joel.chalamousse@gmail.com';
        } else {
            $recipient .= ',' . 'isabelle.frottin@gmail.com';
        }
    
        return $recipient;
    }
    

    Code goes in function.php file of your active child theme (or active theme). It should works.