I’m making a custom shipping method for woocommerce – and my problem is that can add it to a specific zone in the admin-area & adjust settings just fine. But for some reason it does not show up on the checkout page – and i have no clue why.

it should be very straight forward, and i have actually done this a couple of times before (pre 3.0) – but i simply cant get it to work now.

Main Class

abstract class Main_Shipping extends WC_Shipping_Method
{
abstract protected function register_shipping($methods);
abstract protected function addActions();
abstract protected function addFilters();

function init() 
{
    $this->instance_form_fields                     = include('setting/shipping-settings.php' );
    $this->title                                    = $this->get_option('title' );
    $this->tax_status                               = $this->get_option('tax_status');
    $this->shipping_price                           = $this->get_option('shipping_price');
    $this->enable_free_shipping                     = $this->get_option('enable_free_shipping');
    $this->free_shipping_total                      = $this->get_option('free_shipping_total');

    $this->init_settings(); 

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

public function calculate_shipping($package = array())
{
    global $woocommerce;
    $return_price = $this->shipping_price;
    if(!isset($return_price))
    {
       $return_price = 0;
    }
    $rate = array(
        'id'      => $this->id .'_'. $this->instance_id,
        'label'   => $this->title,
        'cost'    => $return_price,
       );
    $this->add_rate( $rate );
}

public function oaship_pickuppoint()
{ 
    ob_start();
    ?>
        <div>
            test
        </div>

    <?php
    $sContent = ob_get_clean();
    echo $sContent;
}
}

Specific GLS carrier Class:

 require_once('main-shipping.php');
 class GLS_Shipping_Pickuppoint extends Main_Shipping
{
public function __construct( $instance_id = 0 ) 
{
    $this->id                    = 'gls_shipping_pickuppoint';
    $this->instance_id           = absint( $instance_id );
    $this->method_title          = __('GLS Pickup Point OA', '');
    $this->method_description    = __('Adds the option to ship with the GLS pickup point to the checkout', '');
    $this->supports              = array(
        'shipping-zones',
        'instance-settings',
    );
    $this->init(); 
}

function addActions()
{     
    add_action('woocommerce_after_shipping_rate', array($this, 'check_choosen_shipping_method') );
}

function addFilters()
{
    add_filter('woocommerce_shipping_methods', array($this, 'register_shipping_method'));
}

function register_shipping($methods) 
{

    $methods['gls_shipping_pickuppoint'] = 'GLS_Shipping_Pickuppoint';

    return $methods;
}

function check_choosen_shipping_method($rate)
{

    $chosen_methods = WC()->session->get( 'chosen_shipping_methods' );
    $chosen_shipping = preg_replace('/[0-9]+/', '', $chosen_methods[0]);;
    if($chosen_shipping === 'gls_shipping_pickuppoint')
    {
        if ($rate->method_id == 'gls_shipping_pickuppoint') {
            $this->oaship_pickuppoint();
        }
    }
}
}

$oOAGlsPickupPoint = new GLS_Shipping_Pickuppoint();
$oOAGlsPickupPoint->addActions();
 $oOAGlsPickupPoint->addFilters();

1 Answer
1

I’m developing the same functionality right now and something like this happen to me (I was able to see the settings in the admin dashboard but not in the checkout page)…

What I did was check the option Enable debug mode in WooCommerce/Settings/Shipping/Shipping options and then I was able to see my custom shipping in the checkout page. After that I disable the option again and my shipping is still visible in the checkout page (I don’t know the reason but it works)

I was checking your code and notice that you’re missing an important action to add a custom shipping:

add_action( 'woocommerce_shipping_init', 'tutsplus_shipping_method' );

where the tutsplus_shipping_method is a function that defines the class that extends the WC_Shipping_Method.

The action woocommerce_shipping_init is the main action for WooCommerce Shippings which includes all the shipping classes before they get instantiated. By using that action we are sure that our shipping method will be included after WooCommerce has been initialized and in the right place for WooCommerce to use it.

I also notice that your filter add_filter('woocommerce_shipping_methods', array($this, 'register_shipping_method')); points to a function that doesn’t exist, maybe it’s a type error and you wanted to point to your register_shipping function.

I followed this tutorial and everything was great: https://code.tutsplus.com/tutorials/create-a-custom-shipping-method-for-woocommerce–cms-26098

Leave a Reply

Your email address will not be published. Required fields are marked *