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
CouponsController.php
100 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Automattic\WooCommerce\Internal\Orders; |
| 4 | |
| 5 | use Automattic\WooCommerce\Utilities\ArrayUtil; |
| 6 | use Automattic\WooCommerce\Utilities\StringUtil; |
| 7 | use Exception; |
| 8 | |
| 9 | /** |
| 10 | * Class with methods for handling order coupons. |
| 11 | */ |
| 12 | class CouponsController { |
| 13 | |
| 14 | /** |
| 15 | * Add order discount via Ajax. |
| 16 | * |
| 17 | * @throws Exception If order or coupon is invalid. |
| 18 | */ |
| 19 | public function add_coupon_discount_via_ajax(): void { |
| 20 | check_ajax_referer( 'order-item', 'security' ); |
| 21 | |
| 22 | if ( ! current_user_can( 'edit_shop_orders' ) ) { |
| 23 | wp_die( -1 ); |
| 24 | } |
| 25 | |
| 26 | $response = array(); |
| 27 | |
| 28 | try { |
| 29 | $order = $this->add_coupon_discount( $_POST ); |
| 30 | |
| 31 | ob_start(); |
| 32 | include __DIR__ . '/../../../includes/admin/meta-boxes/views/html-order-items.php'; |
| 33 | $response['html'] = ob_get_clean(); |
| 34 | |
| 35 | ob_start(); |
| 36 | $notes = wc_get_order_notes( array( 'order_id' => $order->get_id() ) ); |
| 37 | include __DIR__ . '/../../../includes/admin/meta-boxes/views/html-order-notes.php'; |
| 38 | $response['notes_html'] = ob_get_clean(); |
| 39 | } catch ( Exception $e ) { |
| 40 | wp_send_json_error( array( 'error' => $e->getMessage() ) ); |
| 41 | } |
| 42 | |
| 43 | // wp_send_json_success must be outside the try block not to break phpunit tests. |
| 44 | wp_send_json_success( $response ); |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Add order discount programmatically. |
| 49 | * |
| 50 | * @param array $post_variables Contents of the $_POST array that would be passed in an Ajax call. |
| 51 | * @return object The retrieved order object. |
| 52 | * @throws \Exception Invalid order or coupon. |
| 53 | */ |
| 54 | public function add_coupon_discount( array $post_variables ): object { |
| 55 | $order_id = isset( $post_variables['order_id'] ) ? absint( $post_variables['order_id'] ) : 0; |
| 56 | $order = wc_get_order( $order_id ); |
| 57 | $calculate_tax_args = array( |
| 58 | 'country' => isset( $post_variables['country'] ) ? wc_strtoupper( wc_clean( wp_unslash( $post_variables['country'] ) ) ) : '', |
| 59 | 'state' => isset( $post_variables['state'] ) ? wc_strtoupper( wc_clean( wp_unslash( $post_variables['state'] ) ) ) : '', |
| 60 | 'postcode' => isset( $post_variables['postcode'] ) ? wc_strtoupper( wc_clean( wp_unslash( $post_variables['postcode'] ) ) ) : '', |
| 61 | 'city' => isset( $post_variables['city'] ) ? wc_strtoupper( wc_clean( wp_unslash( $post_variables['city'] ) ) ) : '', |
| 62 | ); |
| 63 | |
| 64 | if ( ! $order ) { |
| 65 | throw new Exception( __( 'Invalid order', 'woocommerce' ) ); |
| 66 | } |
| 67 | |
| 68 | $coupon = ArrayUtil::get_value_or_default( $post_variables, 'coupon' ); |
| 69 | if ( StringUtil::is_null_or_whitespace( $coupon ) ) { |
| 70 | throw new Exception( __( 'Invalid coupon', 'woocommerce' ) ); |
| 71 | } |
| 72 | |
| 73 | // Add user ID and/or email so validation for coupon limits works. |
| 74 | $user_id_arg = isset( $post_variables['user_id'] ) ? absint( $post_variables['user_id'] ) : 0; |
| 75 | $user_email_arg = isset( $post_variables['user_email'] ) ? sanitize_email( wp_unslash( $post_variables['user_email'] ) ) : ''; |
| 76 | |
| 77 | if ( $user_id_arg ) { |
| 78 | $order->set_customer_id( $user_id_arg ); |
| 79 | } |
| 80 | if ( $user_email_arg ) { |
| 81 | $order->set_billing_email( $user_email_arg ); |
| 82 | } |
| 83 | |
| 84 | $order->calculate_taxes( $calculate_tax_args ); |
| 85 | $order->calculate_totals( false ); |
| 86 | |
| 87 | $code = wc_format_coupon_code( wp_unslash( $coupon ) ); // phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 88 | $result = $order->apply_coupon( $code ); |
| 89 | |
| 90 | if ( is_wp_error( $result ) ) { |
| 91 | throw new Exception( html_entity_decode( wp_strip_all_tags( $result->get_error_message() ) ) ); |
| 92 | } |
| 93 | |
| 94 | // translators: %s coupon code. |
| 95 | $order->add_order_note( esc_html( sprintf( __( 'Coupon applied: "%s".', 'woocommerce' ), $code ) ), 0, true, array( 'note_group' => OrderNoteGroup::ORDER_UPDATE ) ); |
| 96 | |
| 97 | return $order; |
| 98 | } |
| 99 | } |
| 100 |