Home / Admin / Create a Custom Shipping Method in WooCommerce
Duplicate Snippet

Embed Snippet on Your Site

Create a Custom Shipping Method in WooCommerce

This snippet allows you to create and add a custom shipping method to your WooCommerce store.

Code Preview
php
<?php
add_filter('woocommerce_shipping_methods', 'register_wholesale_shipping_method');
function register_wholesale_shipping_method($methods) {
    $methods['wholesale_shipping'] = 'WC_Shipping_Wholesale';
    return $methods;
}
if (!class_exists('WC_Shipping_Wholesale')) {
    class WC_Shipping_Wholesale extends WC_Shipping_Method {
        public function __construct($instance_id = 0) {
            $this->id                 = 'wholesale_shipping';
            $this->instance_id        = absint($instance_id);
            $this->method_title       = __('Wholesale Shipping');
            $this->method_description = __('Flat rate shipping for wholesale customers only.');
            $this->supports           = ['shipping-zones', 'instance-settings'];
            $this->init();
        }
        public function init() {
            $this->init_form_fields();
            $this->init_settings();
            $this->enabled       = $this->get_option('enabled', 'yes');
            $this->title         = $this->get_option('title', __('Wholesale Shipping'));
            $this->cost          = $this->get_option('cost', 0);
            $this->tax_status    = $this->get_option('tax_status', 'taxable');
            add_action('woocommerce_update_options_shipping_' . $this->id, [$this, 'process_admin_options']);
        }
        public function init_form_fields() {
            $this->instance_form_fields = array(
                'enabled' => array(
                    'title'   => __('Enable/Disable'),
                    'type'    => 'checkbox',
                    'label'   => __('Enable this shipping method'),
                    'default' => 'yes',
                ),
                'title' => array(
                    'title'       => __('Method Title'),
                    'type'        => 'text',
                    'description' => __('Displayed during checkout.'),
                    'default'     => __('Wholesale Shipping'),
                    'desc_tip'    => true,
                ),
                'cost' => array(
                    'title'       => __('Flat Rate Cost'),
                    'type'        => 'number',
                    'description' => __('Flat cost for wholesale shipping.'),
                    'default'     => 0,
                    'custom_attributes' => [
                        'step' => '0.01',
                        'min'  => '0',
                    ],
                ),
                'tax_status' => array(
                    'title'   => __('Tax status'),
                    'type'    => 'select',
                    'default' => 'taxable',
                    'class'   => 'wc-enhanced-select',
                    'options' => array(
                        'taxable' => __('Taxable'),
                        'none'    => __('None'),
                    ),
                ),
            );
        }
        public function calculate_shipping($package = array()) {
            $rate = array(
                'id'        => $this->id . ':' . $this->instance_id,
                'label'     => $this->title,
                'cost'      => $this->cost,
                'taxes'     => '',
                'calc_tax'  => $this->tax_status,
            );
            $this->add_rate($rate);
        }
    }
}

Comments

Add a Comment