Skip to content

Instantly share code, notes, and snippets.

@rpocc
Forked from mikejolley/gist:6713608
Last active April 23, 2020 19:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rpocc/06f63d9383b5e742705e921e8e46f193 to your computer and use it in GitHub Desktop.
Save rpocc/06f63d9383b5e742705e921e8e46f193 to your computer and use it in GitHub Desktop.
WooCommerce Shipping Method Skeleton Plugin
<?php
/*
Plugin Name: Your Shipping plugin
Plugin URI: http://woothemes.com/woocommerce
Description: Your shipping method plugin
Version: 1.0.1
Author: WooThemes
Author URI: http://woothemes.com
*/
/**
* Check if WooCommerce is active
*/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
class your_shipping_method_setup {
public function __construct() {
add_action( 'woocommerce_shipping_init', array($this, 'your_shipping_method_init') );
add_filter( 'woocommerce_shipping_methods', array( $this, 'add_your_shipping_method') );
}
public function your_shipping_method_init() {
include('your_shipping_plugin_class.php');
}
public function add_your_shipping_method( $methods ) {
$methods['your_shipping_method'] = 'WC_Your_Shipping_Method';
return $methods;
}
}
new your_shipping_method_setup();
}
<?php
if ( ! class_exists( 'WC_Your_Shipping_Method' ) ) {
class WC_Your_Shipping_Method extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct( $instance_id = 0 ) {
$this->id = 'your_shipping_method'; // Id for your shipping method. Should be uunique.
$this->instance_id = absint( $instance_id );
$this->method_title = __( 'Your Shipping Method' ); // Title shown in admin
$this->method_description = __( 'Description of your shipping method' ); // Description shown in admin
$this->supports = array(
'shipping-zones',
'instance-settings',
'instance-settings-modal',
);
$this->enabled = "yes"; // This can be added as an setting but for this example its forced enabled
$this->init();
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
}
/**
* Init your settings
*
* @access public
* @return void
*/
public function init() {
// Load the settings API
$this->instance_form_fields = include( 'your_shipping_plugin_settings.php' );
$this->title = $this->get_option( 'title' );
$this->tax_status = $this->get_option( 'tax_status' );
$this->type = $this->get_option( 'type', 'class' );
// Save settings in admin if you have any defined
}
/**
* calculate_shipping function.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping( $package = array() ) {
$rate = array(
'id' => $this->get_rate_id(),
'label' => $this->title,
'cost' => 10.99,
'taxes' => true,
'package' => $package
);
// Register the rate
$this->add_rate( $rate );
do_action( 'woocommerce_' . $this->id . '_shipping_add_rate', $this, $rate );
}
public function get_package_item_qty( $package ) {
$total_quantity = 0;
foreach ( $package['contents'] as $item_id => $values ) {
if ( $values['quantity'] > 0 && $values['data']->needs_shipping() ) {
$total_quantity += $values['quantity'];
}
}
return $total_quantity;
}
public function find_shipping_classes( $package ) {
$found_shipping_classes = array();
foreach ( $package['contents'] as $item_id => $values ) {
if ( $values['data']->needs_shipping() ) {
$found_class = $values['data']->get_shipping_class();
if ( ! isset( $found_shipping_classes[ $found_class ] ) ) {
$found_shipping_classes[ $found_class ] = array();
}
$found_shipping_classes[ $found_class ][ $item_id ] = $values;
}
}
return $found_shipping_classes;
}
}
}
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Settings for Russian post shipping.
*/
$settings = array(
'title' => array(
'title' => __( 'Method Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'Your Shipping Plugin', 'woocommerce' ),
'desc_tip' => false
),
'tax_status' => array(
'title' => __( 'Tax Status', 'woocommerce' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'taxable',
'options' => array(
'taxable' => __( 'Taxable', 'woocommerce' ),
'none' => _x( 'None', 'Tax status', 'woocommerce' )
)
),
);
$shipping_classes = WC()->shipping->get_shipping_classes();
if ( ! empty( $shipping_classes ) ) {
$settings[ 'class_costs' ] = array(
'title' => __( 'Shipping Class Costs', 'woocommerce' ),
'type' => 'title',
'default' => '',
'description' => sprintf( __( 'These costs can optionally be added based on the %sproduct shipping class%s.', 'woocommerce' ), '<a href="' . admin_url( 'edit-tags.php?taxonomy=product_shipping_class&post_type=product' ) . '">', '</a>' )
);
foreach ( $shipping_classes as $shipping_class ) {
if ( ! isset( $shipping_class->term_id ) ) {
continue;
}
$settings[ 'class_cost_' . $shipping_class->term_id ] = array(
'title' => sprintf( __( '"%s" Shipping Class Cost', 'woocommerce' ), esc_html( $shipping_class->name ) ),
'type' => 'text',
'placeholder' => __( 'N/A', 'woocommerce' ),
'description' => $cost_desc,
'default' => $this->get_option( 'class_cost_' . $shipping_class->slug ), // Before 2.5.0, we used slug here which caused issues with long setting names
'desc_tip' => true
);
}
$settings[ 'no_class_cost' ] = array(
'title' => __( 'No Shipping Class Cost', 'woocommerce' ),
'type' => 'text',
'placeholder' => __( 'N/A', 'woocommerce' ),
'description' => $cost_desc,
'default' => '',
'desc_tip' => true
);
$settings[ 'type' ] = array(
'title' => __( 'Calculation Type', 'woocommerce' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'class',
'options' => array(
'class' => __( 'Per Class: Charge shipping for each shipping class individually', 'woocommerce' ),
'order' => __( 'Per Order: Charge shipping for the most expensive shipping class', 'woocommerce' ),
),
);
}
return $settings;
@Bogdancev
Copy link

Thanx for the template. I used it and now ran into problem due to not being good in php.
How can I call methods of class your_shipping_method_setup (first file) from class WC_Your_Shipping_Method (second file)?

I added following (in bold) to the first file:
global $my_var;
$my_var = new your_shipping_method_setup();

but still cannot call its methods in second file like:
$my_var->my_method();

Am I missing something?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment