forked from ChromeOrange/WooCommerce-Free-Shipping-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
woocommerce-free-shipping-plus.php
162 lines (113 loc) · 6.87 KB
/
woocommerce-free-shipping-plus.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
Plugin Name: WooCommerce Free Shipping Plus
Plugin URI: http://codeeveryday.co/woocommerce-free-shipping-plus/
Description: Improves on WooCommerce Free Shipping.
Version: 0.1.0
Author: Andrew Benbow
Author URI: http://codeeveryday.co/
Text Domain: wc-freeshipping-plus
Domain Path: /languages/
*/
/*
Copyright 2012 Andrew Benbow (email : [email protected])
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( is_woocommerce_active() ) {
/**
* Localisation
*/
load_plugin_textdomain( 'wc-freeshipping-plus', false, dirname( plugin_basename( __FILE__ ) ) . '/' );
/**
* woocommerce_product_gallery_slider class
*/
if ( ! class_exists( 'WC_Free_Shipping_Plus' ) ) {
class WC_Free_Shipping_Plus {
public function __construct() {
add_action( 'woocommerce_product_options_shipping', array( $this, 'write_panel' ), 99 );
add_action( 'woocommerce_process_product_meta', array( $this, 'write_panel_save' ) );
add_filter( 'woocommerce_package_rates', array( $this, 'unset_free_shipping_method' ) , 10, 2 );
}
/**
* Create new fields in Shipping section of Product
*
* Add them at the bottom.
*/
function write_panel() {
echo '<div class="options_group">';
woocommerce_wp_select( array( 'id' => '_woocommerce_product_exclude_from_free_shipping',
'label' => __('Exclude this product from the WooCommerce Free Shipping method', 'wc-freeshipping-plus'),
'desc_tip' => true,
'description' => __('This option will remove Free Shipping from the available shipping methods if this product is in the cart. Only works with the WooCommerce Free Shipping method.', 'wc_product_gallery_slider'),
'options' => array(
'no' => __( 'No', 'wc-freeshipping-plus' ),
'yes' => __( 'Yes', 'wc-freeshipping-plus' )
)
)
);
woocommerce_wp_text_input( array(
'id' => '_woocommerce_product_exclude_from_free_shipping_states',
'label' => __( 'Exclude Free Shipping in these US States', 'wc-freeshipping-plus' ),
'desc_tip' => true,
'description' => __( 'If you want to exclude this product from the WooCommerce Free Shipping method but only in certain US states then tick the option above and enter the US State codes. eg for Hawaii add HI, for Hawaii and Alaska add HI,AK', 'wc-freeshipping-plus' ),
'type' => 'text'
) );
echo '</div>';
} // write_panel
/**
* Save shipping
* @param [type] $post_id [description]
* @return [type] [description]
*/
function write_panel_save( $post_id ) {
$woocommerce_product_exclude_from_free_shipping = esc_attr( $_POST['_woocommerce_product_exclude_from_free_shipping'] );
$woocommerce_product_exclude_from_free_shipping_states = esc_attr( $_POST['_woocommerce_product_exclude_from_free_shipping_states'] );
update_post_meta( $post_id, '_woocommerce_product_exclude_from_free_shipping', $woocommerce_product_exclude_from_free_shipping );
update_post_meta( $post_id, '_woocommerce_product_exclude_from_free_shipping_states', $woocommerce_product_exclude_from_free_shipping_states );
} // write_panel_save
/**
* Check the cart for specific products, remove Free Shipping method if they are present
*/
function unset_free_shipping_method( $rates, $package ) {
/**
* loop through the cart checking for _woocommerce_product_exclude_from_free_shipping meta field
*/
foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
if( get_post_meta( $values['product_id'], '_woocommerce_product_exclude_from_free_shipping', TRUE ) == 'yes' ) {
$woocommerce_product_exclude_from_free_shipping_states = get_post_meta( $values['product_id'], '_woocommerce_product_exclude_from_free_shipping_states', TRUE );
if ( isset($woocommerce_product_exclude_from_free_shipping_states) && $woocommerce_product_exclude_from_free_shipping_states != '' ) {
$states_array = explode( ',', $woocommerce_product_exclude_from_free_shipping_states );
if ( in_array( WC()->customer->get_shipping_state(), $states_array ) ) {
/**
* Unset the shipping methods here
*/
unset( $rates['free_shipping'] );
// The rates have been removed, no point in carrying on
break;
}
} else {
/**
* Unset the shipping methods here
*/
unset( $rates['free_shipping'] );
// The rates have been removed, no point in carrying on
break;
}
}
}
// Return what's left of the $rates array
return $rates;
} // unset_free_shipping_method
} // WC_Free_Shipping_Plus
$WC_Free_Shipping_Plus = new WC_Free_Shipping_Plus();
} // if ( ! class_exists( 'WC_Free_Shipping_Plus' ) )
} // is_woocommerce_active