Created
September 28, 2015 01:09
-
-
Save boywondercreative/9877f19fde6b25addef8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: HAZMAT Shipping Options | |
Plugin URI: | |
Description: HAZMAT Shipping Options plugin | |
Version: 1.0.0 | |
Author: sc | |
Author URI: | |
*/ | |
//define("PV_ATTRIBUTE", "vendor"); | |
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly | |
/** | |
* Check if WooCommerce is active | |
*/ | |
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) { | |
if ( ! class_exists( 'TH_Shipping_Options' ) ) { | |
class TH_Shipping_Options { | |
/** | |
* Constructor for your shipping class | |
* | |
* @access public | |
* @return void | |
*/ | |
public function __construct() { | |
add_filter( 'woocommerce_cart_shipping_packages', array( &$this, 'th_woocommerce_cart_shipping_packages') ); | |
// Overriding template to introduce vendor names along with standard labels across shipping packages. | |
add_filter( 'woocommerce_locate_template', array( $this, 'th_woocommerce_locate_template' ), 10, 3 ); | |
// | |
add_filter( 'woocommerce_package_rates', 'hide_shipping_for_shipping_class', 10, 3 ); | |
// | |
add_action( 'woocommerce_cart_calculate_fees', 'wc_fee_per_shipping_class_item' ); | |
add_action( 'woocommerce_after_cart_item_quantity_update', 'wc_fee_per_shipping_class_item' ); | |
} | |
function th_woocommerce_locate_template( $template, $template_name, $template_path ) { | |
if('cart/cart-shipping.php' == $template_name) | |
{ | |
$path = plugin_dir_path( __FILE__ ) . '/woocommerce/templates/' . $template_name; | |
return file_exists( $path ) ? $path : $template; | |
} | |
return $template; | |
} | |
function th_woocommerce_cart_shipping_packages( $packages ) { | |
// Reset the packages | |
$packages = array(); | |
// Bulky items | |
$bulky_items = array(); | |
$regular_items = array(); | |
// Sort bulky from regular | |
foreach ( WC()->cart->get_cart() as $item ) { | |
if ( $item['data']->needs_shipping() ) { | |
if ( $item['data']->get_shipping_class() == 'hazmat' ) { | |
$bulky_items[] = $item; | |
} else { | |
$regular_items[] = $item; | |
} | |
} | |
} | |
// Put inside packages | |
if ( $regular_items ) { | |
$packages[] = array( | |
'contents' => $regular_items, | |
'contents_cost' => array_sum( wp_list_pluck( $regular_items, 'line_total' ) ), | |
'applied_coupons' => WC()->cart->applied_coupons, | |
'destination' => array( | |
'country' => WC()->customer->get_shipping_country(), | |
'state' => WC()->customer->get_shipping_state(), | |
'postcode' => WC()->customer->get_shipping_postcode(), | |
'city' => WC()->customer->get_shipping_city(), | |
'address' => WC()->customer->get_shipping_address(), | |
'address_2' => WC()->customer->get_shipping_address_2() | |
) | |
); | |
} | |
if ( $bulky_items ) { | |
$packages[] = array( | |
'ship_via' => array( 'fedex','ups' ), | |
'contents' => $bulky_items, | |
'contents_cost' => array_sum( wp_list_pluck( $bulky_items, 'line_total' ) ), | |
'applied_coupons' => WC()->cart->applied_coupons, | |
'destination' => array( | |
'country' => WC()->customer->get_shipping_country(), | |
'state' => WC()->customer->get_shipping_state(), | |
'postcode' => WC()->customer->get_shipping_postcode(), | |
'city' => WC()->customer->get_shipping_city(), | |
'address' => WC()->customer->get_shipping_address(), | |
'address_2' => WC()->customer->get_shipping_address_2() | |
) | |
); | |
} | |
return $packages; | |
} | |
} | |
// Hide shipping rates for HAZMAT shipping | |
function hide_shipping_for_shipping_class( $rates, $package ) { | |
// echo "<pre>" . print_r( $rates, true ) . "</pre> ";// printing the methods so I know the available methods to exclude | |
// foreach ( WC()->cart->cart_contents AS $item ) { | |
// if ( has_term( 'hazmat', 'product_shipping_class', $item['product_id'] ) ) { | |
foreach ( WC()->cart->get_cart() as $item ) { | |
if ( $item['data']->needs_shipping() ) { | |
if ( $item['data']->get_shipping_class() == 'hazmat' ) { | |
unset($rates['ups:01']); | |
unset($rates['ups:02']); | |
// unset($rates['ups:03']); | |
unset($rates['ups:59']); | |
// unset($rates['fedex:FEDEX_GROUND']); | |
unset($rates['fedex:FEDEX_EXPRESS_SAVER']); | |
unset($rates['fedex:FEDEX_2_DAY_AM']); | |
unset($rates['fedex:FIRST_OVERNIGHT']); | |
unset($rates['fedex:PRIORITY_OVERNIGHT']); | |
unset($rates['fedex:FEDEX_2_DAY']); | |
unset($rates['fedex:STANDARD_OVERNIGHT']); | |
} | |
} | |
} | |
return $rates; | |
} | |
// | |
function wc_fee_per_shipping_class_item() { | |
global $woocommerce; | |
// Setup an array of shipping classes which correspond to those created in Woocommerce | |
$shippingclass_hazmat = array( 'hazmat' ); | |
$spfee = 0.00; // initialize special fee | |
$spfeeperprod = 28.50; //special fee per product | |
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) { | |
$shipping_class = get_the_terms( $values['product_id'], 'product_shipping_class' ); | |
$qty += $values['quantity']; | |
$spfee = $qty * $spfeeperprod; | |
if ( isset( $shipping_class[0]->slug ) && in_array( $shipping_class[0]->slug, $shippingclass_hazmat ) ) { | |
$woocommerce->cart->add_fee( 'HAZMAT Fee*', $spfee, true, 'standard' ); | |
} | |
} | |
} | |
// finally instantiate our plugin class and add it to the set of globals | |
$GLOBALS['th_shipping_options_init'] = new TH_Shipping_Options(); | |
} | |
// Start up this plugin | |
add_action( 'init', 'TH_Shipping_Options' ); | |
function TH_Shipping_Options() { | |
global $TH_Shipping_Options; | |
$TH_Shipping_Options = new TH_Shipping_Options(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment