-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathmultiple-packages-shipping.php
86 lines (77 loc) · 3.1 KB
/
multiple-packages-shipping.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/*
Plugin Name: Packages Configuration for WooCommerce
Plugin URI: https://github.com/academe/wc-multiple-packages
Description: Configure how products are grouped into shipping packages for WooCommerce.
Author: Jason Judge [email protected]
Original Plugin Author: Erica Dion [email protected] (http://www.bolderelements.net/)
Author URI: https://github.com/judgej
Version: 1.2.5
Copyright: © 2014 Bolder Elements, © 2015 Academe Computing
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
if (is_multisite()) {
$activePlugins = get_site_option('active_sitewide_plugins', array());
$activePlugins = array_keys($activePlugins);
} else {
$activePlugins = apply_filters('active_plugins', get_option('active_plugins'));
}
/**
* Check if WooCommerce is active.
*/
if (in_array('woocommerce/woocommerce.php', $activePlugins)) {
if (!class_exists('Academe_Multiple_Packages')) {
// Include the main class.
// We will keep classes each defined in their own files.
include_once(dirname(__FILE__) . '/classes/Academe_Multiple_Packages.php');
}
// Add the filter to generate the packages.
// At the moment, this plugin will discard any packages already created and then
// generate its own from scratch. A future enhancement would see this plugin
// taking the existing packages, and perhaps splitting them down further if
// necessary, then adding the linking meta fields to the result.
add_filter(
'woocommerce_cart_shipping_packages',
array(Academe_Multiple_Packages::get_instance(), 'generate_packages')
);
// This action allows plugins to add order item meta to shipping.
add_action(
'woocommerce_add_shipping_order_item',
array(Academe_Multiple_Packages::get_instance(), 'link_shipping_line_item'),
10, 3
);
// Add shipping line meta fields to the shipping line in the order API.
add_filter(
'woocommerce_api_order_response',
array(Academe_Multiple_Packages::get_instance(), 'api_show_shipping_line_meta'),
10, 4
);
/**
* The settings are needed only when in the admin area.
*/
if (is_admin()) {
/**
* Define the shipping method.
*/
function academe_wc_multiple_packages_init()
{
if (!class_exists('Academe_Multiple_Packages_Settings')) {
// Include the settings and create a new instance.
require_once(dirname(__FILE__) . '/classes/Academe_Multiple_Packages_Settings.php');
}
}
add_action('woocommerce_shipping_init', 'academe_wc_multiple_packages_init');
/**
* Add the shipping method to the WC list of methods.
* It is not strictly a shipping method itself, but a tool for grouping other
* shipping methods.
*/
function academe_add_wc_multiple_packages($methods)
{
$methods[] = 'Academe_Multiple_Packages_Settings';
return $methods;
}
add_filter('woocommerce_shipping_methods', 'academe_add_wc_multiple_packages');
}
}