How to add a custom working Shipping Method in WooCommerce 3 How to add a custom working Shipping Method in WooCommerce 3 wordpress wordpress

How to add a custom working Shipping Method in WooCommerce 3


The method key on "woocommerce_shipping_methods" should match the shipping method id

In your case:You should change

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

To:

function request_shipping_quote_shipping_method( $methods ) {    $methods['request_a_shipping_quote'] = 'WC_Request_Shipping_Quote_Method';    return $methods;}add_filter( 'woocommerce_shipping_methods', 'request_shipping_quote_shipping_method' );


Change this line

public function calculate_shipping( $package ) {

to this line

public function calculate_shipping( $package = array() ) {


After I trying to use code in question and fix all errors that I found in comments to these post I still had some problems with it. For example I can't edit shipping method after even I successfully added it to shipping zone.

enter image description here

Finally I got desired code that working for me after edit standard free shipping woocoommerce method. Hope it will save time for someone.

function request_a_shipping_quote_init() {    if ( ! class_exists( 'Imp_WC_Shipping_Local_Pickup' ) ) {        class Imp_WC_Pickup_Shipping_Method extends WC_Shipping_Method {            /**             * Constructor.             *             * @param int $instance_id             */            public function __construct( $instance_id = 0 ) {                $this->id           = 'imp_pickup_shipping_method';                $this->instance_id  = absint( $instance_id );                $this->method_title = __( "Самовывоз из точки выдачи ( MO г. Дзержинский )", 'imp' );                $this->supports     = array(                    'shipping-zones',                    'instance-settings',                    'instance-settings-modal',                );                $this->init();            }            /**             * Initialize custom shiping method.             */            public function init() {                // Load the settings.                $this->init_form_fields();                $this->init_settings();                // Define user set variables                $this->title = $this->get_option( 'title' );                // Actions                add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );            }            /**             * Calculate custom shipping method.             *             * @param array $package             *             * @return void             */            public function calculate_shipping( $package = array() ) {                $this->add_rate( array(                    'label'   => $this->title,                    'package' => $package,                ) );            }            /**             * Init form fields.             */            public function init_form_fields() {                $this->instance_form_fields = array(                    'title' => array(                        'title'       => __( 'Самовывоз из точки выдачи ( MO г. Дзержинский )', 'imp' ),                        'type'        => 'text',                        'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),                        'default'     => __( 'Самовывоз из точки выдачи ( MO г. Дзержинский )', 'imp' ),                        'desc_tip'    => true,                    ),                );            }        }    }}add_action( 'woocommerce_shipping_init', 'request_a_shipping_quote_init' );function request_shipping_quote_shipping_method( $methods ) {    $methods['imp_pickup_shipping_method'] = 'Imp_WC_Pickup_Shipping_Method';    return $methods;}add_filter( 'woocommerce_shipping_methods', 'request_shipping_quote_shipping_method' );