• Resolved coopersita

    (@coopersita)


    I made a custom shipping method for Woocommerce. It shows in shipping zones, and it’s been added to all the zones, but during checkout, the option for selecting this method doesn’t show up.

    Here’s my code:

    add_action('woocommerce_shipping_init', 'UPS_number_shipping_method');
    function UPS_number_shipping_method()
    {
    
        if (!class_exists('UPS_Number_Shipping_Method')) {
            class UPS_Number_Shipping_Method extends WC_Shipping_Method
            {
    
                public function __construct($instance_id = 0)
                {
                    $this->id = 'UPS_number_shipping';
                    $this->instance_id = absint($instance_id);
                    $this->domain = 'UPS_number_shipping';
                    $this->method_title = __('UPS Number', $this->domain);
                    $this->method_description = __('Shipping method to be used when dealer has a UPS number', $this->domain);
                    $this->title = __('UPS Number', $this->domain);
                    $this->supports = array(
                        'shipping-zones',
                        'instance-settings',
                        'instance-settings-modal',
                    );
                    $this->init();
                }
    
                ## Load the settings API
                function init()
                {
                    $this->init_form_fields();
                    $this->init_settings();
                    $this->enabled = 'yes';
                    $this->title   = __('UPS Number', $this->domain);
    
                    add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
                }
    
                public function calculate_shipping($packages = array())
                {
    
                    $this->add_rate(0);
                }
            }
        }
    }
    
    add_filter('woocommerce_shipping_methods', 'add_UPS_number_shipping');
    function add_UPS_number_shipping($methods)
    {
        $methods['UPS_number_shipping'] = 'UPS_Number_Shipping_Method';
        return $methods;
    }
    
Viewing 7 replies - 1 through 7 (of 7 total)
  • Plugin Support Rynald0s.a11n

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @coopersita!

    Try the following code:

    add_filter('woocommerce_shipping_methods', 'add_UPS_number_shipping');
    function add_UPS_number_shipping($methods) {
        $methods['UPS_number_shipping'] = 'UPS_Number_Shipping_Method';
        return $methods;
    }
    
    class UPS_Number_Shipping_Method extends WC_Shipping_Method {
    
                public function __construct($instance_id = 0) {
                    $this->id = 'UPS_number_shipping';
                    $this->instance_id = absint($instance_id);
                    $this->domain = 'UPS_number_shipping';
                    $this->method_title = __('UPS Number', $this->domain);
                    $this->method_description = __('Shipping method to be used when dealer has a UPS number', $this->domain);
                    $this->title = __('UPS Number', $this->domain);
                    $this->supports = array(
                        'shipping-zones',
                        'instance-settings',
                        'instance-settings-modal',
                    );
    			
    				$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' 	=> __( 'This controls the title which the user sees during checkout.' ),
    					'default'		=> __( 'UPS Number' ),
    					'desc_tip'		=> true
    				)
    			);
    
                    $this->enabled = $this->get_option( 'enabled' );
                    $this->title   = __('UPS Number', $this->domain);
    
                    add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
                }
    
    	public function calculate_shipping( $package = array() ) {
    		$this->add_rate( array(
    			'id'    => $this->id . $this->instance_id,
    			'label' => $this->title,
    			'cost'  => 0,
    	) );
    	} 
    }
    Thread Starter coopersita

    (@coopersita)

    Thanks, but I get this error:

    “Fatal error: Uncaught Error: Class ‘WC_Shipping_Method’ not found ”

    I tried wrapping it in a function, but now it doesn’t even show in my shipping zones. Also, I get no settings page for it so there is no way to enable it:

    add_filter('woocommerce_shipping_methods', 'add_UPS_number_shipping');
    function add_UPS_number_shipping($methods)
    {
        $methods['UPS_number_shipping'] = 'UPS_Number_Shipping_Method';
        return $methods;
    }
    add_action('woocommerce_shipping_init', 'UPS_number_shipping_method');
    function UPS_number_shipping_method()
    {
        class UPS_Number_Shipping_Method extends WC_Shipping_Method
        {
    
            public function __construct($instance_id = 0)
            {
                $this->id = 'UPS_number_shipping';
                $this->instance_id = absint($instance_id);
                $this->domain = 'UPS_number_shipping';
                $this->method_title = __('UPS Number', $this->domain);
                $this->method_description = __('Shipping method to be used when dealer has a UPS number', $this->domain);
                $this->title = __('UPS Number', $this->domain);
                $this->supports = array(
                    'shipping-zones',
                    'instance-settings',
                    'instance-settings-modal',
                );
    
                $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'     => __('This controls the title which the user sees during checkout.'),
                        'default'        => __('UPS Number'),
                        'desc_tip'        => true
                    )
                );
    
                $this->enabled = $this->get_option('enabled');
                $this->title   = __('UPS Number', $this->domain);
    
                add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
            }
    
            public function calculate_shipping($package = array())
            {
                $this->add_rate(array(
                    'id'    => $this->id . $this->instance_id,
                    'label' => $this->title,
                    'cost'  => 0,
                ));
            }
        }
    }
    Plugin Support Rynald0s.a11n

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @coopersita!

    Let me have another quick look at it and get back to you. In the meantime, during testing from my side, this is what the outcome was:

    https://snipboard.io/mokQ0n.jpg

    https://snipboard.io/r1GhAm.jpg

    https://snipboard.io/Y5rbu3.jpg

    Will get back to you shortly.

    Cheers!

    Thread Starter coopersita

    (@coopersita)

    Thanks.

    I tried installing it on a site with the Twenty Twenty theme and only Woocommerce and this plugin installed, and still nothing. No options, not listed in shipping zones.

    To make it a plugin all I did was put this one file on a folder and installed it like a regular plugin. I tried pasting the code into functions.php in the theme, and also didn’t work.

    Plugin Support Rynald0s.a11n

    (@rynald0s)

    Automattic Happiness Engineer

    Hi @coopersita!

    I reviewed the code once again, and it works as expected.

    Are you not seeing it when adding a method here?

    https://snipboard.io/4c3unf.jpg

    The settings for this method is available when you hover over the method, and then clinking the “edit” button.

    Could you please send your status report here? You can find it under “WooCommerce > Status” page. Also, what version of WooCommerce core / WP are you running, and is your database up to date, too?

    Cheers!

    Thread Starter coopersita

    (@coopersita)

    That’s so weird. Maybe it was some sort of cache issue because it’s working now. I didn’t change anything since yesterday.

    Thanks a million!

    Hi @coopersita

    Maybe it was some sort of cache issue because it’s working now.

    Glad to hear it’s working now. I’m marking this thread as resolved now. If you have any further questions, feel free to open a new topic.

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘My custom shipping method doesn’t show up in the checkout page’ is closed to new replies.