Hi there, I’m trying to build a very simple plugin for woocommerce to show some information about delivery without taxes. The diference for the other methods is the extra HTML field.

I read a lot of the documentation but I think there is something miss on the configuration that are messing the admin options (edit, delete). The code is below:

<?php
if ( ! defined( 'WPINC' ) ) {
   die('security by preventing any direct access to your plugin file');
}
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {

function shipping_delivery_info() {

    if (!class_exists('shipping_delivery_info')) {

        class shipping_delivery_info extends WC_Shipping_Method {

            public function __construct() {

                $this->id = 'shipping_delivery_info';
                $this->method_title = __('Shipping Delivery Info', 'shipping_delivery_info');
                $this->method_description = __('A Woocommerce custom shipping method plugin, that shows ' . 
                    'some shipping information to costumer, like free shipping but with HTML field.', 
                    'shipping_delivery_info');
                $this->supports = array(
                    'shipping-zones',
                    'instance-settings',
                    'instance-settings-modal',                      
                );

                $this->init();
            }

             /**
              * Load the settings API
              */
             function init() {

                // Load the settings
                $this->init_form_fields();
                $this->init_settings();

                $this->enabled = $this->get_option( 'enabled' );
                $this->title   = $this->get_option( 'title' );
                $this->info   = $this->get_option( 'info' );

                add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
             }

             function init_form_fields() {
                $this->instance_form_fields = array(
                    'enabled' => array(
                        'title'         => __( 'Enable/Disable', 'shipping_delivery_info'),
                        'type'          => 'checkbox',
                        'label'         => __( 'Enable this shipping method', 'shipping_delivery_info'),
                        'default'       => 'yes',
                    ),
                    'title' => array(
                        'title' => __('Title', 'shipping_delivery_info'),
                        'type' => 'text',
                        'description' => __( 'The title to be displayed during checkout.', 'shipping_delivery_info' ),
                        'default' => __('Shipping Information', 'shipping_delivery_info'),
                    ),
                    'info' => array(
                        'title' => __('Information', 'shipping_delivery_info'),
                        'type' => 'text',
                        'description' => __( 'Information about delivery and its taxes.', 'shipping_delivery_info' ),
                        'default' => __('Insert here some HTML.'),
                    ),
                );
             }
        }
    }
}
add_action('woocommerce_shipping_init', 'shipping_delivery_info');

function add_shipping_delivery_info($methods)
{
    $methods['shipping_delivery_info'] = 'shipping_delivery_info';
    return $methods;
}
add_filter('woocommerce_shipping_methods', 'add_shipping_delivery_info');

function shipping_delivery_info_message($posted)
{
    $packages = WC()->shipping->get_packages();
    $chosen_methods = WC()->session->get('chosen_shipping_methods');
    if (is_array($chosen_methods) && in_array('shipping_delivery_info', $chosen_methods)) {
        foreach ($packages as $i => $package) {
            if ($chosen_methods[$i] != "shipping_delivery_info") {
                continue;
            }
            $shipping_delivery_info = new shipping_delivery_info();

            $message = $shipping_delivery_info->settings['info'];
            return $message;
            /*$messageType = "info";                
            wc_add_notice($message, $messageType);*/
        }
    }
}

add_action('woocommerce_review_order_before_cart_contents', 'shipping_delivery_info_message', 10);
add_action('woocommerce_after_checkout_validation', 'shipping_delivery_info_message', 10);
}

Read more here: woocommerce shipping method with 3.0 api


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: