Title: Problem with Custom Shipping Zone Method
Last modified: December 22, 2022

---

# Problem with Custom Shipping Zone Method

 *  Resolved [noadpl](https://wordpress.org/support/users/noadpl/)
 * (@noadpl)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/problem-with-custom-shipping-zone-method/)
 * Hello,
 * I’ve spended last few hours trying to resolve the proper way to achieve solution
   for creating a custom Shipping Zone Method. 
   WC: 7.2.2WP: 6.0.3Site Health is
   ok, no permission errors etc. (only updates available info)I used few information
   sources like: [https://woocommerce.github.io/woocommerce-rest-api-docs/?php#shipping-zone-methods](https://woocommerce.github.io/woocommerce-rest-api-docs/?php#shipping-zone-methods)
   [https://github.com/woocommerce/woocommerce/wiki/Shipping-Method-API#properties](https://github.com/woocommerce/woocommerce/wiki/Shipping-Method-API#properties)
   [https://gist.github.com/mikejolley/3b37b9cc19a774665f31](https://gist.github.com/mikejolley/3b37b9cc19a774665f31)
   [https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce–cms-26098](https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce–cms-26098)
   And many other. Basicly i need to sample code to create (plugin) for shipping
   zone method with custom shipping cost function. We base od Woocommerce Easy Booking
   and we have number of days in meta, thtats why we can’t use one of the woocommerce
   official premium plugins to handle that.I’ve ended up with code below:
 *     ```wp-block-code
       <?php
       if (!defined('ABSPATH')) {
           exit; // Exit if accessed directly
       }
       function your_shipping_method_init()
       {
           if (!class_exists('WC_Your_Shipping_Method')) {
               class WC_Your_Shipping_Method extends WC_Shipping_Method
               {
                   /**
                    * Constructor for your shipping class
                    *
                    * @access public
                    * @return void
                    */
                   public function __construct()
                   {
                       $this->id                 = 'diet_day_shipping'; // Id for your shipping method. Should be uunique.
                       $this->method_title       = __('Shipping per diet day');  // Title shown in admin
                       $this->method_description = __('Shipping cost per booking'); // Description shown in admin
                       $this->enabled            = "yes"; // This can be added as an setting but for this example its forced enabled
                       //$this->title              = "Easy Booking Shipping per day"; // This can be added as an setting but for this example its forced.
                       //$this->rates              = array('calculate_shipping');
                       $this->supports              = array(
                           'shipping-zones',
                           'instance-settings',
                           'instance-settings-modal',
                       );
                       $this->instance_form_fields =
                           array(
                               'title' => array(
                                   'title'         => __('Method Title'),
                                   'type'             => 'text',
                                   'description'     => __('This controls the title which the user sees during checkout.'),
                                   'default'        => __('Daily delivery for Your diet'),
                               ),
                               'cost' => array(
                                   'title'         => __('Cost'),
                                   'type'             => 'number',
                                   'description'     => __('This controls the cost per day.'),
                                   'default'        => 7,
                               )
                           );
                       $this->init();
                           $this->title                = $this->get_option('title');
                           $this->cost                = $this->get_option('cost');
   
                       add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
                   }
   
                   /**
                    * Init your settings
                    *
                    * @access public
                    * @return void
                    */
                   function init()
                   {
                       // Load the settings API
                       $this->init_form_fields();
   
                       // Save settings in admin if you have any defined
                       add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
                   }
   
                   function init_form_fields()
                   {
                       $this->form_fields = array(
                           'title' => array(
                               'title'         => __('Method Title'),
                               'type'             => 'text',
                               'description'     => __('This controls the title which the user sees during checkout.'),
                               'default'        => __('Daily diet shipping cost'),
                           ),
                           'cost' => array(
                               'title'         => __('Cost'),
                               'type'             => 'number',
                               'description'     => __('This controls the cost per day.'),
                               'default'        => 7,
                           )
                       );
                   }
   
                   /**
                    * calculate_shipping function.
                    * @param array $package (default: array())
                    */
                   public function calculate_shipping($package = array())
                   {
   
                       //hardcode for testing
                       $shipping_number = 7;
   
                       //probably way to calculate final shipping cost 
                       /*
                       foreach ($package['contents'] as $item_id => $values) {
                           $_product = $values['data'];
                           $shipping_number = $shipping_number + $_product->wceb_get_product_booking_duration() * $values['quantity'];
                       }*/
   
                       $this->add_rate(array(
                           'id'    => $this->id . $this->instance_id,
                           'label' => 'Dostawa poza Wa-wa',
                           'cost'  => $shipping_number,
                       ));
                   }
               }
           }
       }
   
       add_action('woocommerce_shipping_init', 'your_shipping_method_init');
   
       function add_your_shipping_method($methods)
       {
           $methods['diet_day_shipping'] = 'WC_Your_Shipping_Method';
           return $methods;
       }
   
       add_filter('woocommerce_shipping_methods', 'add_your_shipping_method');
       ```
   
 * I need to have 2 settings (it can be only in zone, method, no need global) Title(
   to display for customers) and cost – to know value to multiplay days by.
   I want
   to “Edit” button show below method on shipping zone method where i can set theese
   values. Can someone help fix my code ?
 * For now it shows no settings options in admin panel. Not in shipping zone method
   row, and no in sgipping settings panel.
 * Also when i ser postal code from zone with my custom shipping method – cart says
   that there are no availble shipping methods to that postal code)
    -  This topic was modified 3 years, 4 months ago by [noadpl](https://wordpress.org/support/users/noadpl/).
      Reason: added last 2 paragraphs with additional info
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fproblem-with-custom-shipping-zone-method%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 1 replies (of 1 total)

 *  [Saif](https://wordpress.org/support/users/babylon1999/)
 * (@babylon1999)
 * [3 years, 4 months ago](https://wordpress.org/support/topic/problem-with-custom-shipping-zone-method/#post-16316766)
 * Hello [@noadpl](https://wordpress.org/support/users/noadpl/),
 * This is a fairly complex development topic that’s not within our scope of support
   here in the forums. I’m going to leave it open for a bit to see if anyone is 
   able to chime in to help you out.
 * I can also recommend [the WooCommerce Developer Resources Portal](https://developer.woocommerce.com/)
   for resources on developing for WooCommerce.
 * You can also visit the [WooCommerce Facebook group](https://www.facebook.com/groups/advanced.woocommerce/)
   or the `#developers` channel of the [WooCommerce Community Slack](https://woocommerce.com/community-slack/).
   We’re lucky to have a great community of open-source developers for WooCommerce,
   and many of our developers hang out there, as well.
 * Cheers!

Viewing 1 replies (of 1 total)

The topic ‘Problem with Custom Shipping Zone Method’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 1 reply
 * 2 participants
 * Last reply from: [Saif](https://wordpress.org/support/users/babylon1999/)
 * Last activity: [3 years, 4 months ago](https://wordpress.org/support/topic/problem-with-custom-shipping-zone-method/#post-16316766)
 * Status: resolved