CardIcons
1 year ago
CouponsController.php
7 months ago
IppFunctions.php
1 year ago
MobileMessagingHandler.php
2 years ago
OrderActionsRestController.php
3 months ago
OrderAttributionBlocksController.php
1 year ago
OrderAttributionController.php
5 months ago
OrderNoteGroup.php
3 months ago
OrderStatusRestController.php
5 months ago
PaymentInfo.php
10 months ago
PointOfSaleEmailHandler.php
3 months ago
PointOfSaleOrderUtil.php
4 weeks ago
TaxesController.php
3 years ago
TaxesController.php
56 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Internal\Orders; |
| 4 | |
| 5 | /** |
| 6 | * Class with methods for handling order taxes. |
| 7 | */ |
| 8 | class TaxesController { |
| 9 | |
| 10 | /** |
| 11 | * Calculate line taxes via Ajax call. |
| 12 | */ |
| 13 | public function calc_line_taxes_via_ajax(): void { |
| 14 | check_ajax_referer( 'calc-totals', 'security' ); |
| 15 | |
| 16 | if ( ! current_user_can( 'edit_shop_orders' ) || ! isset( $_POST['order_id'], $_POST['items'] ) ) { |
| 17 | wp_die( -1 ); |
| 18 | } |
| 19 | |
| 20 | $order = $this->calc_line_taxes( $_POST ); |
| 21 | |
| 22 | include __DIR__ . '/../../../includes/admin/meta-boxes/views/html-order-items.php'; |
| 23 | wp_die(); |
| 24 | } |
| 25 | |
| 26 | /** |
| 27 | * Calculate line taxes programmatically. |
| 28 | * |
| 29 | * @param array $post_variables Contents of the $_POST array that would be passed in an Ajax call. |
| 30 | * @return object The retrieved order object. |
| 31 | */ |
| 32 | public function calc_line_taxes( array $post_variables ): object { |
| 33 | $order_id = absint( $post_variables['order_id'] ); |
| 34 | $calculate_tax_args = array( |
| 35 | 'country' => isset( $post_variables['country'] ) ? wc_strtoupper( wc_clean( wp_unslash( $post_variables['country'] ) ) ) : '', |
| 36 | 'state' => isset( $post_variables['state'] ) ? wc_strtoupper( wc_clean( wp_unslash( $post_variables['state'] ) ) ) : '', |
| 37 | 'postcode' => isset( $post_variables['postcode'] ) ? wc_strtoupper( wc_clean( wp_unslash( $post_variables['postcode'] ) ) ) : '', |
| 38 | 'city' => isset( $post_variables['city'] ) ? wc_strtoupper( wc_clean( wp_unslash( $post_variables['city'] ) ) ) : '', |
| 39 | ); |
| 40 | |
| 41 | // Parse the jQuery serialized items. |
| 42 | $items = array(); |
| 43 | parse_str( wp_unslash( $post_variables['items'] ), $items ); |
| 44 | |
| 45 | // Save order items first. |
| 46 | wc_save_order_items( $order_id, $items ); |
| 47 | |
| 48 | // Grab the order and recalculate taxes. |
| 49 | $order = wc_get_order( $order_id ); |
| 50 | $order->calculate_taxes( $calculate_tax_args ); |
| 51 | $order->calculate_totals( false ); |
| 52 | |
| 53 | return $order; |
| 54 | } |
| 55 | } |
| 56 |