• Resolved noadpl

    (@noadpl)


    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.2
    WP: 6.0.3

    Site 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://github.com/woocommerce/woocommerce/wiki/Shipping-Method-API#properties

    https://gist.github.com/mikejolley/3b37b9cc19a774665f31

    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:

    <?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. Reason: added last 2 paragraphs with additional info

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)

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