GeneratorsWooCommerceShipping Method

Shipping Method Generator

A WC_Shipping_Method subclass — settings fields, rate calculation, and the class-exists guard that stops it fataling on a site without WooCommerce active.

class-acme-flat-extra-shipping-method.php
Saved just now
The method
Acme_Flat_Extra_Shipping_Method
Extra settings fields
0 fields
No extra fields — just title and cost.
<?php
// Generated with WP CodeKit — powered by GrowQuest (https://growquest.io).
/**
 * Plugin Name:       Flat Rate Extra shipping method
 * Description:       Registers the acme_flat_extra shipping method.
 * Version:           1.0.0
 * Requires PHP:      7.4
 * Requires Plugins:  woocommerce
 * Text Domain:       acme
 */

defined( 'ABSPATH' ) || exit;

/**
 * Declare the class once WooCommerce's own shipping base class exists.
 */
function acme_shipping_method_init() {
	if ( class_exists( 'Acme_Flat_Extra_Shipping_Method' ) ) {
		return;
	}

	class Acme_Flat_Extra_Shipping_Method extends WC_Shipping_Method {

	public function __construct( $instance_id = 0 ) {
		$this->id                 = 'acme_flat_extra';
		$this->instance_id        = absint( $instance_id );
		$this->method_title       = __( 'Flat Rate Extra', 'acme' );
		$this->method_description = __( 'A flat additional cost added on top of the customer\'s chosen shipping option.', 'acme' );
		$this->supports           = array( 'shipping-zones', 'instance-settings' );

		$this->init();
	}

	/**
	 * Load the settings API — fields, saved values, and the save hook.
	 */
	public function init() {
		$this->init_form_fields();
		$this->init_settings();

		$this->title   = $this->get_option( 'title' );
		$this->cost    = $this->get_option( 'cost' );

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

	/**
	 * The settings fields, keyed by option name.
	 */
	public function init_form_fields() {
		$this->instance_form_fields = array(
		'title' => array(
			'title'       => __( 'Method title', 'acme' ),
			'type'        => 'text',
			'default'     => 'Flat Rate Extra',
			'description' => __( 'Shown to the customer at checkout.', 'acme' ),
			'desc_tip'    => true,
		),

		'cost' => array(
			'title'       => __( 'Cost', 'acme' ),
			'type'        => 'number',
			'default'     => '5.00',
			'description' => __( 'Enter a flat cost, or 0 to disable.', 'acme' ),
			'desc_tip'    => true,
		),
		);
	}

	/**
	 * Add a rate for this method to the package.
	 *
	 * @param array $package The cart contents and destination.
	 */
	public function calculate_shipping( $package = array() ) {
		$rate = array(
			'id'      => $this->get_rate_id(),
			'label'   => $this->title,
			'cost'    => $this->cost,
			'package' => $package,
			'taxes'   => '', // Let WooCommerce calculate tax on this rate.
		);

		$this->add_rate( $rate );
	}
	}
}
add_action( 'woocommerce_shipping_init', 'acme_shipping_method_init' );

/**
 * Register it so it can be added to a shipping zone.
 *
 * @param array $methods Existing methods.
 * @return array
 */
function acme_add_shipping_method( $methods ) {
	$methods['acme_flat_extra'] = 'Acme_Flat_Extra_Shipping_Method';

	return $methods;
}
add_filter( 'woocommerce_shipping_methods', 'acme_add_shipping_method' );