1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* SSI API Connector |
4
|
|
|
* |
5
|
|
|
* @package PrintCenter\SSI_API |
6
|
|
|
* @since 1.0.0 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
// Exit if accessed directly |
11
|
|
|
if( ! defined( 'ABSPATH' ) ) { |
12
|
|
|
exit; |
13
|
|
|
} |
14
|
|
|
|
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Main SSI_API class |
18
|
|
|
* |
19
|
|
|
* @since 1.0.0 |
20
|
|
|
*/ |
21
|
|
|
class SSI_API { |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @access public |
26
|
|
|
* @since 1.0.0 |
27
|
|
|
* @var string $shipping The defined shipping method |
28
|
|
|
*/ |
29
|
|
|
public $shipping = 'Priority Mail'; |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get things started |
34
|
|
|
* |
35
|
|
|
* @access public |
36
|
|
|
* @since 1.0.0 |
37
|
|
|
* @return void |
|
|
|
|
38
|
|
|
*/ |
39
|
|
|
public function __construct() { |
40
|
|
|
$this->hooks(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Run action and filter hooks |
46
|
|
|
* |
47
|
|
|
* @access public |
48
|
|
|
* @since 1.0.0 |
49
|
|
|
* @return void |
50
|
|
|
*/ |
51
|
|
|
public function hooks() { |
52
|
|
|
add_action( 'woocommerce_checkout_order_processed', array( $this, 'process_api_order' ), 10, 2 ); |
53
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'disable_woocommerce_checkout_scripts' ) ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Disable WooCommerce checkout scripts if test mode is active |
59
|
|
|
* |
60
|
|
|
* @access public |
61
|
|
|
* @since 1.0.0 |
62
|
|
|
* @return void |
63
|
|
|
*/ |
64
|
|
|
public function disable_woocommerce_checkout_scripts() { |
65
|
|
|
if( printcenter()->loader->settings->get_option( 'ssi_mode', 'live' ) == 'test' ) { |
66
|
|
|
wp_dequeue_script( 'wc-checkout' ); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Process new orders and send to API |
73
|
|
|
* |
74
|
|
|
* @access public |
75
|
|
|
* @since 1.0.0 |
76
|
|
|
* @param int $order_id The ID of this order |
77
|
|
|
* @param array $posted The data posted for the order |
78
|
|
|
* @return void |
79
|
|
|
*/ |
80
|
|
|
public function process_api_order( $order_id = 0, $posted ) { |
81
|
|
|
global $the_order; |
|
|
|
|
82
|
|
|
|
83
|
|
|
if( empty( $the_order ) || $the_order->id != $post->ID ) { |
|
|
|
|
84
|
|
|
$the_order = wc_get_order( $order_id ); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$ssi_mode = printcenter()->loader->settings->get_option( 'ssi_mode', 'live' ); |
88
|
|
|
|
89
|
|
|
if( $ssi_mode == 'capture' ) { |
90
|
|
|
$custid = printcenter()->loader->settings->get_option( 'ssi_test_custid', '1013' ); |
91
|
|
|
$custzip = printcenter()->loader->settings->get_option( 'ssi_test_custzip', '99999' ); |
92
|
|
|
$endpoint = 'https://orders.silkscreenink.com/capture.asp'; |
93
|
|
|
} elseif( $ssi_mode == 'test' ) { |
94
|
|
|
$custid = printcenter()->loader->settings->get_option( 'ssi_test_custid', '1013' ); |
95
|
|
|
$custzip = printcenter()->loader->settings->get_option( 'ssi_test_custzip', '99999' ); |
96
|
|
|
$endpoint = 'https://orders.silkscreenink.com/orderstest/default.asp'; |
97
|
|
|
} else { |
98
|
|
|
$custid = printcenter()->loader->settings->get_option( 'ssi_custid', '1024' ); |
99
|
|
|
$custzip = printcenter()->loader->settings->get_option( 'ssi_custzip', '80304' ); |
100
|
|
|
$endpoint = 'https://orders.silkscreenink.com/orderslive/'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$ssi_order = array( |
104
|
|
|
'DocType' => 'Order', |
105
|
|
|
'GarmentsProvided' => 'No', |
106
|
|
|
'CustID' => $custid, |
107
|
|
|
'CustZip' => $custzip, |
108
|
|
|
'PO' => $order_id, |
109
|
|
|
'ShipTo' => array( |
110
|
|
|
'FirstName' => ( $posted['billing_first_name'] ? $posted['billing_first_name'] : '' ), |
111
|
|
|
'LastName' => ( $posted['billing_last_name'] ? $posted['billing_last_name'] : '' ), |
112
|
|
|
'Adrx1' => ( $posted['billing_address_1'] ? $posted['billing_address_1'] : '' ), |
113
|
|
|
'City' => ( $posted['billing_city'] ? $posted['billing_city'] : '' ), |
114
|
|
|
'State' => ( $posted['billing_state'] ? $posted['billing_state'] : '' ), |
115
|
|
|
'Zip' => ( $posted['billing_postcode'] ? $posted['billing_postcode'] : '' ), |
116
|
|
|
'Country' => ( $posted['billing_country'] ? $posted['billing_country'] : '' ), |
117
|
|
|
'Email' => ( $posted['billing_email'] ? $posted['billing_email'] : '' ), |
118
|
|
|
'Phone' => ( $posted['billing_phone'] ? $posted['billing_phone'] : '' ), |
119
|
|
|
), |
120
|
|
|
'ShipMethod' => $this->shipping, |
121
|
|
|
'ProductionPriority' => 'Normal', |
122
|
|
|
); |
123
|
|
|
|
124
|
|
|
foreach( $the_order->get_items() as $item ) { |
125
|
|
|
$product = $the_order->get_product_from_item( $item ); |
126
|
|
|
$item_meta = new WC_Order_Item_Meta( $item, $product ); |
127
|
|
|
$item_meta = $item_meta->get_formatted(); |
128
|
|
|
|
129
|
|
|
foreach( $item_meta as $item_id => $meta ) { |
130
|
|
|
switch( $meta['key'] ) { |
131
|
|
|
case 'pa_size' : |
132
|
|
|
$size = $meta['value']; |
133
|
|
|
break; |
134
|
|
|
case 'pa_color' : |
135
|
|
|
$color = $meta['value']; |
136
|
|
|
break; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$sku = get_post_meta( $item['product_id'] , '_ssi_sku', true ); |
141
|
|
|
$location = get_post_meta( $item['product_id'] , '_ssi_location', true ); |
142
|
|
|
$sizing = get_post_meta( $item['product_id'] , '_ssi_sizing', true ); |
143
|
|
|
$art = get_post_meta( $item['product_id'] , '_ssi_art', true ); |
144
|
|
|
$thumb = wp_get_attachment_image_src( get_post_thumbnail_id( $item['product_id'] ), 'full' ); |
145
|
|
|
$thumb = $thumb[0]; |
146
|
|
|
|
147
|
|
|
if( $location == 'front' ) { |
148
|
|
|
$location = 'Full Front'; |
149
|
|
|
} elseif( $location == 'back' ) { |
150
|
|
|
$location = 'Full Back'; |
151
|
|
|
} else { |
152
|
|
|
$location = 'Left Chest'; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$ssi_order['Item'][] = array( |
156
|
|
|
'SKU' => $sku, |
157
|
|
|
'Color' => ( isset( $color ) ? $color : false ), |
158
|
|
|
'Size' => ( isset( $size ) ? $size : false ), |
159
|
|
|
'Qty' => $item['qty'], |
160
|
|
|
'DesignLocation' => $location, |
161
|
|
|
'DesignType' => 1, |
162
|
|
|
'DesignArt' => ( isset( $art ) ? $art : false ), |
163
|
|
|
'DesignThumb' => $thumb, |
164
|
|
|
'DesignCategory' => $sizing |
165
|
|
|
); |
166
|
|
|
|
167
|
|
|
$xml = Array2XML::createXML( 'Request', $ssi_order ); |
168
|
|
|
$xml = $xml->saveXML(); |
169
|
|
|
|
170
|
|
|
$content = array( |
171
|
|
|
'headers' => array( |
172
|
|
|
'content-type' => 'text/xml' |
173
|
|
|
), |
174
|
|
|
'body' => $xml |
175
|
|
|
); |
176
|
|
|
|
177
|
|
|
$response = wp_remote_post( $endpoint, $content ); |
178
|
|
|
|
179
|
|
|
if( $ssi_mode == 'test' ) { |
180
|
|
|
$response = wp_remote_retrieve_body( $response ); |
181
|
|
|
|
182
|
|
|
echo '<pre>' . printcenter_prettify_xml( $response, true ) . '</pre>'; |
183
|
|
|
exit; |
|
|
|
|
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
} |
187
|
|
|
} |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.