PluginProbe ʕ •ᴥ•ʔ
TaxCloud for WooCommerce / 8.4.11
TaxCloud for WooCommerce v8.4.11
8.4.11 8.4.10 8.4.9 trunk 6.0.11 6.0.12 6.0.13 6.0.14 6.1.0 6.1.1 6.1.2 6.2.0 6.2.1 6.2.2 6.2.3 6.2.4 6.2.5 6.2.6 6.3.0 6.3.1 6.3.10 6.3.11 6.3.12 6.3.13 6.3.2 6.3.3 6.3.4 6.3.5 6.3.6 6.3.7 6.3.8 6.3.9 7.0.0 7.0.1 7.0.10 7.0.11 7.0.12 7.0.13 7.0.2 7.0.3 7.0.4 7.0.5 7.0.6 7.0.7 7.0.8 7.0.9 8.0.0 8.0.1 8.0.10 8.0.11 8.0.12 8.0.13 8.0.14 8.0.15 8.0.16 8.0.17 8.0.2 8.0.3 8.0.4 8.0.5 8.0.6 8.0.7 8.0.8 8.0.9 8.1.0 8.1.1 8.2.0 8.2.1 8.2.2 8.2.3 8.2.4 8.3.0 8.3.1 8.3.2 8.3.3 8.3.4 8.3.5 8.3.6 8.3.7 8.3.8 8.4.0 8.4.1 8.4.2 8.4.3 8.4.4 8.4.5 8.4.6 8.4.7 8.4.8
simple-sales-tax / includes / class-sst-order-controller.php
simple-sales-tax / includes Last commit date
abstracts 3 months ago admin 3 months ago frontend 1 month ago integrations 1 month ago v3 3 months ago vendor 6 days ago views 8 months ago class-simplesalestax.php 6 days ago class-sst-addresses.php 1 month ago class-sst-ajax.php 3 months ago class-sst-assets.php 6 months ago class-sst-blocks-integration.php 1 month ago class-sst-blocks.php 1 year ago class-sst-certificates.php 6 days ago class-sst-install.php 6 months ago class-sst-logger.php 5 months ago class-sst-marketplaces.php 6 months ago class-sst-order-controller.php 3 months ago class-sst-order.php 1 month ago class-sst-origin-address.php 8 months ago class-sst-product.php 3 months ago class-sst-rate-limit.php 5 months ago class-sst-settings.php 3 months ago class-sst-shipping.php 3 years ago class-sst-taxcloud-v3-api.php 3 months ago class-sst-taxcloud-v3.php 3 months ago class-sst-tic.php 2 years ago class-sst-updater.php 3 years ago sst-compatibility-functions.php 4 months ago sst-functions.php 6 days ago sst-message-functions.php 3 years ago sst-update-functions.php 11 months ago
class-sst-order-controller.php
291 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly.
5 }
6
7 /**
8 * Order Controller.
9 *
10 * Handles requests related to orders.
11 *
12 * @author Simple Sales Tax
13 * @package SST
14 * @since 5.0
15 */
16 class SST_Order_Controller {
17
18 /**
19 * Constructor.
20 *
21 * @since 5.0
22 */
23 public function __construct() {
24 add_action( 'woocommerce_order_status_completed', array( $this, 'capture_order' ), 10, 2 );
25 add_action( 'woocommerce_refund_created', array( $this, 'refund_order' ), 10, 2 );
26 add_action( 'woocommerce_payment_complete', array( $this, 'maybe_capture_order' ) );
27 add_filter( 'woocommerce_hidden_order_itemmeta', array( $this, 'hide_order_item_meta' ) );
28 add_filter( 'woocommerce_order_item_get_taxes', array( $this, 'fix_shipping_tax_issue' ), 10, 2 );
29 add_action( 'woocommerce_before_order_object_save', array( $this, 'save_db_version' ) );
30 add_action(
31 'woocommerce_order_after_calculate_totals',
32 array( $this, 'calculate_order_tax' ),
33 10,
34 2
35 );
36 add_filter(
37 'woocommerce_order_data_store_cpt_get_orders_query',
38 array( $this, 'handle_version_query_var' ),
39 10,
40 2
41 );
42 add_action( 'woocommerce_process_shop_order_meta', array( $this, 'save_certficiate' ) );
43 add_filter(
44 'woocommerce_order_hide_zero_taxes',
45 array( $this, 'filter_hide_zero_taxes' )
46 );
47 }
48
49 /**
50 * When an order is completed, mark it as captured in TaxCloud.
51 *
52 * @param int $_order_id Order ID - unused.
53 * @param WC_Order $order Order object.
54 *
55 * @return bool True on success, false on failure.
56 *
57 * @throws Exception If capture fails.
58 * @since 5.0
59 */
60 public function capture_order( $_order_id, $order ) {
61 // Disable integration
62 if ( 'yes' === SST_Settings::get( 'disable_integration' ) ) {
63 SST_Logger::order_log( __( 'Integration disabled. Skipping order capture.', 'simple-sales-tax' ), $order->get_id() );
64 return false;
65 }
66
67 $sst_order = new SST_Order( $order );
68
69 // Logging
70 SST_Logger::order_log( __( 'order_status_completed: Capturing order.', 'simple-sales-tax' ), $order->get_id() );
71
72 return $sst_order->do_capture();
73 }
74
75 /**
76 * When a new refund is created, send a Returned request to TaxCloud.
77 *
78 * @param int $refund_id ID of new refund.
79 * @param array $args Refund arguments (see wc_create_refund()).
80 *
81 * @return bool True on success, false on failure.
82 *
83 * @throws Exception If refund fails.
84 * @since 5.0
85 */
86 public function refund_order( $refund_id, $args ) {
87 $order = new SST_Order( $args['order_id'] );
88 $refund = wc_get_order( $refund_id );
89 $result = false;
90
91 // Logging
92 SST_Logger::order_log( __( 'refund_created triggered', 'simple-sales-tax' ), $order->get_id() );
93
94 try {
95 $result = $order->do_refund( $refund );
96
97 // Avoid Deleting the refund - WooApi errors if the refund is deleted
98 // if ( ! $result ) {
99 // $refund->delete( true );
100 // }
101 } catch ( Exception $ex ) {
102 // Avoid Deleting the refund - WooApi errors if the refund is deleted
103 // $refund->delete( true );
104 throw $ex; /* Let Woo handle the exception */
105 }
106
107 return $result;
108 }
109
110 /**
111 * If the "Capture Orders Immediately" option is enabled, capture orders
112 * when payment is received.
113 *
114 * @param int $order_id ID of order for which payment was just received.
115 *
116 * @throws Exception If attempt to capture order fails.
117 * @since 5.0
118 */
119 public function maybe_capture_order( $order_id ) {
120 // Disable integration
121 if ( 'yes' === SST_Settings::get( 'disable_integration' ) ) {
122 SST_Logger::order_log( __( 'Integration disabled. Skipping order capture.', 'simple-sales-tax' ), $order_id );
123 return;
124 }
125
126 if ( 'yes' === SST_Settings::get( 'capture_immediately' ) ) {
127 $order = new SST_Order( $order_id );
128
129 // Logging
130 SST_Logger::order_log( __( 'payment_complete: Capturing order.', 'simple-sales-tax' ), $order->get_id() );
131
132 $order->do_capture();
133 }
134 }
135
136 /**
137 * Hides Simple Sales Tax order item meta.
138 *
139 * @param array $to_hide Meta keys to hide.
140 *
141 * @return array
142 * @since 5.0
143 */
144 public function hide_order_item_meta( $to_hide ) {
145 $to_hide[] = '_wootax_tax_amount';
146 $to_hide[] = '_wootax_location_id';
147 $to_hide[] = '_wootax_index';
148
149 return $to_hide;
150 }
151
152 /**
153 * Temporary fix for #50. Ensures that tax data for shipping items is
154 * correctly formatted.
155 *
156 * @param array $taxes Tax data for WooCommerce order item.
157 * @param WC_Order_Item $item WooCommerce order item object.
158 *
159 * @return array
160 * @since 5.0
161 */
162 public function fix_shipping_tax_issue( $taxes, $item ) {
163 if ( 'shipping' === $item->get_type() ) {
164 if ( isset( $taxes['total'], $taxes['total']['total'] ) ) {
165 unset( $taxes['total']['total'] );
166 }
167 }
168
169 return $taxes;
170 }
171
172 /**
173 * Runs when an order is saved to set the order DB version.
174 *
175 * @param WC_Order $order The WooCommerce order that is about to be saved.
176 */
177 public function save_db_version( $order ) {
178 $db_version = $order->get_meta( '_wootax_db_version', true );
179
180 if ( empty( $db_version ) ) {
181 $order->update_meta_data( '_wootax_db_version', SST()->version );
182 }
183 }
184
185 /**
186 * Calculates tax for orders created via WooCommerce REST API.
187 *
188 * @param bool $and_taxes Whether to calculate taxes
189 * @param WC_Order $order Order object
190 */
191 public function calculate_order_tax( $and_taxes, $order ) {
192 // Note: sst_order_calculate_taxes sets `and_taxes` to false so
193 // the `and_taxes` check prevents infinite recursion
194
195 if ( ! $order instanceof WC_Order || $order instanceof WC_Order_Refund ) {
196 return;
197 }
198
199 // Disable integration
200 if ( 'yes' === SST_Settings::get( 'disable_integration' ) ) {
201 SST_Logger::order_log( __( 'Integration disabled. Skipping order tax calculation.', 'simple-sales-tax' ), $order->get_id() );
202 return;
203 }
204
205 /**
206 * Data mover mode
207 * @since 8.4.1
208 */
209 if ( 'data_mover' === sst_integration_mode() ) {
210 SST_Logger::order_log( __( 'Data mover mode. Skipping order tax calculation.', 'simple-sales-tax' ), $order->get_id() );
211 return;
212 }
213
214 $should_calculate = (
215 WC()->is_rest_api_request()
216 && 'rest-api' === $order->get_created_via()
217 && $and_taxes
218 );
219
220 // Logging
221 SST_Logger::order_log( __( 'Calculating order tax.', 'simple-sales-tax' ), $order->get_id(), $should_calculate );
222
223 if ( $should_calculate ) {
224 sst_order_calculate_taxes( $order );
225 }
226 }
227
228 /**
229 * Adds support for querying orders by DB version and TaxCloud status with wc_get_orders()
230 *
231 * @param array $query Args for WP_Query.
232 * @param array $query_vars Query vars from WC_Order_Query.
233 *
234 * @return array
235 */
236 public function handle_version_query_var( $query, $query_vars ) {
237 if ( ! is_array( $query['meta_query'] ) ) {
238 $query['meta_query'] = array();
239 }
240
241 if ( ! empty( $query_vars['wootax_version'] ) ) {
242 $query['meta_query'][] = array(
243 'key' => '_wootax_db_version',
244 'value' => esc_attr( $query_vars['wootax_version'] ),
245 );
246 }
247
248 if ( ! empty( $query_vars['wootax_status'] ) ) {
249 $query['meta_query'][] = array(
250 'key' => '_wootax_status',
251 'value' => esc_attr( $query_vars['wootax_status'] ),
252 );
253 }
254
255 return $query;
256 }
257
258 /**
259 * Saves the exemption certificate for an order.
260 *
261 * @param int $order_id Order post ID.
262 */
263 public function save_certficiate( $order_id ) {
264 $order = new SST_Order( $order_id );
265 $is_editable = 'pending' === $order->get_taxcloud_status();
266
267 if ( ! $is_editable ) {
268 return;
269 }
270
271 $certificate_id = sanitize_text_field(
272 wp_unslash( $_POST['exempt_cert'] ?? '' )
273 );
274 $order->set_certificate_id( $certificate_id );
275 $order->save();
276 }
277
278 /**
279 * Filters woocommerce_order_hide_zero_taxes so zero taxes are
280 * shown if the SST "Show Zero Tax" setting is enabled.
281 *
282 * @return bool
283 */
284 public function filter_hide_zero_taxes() {
285 return 'true' !== SST_Settings::get( 'order_show_zero_tax', 'true' );
286 }
287
288 }
289
290 new SST_Order_Controller();
291