Skip to content

Instantly share code, notes, and snippets.

@vovadocent
Last active December 13, 2023 16:34
Show Gist options
  • Save vovadocent/27545ddfd9c3152156d28c72c03bf1c8 to your computer and use it in GitHub Desktop.
Save vovadocent/27545ddfd9c3152156d28c72c03bf1c8 to your computer and use it in GitHub Desktop.
Woo Custom Shipping Method
<?php
/**
* Plugin Name: Woo Custom Shipping Method
* Description: Custom Shipping Method for WooCommerce
* Version: 1.0.0
*/
if (!defined('WPINC')) {
die;
}
/*
* Check if WooCommerce is active
*/
if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) {
function custom_shipping_method() {
if (!class_exists('Custom_Shipping_Method')) {
class Custom_Shipping_Method extends WC_Shipping_Method {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
$this->id = 'customship';
$this->method_title = __('Elivate Shipping', 'woo_custom_shipping_method');
$this->method_description = __('Custom Shipping Method for Elivate', 'woo_custom_shipping_method');
// Availability & Countries
$this->availability = 'including';
$this->countries = array(
'US', // Unites States of America
/*
'CA', // Canada
'DE', // Germany
'GB', // United Kingdom
'IT', // Italy
'ES', // Spain
'HR' // Croatia
*/
);
$this->init();
$this->enabled = isset($this->settings['enabled']) ? $this->settings['enabled'] : 'yes';
$this->title = isset($this->settings['title']) ? $this->settings['title'] : __('Custom Shipping', 'woo_custom_shipping_method');
}
/**
* Init your settings
*
* @access public
* @return void
*/
function init() {
// Load the settings API
$this->init_form_fields();
$this->init_settings();
// Save settings in admin if you have any defined
add_action('woocommerce_update_options_shipping_' . $this->id, array($this, 'process_admin_options'));
}
/**
* Define settings field for this shipping
* @return void
*/
function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable', 'woo_custom_shipping_method'),
'type' => 'checkbox',
'description' => __('Enable this shipping.', 'woo_custom_shipping_method'),
'default' => 'yes'
),
'title' => array(
'title' => __('Title', 'woo_custom_shipping_method'),
'type' => 'text',
'description' => __('Title to be display on site', 'woo_custom_shipping_method'),
'default' => __('TutsPlus Shipping', 'woo_custom_shipping_method')
),
'woocustomshipping_price_1' => array(
'title' => __('Fee ($), count products = 1', 'woo_custom_shipping_method'),
'type' => 'text',
'description' => __('Shipping Fee for 1 product', 'woo_custom_shipping_method'),
'default' => 1
),
'woocustomshipping_price_2' => array(
'title' => __('Fee ($), count products > 1', 'woo_custom_shipping_method'),
'type' => 'text',
'description' => __('Shipping Fee for 2 products and more', 'woo_custom_shipping_method'),
'default' => 2
),
);
}
/**
* This function is used to calculate the shipping cost. Within this function we can check for weights, dimensions and other parameters.
*
* @access public
* @param mixed $package
* @return void
*/
public function calculate_shipping($package) {
$weight = 0;
$cost = 0;
$country = $package["destination"]["country"];
$cnt = 0;
foreach ($package['contents'] as $item_id => $values) {
$cnt += $values['quantity'];
}
$fee = $cnt > 1 ? $this->settings['woocustomshipping_price_2'] : $this->settings['woocustomshipping_price_1'];
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $fee
);
$this->add_rate($rate);
}
}
}
}
add_action('woocommerce_shipping_init', 'custom_shipping_method');
function add_custom_shipping_method($methods) {
$methods[] = 'Custom_Shipping_Method';
return $methods;
}
add_filter('woocommerce_shipping_methods', 'add_custom_shipping_method');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment