PluginProbe
Additional Terms Lite for WooCommerce / trunk
Additional Terms Lite for WooCommerce vtrunk
1.7.3 1.7.2.1 trunk 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.2.2 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.6.0 1.6.1 1.6.2 1.6.3 All 35 releases
woo-additional-terms / src / WooCommerce / Block / Checkout.php

Checkout.php in Additional Terms Lite for WooCommerce trunk, at src/WooCommerce/Block/Checkout.php

154 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WooCommerce checkout customizations.
4 *
5 * @since 1.6.0
6 *
7 * @package woo-additional-terms
8 */
9
10 namespace Woo_Additional_Terms\WooCommerce\Block;
11
12 use Exception;
13 use WP_Error;
14 use WC_Order;
15 use WP_REST_Request;
16 use Woo_Additional_Terms\Admin;
17 use Automattic\WooCommerce\StoreApi\Schemas\V1\CheckoutSchema;
18
19 /**
20 * Checkout class.
21 */
22 class Checkout {
23
24 /**
25 * Setup hooks and filters.
26 *
27 * @since 1.6.0
28 *
29 * @return void
30 */
31 public function setup() {
32
33 add_action( 'woocommerce_blocks_loaded', array( $this, 'extend_store_api' ) );
34 add_action( 'woocommerce_store_api_checkout_update_order_from_request', array( $this, 'process_acceptance' ), 10, 2 );
35 add_action( 'woo_additional_terms_checkout_save_acceptance', array( $this, 'save_acceptance' ), 10, 2 );
36 }
37
38 /**
39 * Add schema Store API to support posted data.
40 * Registers the checkout endpoint extension to inform our frontend component about the result of
41 * the validity of the additional terms checkbox and react accordingly.
42 *
43 * @since 1.5.0
44 *
45 * @throws Exception When the schema callback is not callable.
46 *
47 * @return void
48 */
49 public function extend_store_api() {
50
51 woocommerce_store_api_register_endpoint_data(
52 array(
53 'endpoint' => CheckoutSchema::IDENTIFIER,
54 'namespace' => Block::NAME,
55 'data_callback' => null,
56 'schema_type' => ARRAY_A,
57 'schema_callback' => fn() => array(
58 'data' => array(
59 'description' => __( 'Whether additional terms were accepted.', 'woo-additional-terms' ),
60 'type' => 'string',
61 'context' => array( 'view', 'edit' ),
62 'arg_options' => array(
63 'validate_callback' => function( $value ) {
64 if ( ! is_string( $value ) ) {
65 return new WP_Error(
66 'api-error',
67 __( 'Invalid value type for additional terms.', 'woo-additional-terms' )
68 );
69 }
70
71 $allowed = array( 'yes', 'no', '1', '0', 'true', 'false', '' );
72
73 if ( ! in_array( strtolower( $value ), $allowed, true ) ) {
74 return new WP_Error(
75 'api-error',
76 __( 'Invalid value for additional terms.', 'woo-additional-terms' )
77 );
78 }
79
80 return true;
81 },
82 ),
83 ),
84 ),
85 )
86 );
87 }
88
89 /**
90 * Fires after an order saved into the database.
91 * We will update the post meta.
92 *
93 * @since 1.5.0
94 *
95 * @param WC_Order $order Order ID or order object.
96 * @param WP_REST_Request $request The API request currently being processed.
97 *
98 * @return void
99 */
100 public function process_acceptance( $order, $request ) {
101
102 if ( ! isset( $request['extensions'], $request['extensions'][ Block::NAME ] ) ) {
103 return;
104 }
105
106 $raw = $request['extensions'][ Block::NAME ]['data'] ?? null;
107
108 if ( null === $raw || '' === $raw ) {
109 $has_accepted = null;
110 } else {
111 $has_accepted = wc_string_to_bool( $raw );
112 }
113
114 /**
115 * Fires after additional terms submissions is about to be saved.
116 *
117 * @since 1.6.1
118 *
119 * @param null|bool $has_accepted Whether the additional terms has been accepted.
120 * @param WC_Order $order Order object.
121 */
122 do_action( 'woo_additional_terms_checkout_save_acceptance', $has_accepted, $order );
123 }
124
125 /**
126 * Stores additional terms submissions after a new order being processed.
127 *
128 * @since 1.6.1
129 *
130 * @param null|bool $has_accepted Whether the additional terms has been accepted.
131 * @param WC_Order $order Order object.
132 *
133 * @return void
134 */
135 public function save_acceptance( $has_accepted, $order ) {
136
137 // Leave if the order is not an instance of WC_Order.
138 if ( ! $order instanceof WC_Order || is_null( $has_accepted ) ) {
139 return;
140 }
141
142 // Add order note based on acceptance status.
143 $note_message = $has_accepted
144 ? __( 'Customer accepted the additional terms.', 'woo-additional-terms' )
145 : __( 'Customer did not accept the additional terms.', 'woo-additional-terms' );
146
147 $order->add_order_note( $note_message );
148
149 // Save the additional terms checkbox value as order meta.
150 $order->update_meta_data( Admin\Order::META_KEY, wc_bool_to_string( $has_accepted ) );
151 $order->save();
152 }
153 }
154