Printful Integration for WooCommerce - Version 1.2

Version Description

Support calculating shipping rates for both Printful and non-Printful products at the same time

Download this release

Release Info

Developer printful
Plugin Icon 128x128 Printful Integration for WooCommerce
Version 1.2
Comparing to
See all releases

Code changes from version 1.1.2 to 1.2

includes/class-printful-integration.php CHANGED
@@ -121,24 +121,30 @@ class Printful_Integration extends WC_Integration
121
$countries = $this->get_tax_countries();
122
if(isset($countries[$country][$state]))
123
{
124
- $rate = false;
125
- try {
126
- $client = $this->get_client();
127
- $response = $client->post('tax/rates', array(
128
- 'recipient' => array(
129
- 'country_code' => $country,
130
- 'state_code' => $state,
131
- 'city' => $city,
132
- 'zip' => $postcode,
133
- )
134
- ));
135
- }
136
- catch(Exception $e)
137
- {}
138
139
- if(isset($response['rate']))
140
- {
141
- $rate = $response['rate'];
142
}
143
144
if ($rate)
@@ -173,7 +179,6 @@ class Printful_Integration extends WC_Integration
173
174
foreach($list as $country)
175
{
176
- $list[$country['country']] = array();
177
foreach($country['states'] as $state){
178
$countries[$country['code']][$state['code']] = 1;
179
}
121
$countries = $this->get_tax_countries();
122
if(isset($countries[$country][$state]))
123
{
124
+ $key = 'printful_tax_' . $country.'-'.$state.'-'.$city.'-'.$postcode;
125
+ $rate = get_transient($key);
126
+ if ($rate === false) {
127
+ try {
128
+ $client = $this->get_client();
129
+ $response = $client->post('tax/rates', array(
130
+ 'recipient' => array(
131
+ 'country_code' => $country,
132
+ 'state_code' => $state,
133
+ 'city' => $city,
134
+ 'zip' => $postcode,
135
+ )
136
+ ));
137
+ }
138
+ catch(Exception $e)
139
+ {}
140
141
+ if(isset($response['rate']))
142
+ {
143
+ $rate = $response['rate'];
144
+ } else {
145
+ $rate = 0;
146
+ }
147
+ set_transient($key, $rate, 1800);
148
}
149
150
if ($rate)
179
180
foreach($list as $country)
181
{
182
foreach($country['states'] as $state){
183
$countries[$country['code']][$state['code']] = 1;
184
}
includes/class-printful-shipping.php CHANGED
@@ -7,6 +7,9 @@ class Printful_Shipping extends WC_Shipping_Method
7
public $calculate_tax = false;
8
private $last_error = false;
9
10
public function __construct()
11
{
12
$this->id = 'printful_shipping';
@@ -21,7 +24,14 @@ class Printful_Shipping extends WC_Shipping_Method
21
$this->enabled = $this->get_option('enabled');
22
$this->show_warnings = $this->get_option('show_warnings') == 'yes';
23
24
- $this->type = 'order';
25
}
26
27
public function generate_settings_html($form_fields = false)
@@ -58,10 +68,116 @@ class Printful_Shipping extends WC_Shipping_Method
58
'type' => 'checkbox',
59
'label' => 'Display Printful status messages if rate API request fails',
60
'default' => 'yes'
61
- ),
62
);
63
}
64
65
public function calculate_shipping($package = array())
66
{
67
$request = array(
@@ -83,7 +199,8 @@ class Printful_Shipping extends WC_Shipping_Method
83
}
84
$request['items'] []= array(
85
'external_variant_id' => $item['variation_id'] ? $item['variation_id'] : $item['product_id'],
86
- 'quantity' => $item['quantity']
87
);
88
}
89
@@ -99,9 +216,15 @@ class Printful_Shipping extends WC_Shipping_Method
99
}
100
101
try {
102
- $response = $client->post('shipping/rates', $request, array(
103
- 'expedited' => true,
104
- ));
105
106
foreach ($response as $rate) {
107
$rateData = array(
7
public $calculate_tax = false;
8
private $last_error = false;
9
10
+ //Store whether currently processed package contains Printful products (for WC<2.6)
11
+ private $printful_package = true;
12
+
13
public function __construct()
14
{
15
$this->id = 'printful_shipping';
24
$this->enabled = $this->get_option('enabled');
25
$this->show_warnings = $this->get_option('show_warnings') == 'yes';
26
27
+ //Initialize shipping methods for specific package (or no package)
28
+ add_filter('woocommerce_load_shipping_methods', array($this, 'woocommerce_load_shipping_methods'), 10000);
29
+
30
+ //Remove other shipping methods for Printful package on WC < 2.6
31
+ add_filter('woocommerce_shipping_methods', array($this, 'woocommerce_shipping_methods'), 10000);
32
+
33
+ add_filter('woocommerce_cart_shipping_packages', array($this, 'woocommerce_cart_shipping_packages'));
34
+
35
}
36
37
public function generate_settings_html($form_fields = false)
68
'type' => 'checkbox',
69
'label' => 'Display Printful status messages if rate API request fails',
70
'default' => 'yes'
71
+ )
72
);
73
}
74
75
+ //Enable only Printful shipping method for Printful packages
76
+ public function woocommerce_load_shipping_methods( $package = array() ) {
77
+ $this->printful_package = false;
78
+ if($package && !empty($package['printful'])) {
79
+ if($this->enabled == 'yes') {
80
+ $this->printful_package = true;
81
+ //Remove default methods if we process Printful package
82
+ WC()->shipping()->unregister_shipping_methods();
83
+ WC()->shipping()->register_shipping_method($this);
84
+ }
85
+ } else if(!$package) {
86
+ //Show Printful tab on Shipping rate settings
87
+ WC()->shipping()->register_shipping_method($this);
88
+ }
89
+ }
90
+
91
+ //Remove non-Printful methods for Printful packages on WC < 2.6
92
+ public function woocommerce_shipping_methods($methods) {
93
+ if($this->printful_package && version_compare(WC()->version, '2.6', '<')) {
94
+ //For WC < 2.6 woocommerce_shipping_methods is executed after woocommerce_load_shipping_methods
95
+ //So we need to clean up unnecessary methods from there
96
+ return array();
97
+ }
98
+ return $methods;
99
+ }
100
+
101
+ //Split Printul products to a separate package if there are any
102
+ public function woocommerce_cart_shipping_packages($packages = array()) {
103
+
104
+ //Printful rates are turned off, do not split products
105
+ if ($this->enabled !== 'yes') {
106
+ return $packages;
107
+ }
108
+
109
+ $return_packages = array();
110
+
111
+ foreach ($packages as $package) {
112
+
113
+ $ids = array();
114
+ foreach ($package['contents'] as $key => $item) {
115
+ $ids[$key]= $item['variation_id'] ? $item['variation_id'] : $item['product_id'];
116
+ }
117
+
118
+ $printful_ids = array();
119
+ if($ids) {
120
+ asort($ids);
121
+ $values = implode(',', array_unique($ids));
122
+ $key = 'printful_productids_'.md5($values);
123
+ $printful_ids = get_transient($key);
124
+ if ($printful_ids === false) {
125
+ try {
126
+ $client = Printful_Integration::instance()->get_client();
127
+ $status = $client->get('sync/variants', array(
128
+ 'external_ids' => $values
129
+ ));
130
+ if(!empty($status['sync_variants'])) {
131
+ foreach($status['sync_variants'] as $variant) {
132
+ if($variant['synced']) {
133
+ $printful_ids[]= $variant['external_id'];
134
+ }
135
+ }
136
+ }
137
+ set_transient($key, $printful_ids, 1800);
138
+ } catch (PrintfulException $e) {
139
+ $this->set_error($e);
140
+ //Failed to get Printful status, return default packages
141
+ return $packages;
142
+ }
143
+ }
144
+ }
145
+ $new_contents = array(
146
+ 'printful' => array(),
147
+ 'woocommerce' => array()
148
+ );
149
+
150
+ foreach($ids as $key => $external_id) {
151
+ if(in_array($external_id,$printful_ids)) {
152
+ $new_contents['printful'][$key] = $package['contents'][$key];
153
+ } else {
154
+ $new_contents['woocommerce'][$key] = $package['contents'][$key];
155
+ }
156
+ }
157
+
158
+ foreach ($new_contents as $key => $contents) {
159
+ if($contents) {
160
+ $new_package = $package;
161
+ $new_package['contents_cost'] = 0;
162
+ $new_package['contents'] = $contents;
163
+ foreach ($contents as $item ) {
164
+ if ( $item['data']->needs_shipping() ) {
165
+ if ( isset( $item['line_total'] ) ) {
166
+ $new_package['contents_cost'] += $item['line_total'];
167
+ }
168
+ }
169
+ }
170
+ if ($key == 'printful') {
171
+ $new_package['printful'] = true;
172
+ }
173
+ $return_packages[]= $new_package;
174
+ }
175
+ }
176
+ }
177
+ return $return_packages;
178
+ }
179
+
180
+
181
public function calculate_shipping($package = array())
182
{
183
$request = array(
199
}
200
$request['items'] []= array(
201
'external_variant_id' => $item['variation_id'] ? $item['variation_id'] : $item['product_id'],
202
+ 'quantity' => $item['quantity'],
203
+ 'value' => $item['line_total'] / $item['quantity']
204
);
205
}
206
216
}
217
218
try {
219
+ $key = 'printful_rates_' . md5(json_encode($request));
220
+ $response = get_transient($key);
221
+ if($response === false) {
222
+ $response = $client->post('shipping/rates', $request, array(
223
+ 'expedited' => true,
224
+ ));
225
+ //Cache locally, since WC < 2.6 had problems with caching rates form ultiple packages internally
226
+ set_transient($key, $response, 1800);
227
+ }
228
229
foreach ($response as $rate) {
230
$rateData = array(
printful-shipping.php CHANGED
@@ -3,7 +3,7 @@
3
Plugin Name: Printful Integration for WooCommerce
4
Plugin URI: https://wordpress.org/plugins/printful-shipping-for-woocommerce/
5
Description: Calculate correct shipping and tax rates for your Printful-Woocommerce integration.
6
- Version: 1.1.2
7
Author: Printful
8
Author URI: http://www.theprintful.com
9
License: GPL2 http://www.gnu.org/licenses/gpl-2.0.html
@@ -16,7 +16,7 @@ new Printful_Base();
16
17
class Printful_Base {
18
19
- const VERSION = '1.1.2';
20
21
/**
22
* Construct the plugin.
@@ -34,18 +34,11 @@ class Printful_Base {
34
//Register integration section
35
add_filter('woocommerce_integrations', array($this, 'add_integration'));
36
37
- //Register shipping method
38
- add_filter('woocommerce_shipping_methods', array($this, 'add_shipping'));
39
-
40
//Register API endpoint
41
add_filter('woocommerce_api_classes', array($this, 'add_api_resource'));
42
43
- }
44
-
45
- public function add_shipping( $methods ) {
46
require_once 'includes/class-printful-shipping.php';
47
- $methods[] = 'Printful_Shipping';
48
- return $methods;
49
}
50
51
public function add_integration( $integrations ) {
3
Plugin Name: Printful Integration for WooCommerce
4
Plugin URI: https://wordpress.org/plugins/printful-shipping-for-woocommerce/
5
Description: Calculate correct shipping and tax rates for your Printful-Woocommerce integration.
6
+ Version: 1.2
7
Author: Printful
8
Author URI: http://www.theprintful.com
9
License: GPL2 http://www.gnu.org/licenses/gpl-2.0.html
16
17
class Printful_Base {
18
19
+ const VERSION = '1.2';
20
21
/**
22
* Construct the plugin.
34
//Register integration section
35
add_filter('woocommerce_integrations', array($this, 'add_integration'));
36
37
//Register API endpoint
38
add_filter('woocommerce_api_classes', array($this, 'add_api_resource'));
39
40
require_once 'includes/class-printful-shipping.php';
41
+ new Printful_Shipping();
42
}
43
44
public function add_integration( $integrations ) {
readme.txt CHANGED
@@ -2,8 +2,8 @@
2
Contributors: girts_u
3
Tags: woocommerce, printful, drop shipping, shipping, shipping rates, fulfillment, printing, fedex, carriers, checkout, t-shirts
4
Requires at least: 3.8
5
- Tested up to: 4.3
6
- Stable tag: 1.1.2
7
License: GPLv2 or later
8
License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
@@ -18,7 +18,6 @@ This plugin will also automatically calculate taxes where it is required for Pri
18
= Known Limitations =
19
20
* Works with WooCommmerce 2.1 and up
21
- * Works only if every item in the order is fulfilled by Printful
22
23
== Installation ==
24
1. Upload 'printful-shipping' to the '/wp-content/plugins/' directory
@@ -42,6 +41,9 @@ Go to https://www.theprintful.com/dashboard/store , select your WooCommerce stor
42
43
== Upgrade Notice ==
44
45
= 1.1.2 =
46
Removed check for Curl extension
47
@@ -62,6 +64,12 @@ First release
62
63
== Changelog ==
64
65
= 1.1.2 =
66
* Removed check for Curl extension (since we already used wp_remote_get and it is no longer necessary)
67
2
Contributors: girts_u
3
Tags: woocommerce, printful, drop shipping, shipping, shipping rates, fulfillment, printing, fedex, carriers, checkout, t-shirts
4
Requires at least: 3.8
5
+ Tested up to: 4.5.2
6
+ Stable tag: 1.2
7
License: GPLv2 or later
8
License URI: http://www.gnu.org/licenses/gpl-2.0.html
9
18
= Known Limitations =
19
20
* Works with WooCommmerce 2.1 and up
21
22
== Installation ==
23
1. Upload 'printful-shipping' to the '/wp-content/plugins/' directory
41
42
== Upgrade Notice ==
43
44
+ = 1.2 =
45
+ Support calculating shipping rates for both Printful and non-Printful products at the same time
46
+
47
= 1.1.2 =
48
Removed check for Curl extension
49
64
65
== Changelog ==
66
67
+ = 1.2 =
68
+ * Support calculating shipping rates for both Printful and non-Printful products at the same time (non-Printful
69
+ products will get default rates provided by Woocommerce)
70
+ * Added caching to tax rates
71
+ * Improved compatibility with Woocommerce 2.6
72
+
73
= 1.1.2 =
74
* Removed check for Curl extension (since we already used wp_remote_get and it is no longer necessary)
75