Skip to content

Instantly share code, notes, and snippets.

@mikemanger
Forked from VjWoo/th_shipping_options.php
Last active March 12, 2018 15:50
Show Gist options
  • Save mikemanger/995738b7ac6498e47a30c111d2d1fbd5 to your computer and use it in GitHub Desktop.
Save mikemanger/995738b7ac6498e47a30c111d2d1fbd5 to your computer and use it in GitHub Desktop.
Shipping Options plugin
<?php
/*
Plugin Name: TH Shipping Options
Plugin URI: NA
Description: TH Shipping Options plugin
Version: 1.0.0
Author: Vj
Author URI: www.wooforce.com
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'TH_Shipping_Options' ) ) {
class TH_Shipping_Options {
/**
* Constructor for your shipping class
*
* @access public
* @return void
*/
public function __construct() {
// take care of anything else that needs to be done immediately upon plugin instantiation, here in the constructor.
add_filter( 'woocommerce_cart_shipping_packages', array( &$this, 'cart_shipping_packages') );
add_filter( 'woocommerce_shipping_package_name', array( &$this, 'shipping_package_name'), 10, 3 );
}
function cart_shipping_packages( $packages ) {
// Reset the packages
$packages = array();
$vendor_items_map = array();
foreach ( WC()->cart->get_cart() as $item ) {
$product_id = $item['product_id'];
if ( $item['data']->needs_shipping() ) {
$vendor_id = WC_Product_Vendors_Utils::get_vendor_id_from_product( $product_id );
if ( $vendor_id ) {
$vendor_items_map[ $vendor_id ][] = $item;
}
// No product vendor associated with item.
else {
$vendor_items_map['0'][] = $item;
}
}
}
foreach ( $vendor_items_map as $key => $vendor_items ) {
$data = array(
'vendor_id' => $key,
'contents' => $vendor_items,
'contents_cost' => array_sum( wp_list_pluck( $vendor_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(),
)
);
// In future we could specifiy different methods here
/*
if ( $key == 97 ) {
$data['ship_via'] = array( 'flat_rate', 'free_shipping' );
}
*/
$packages[] = $data;
}
return $packages;
}
function shipping_package_name( $text, $i, $package ) {
if ( ! empty( $package['vendor_id'] ) ) {
$vendor_data = WC_Product_Vendors_Utils::get_vendor_data_by_id( $package['vendor_id'] );
$text = 'Shipping options for ' . $vendor_data['name'];
}
return $text;
}
}
// 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
function TH_Shipping_Options() {
global $TH_Shipping_Options;
$TH_Shipping_Options = new TH_Shipping_Options();
}
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
add_action( 'init', 'TH_Shipping_Options' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment