PluginProbe
Additional Terms Lite for WooCommerce / 1.6.2
Additional Terms Lite for WooCommerce v1.6.2
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 / Checkout.php

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

213 lines 5.6 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 * @author MyPreview (Github: @mahdiyazdani, @gooklani, @mypreview)
6 *
7 * @since 1.6.0
8 *
9 * @package woo-additional-terms
10 */
11
12 namespace Woo_Additional_Terms\WooCommerce;
13
14 use WC_Order;
15
16 /**
17 * Checkout class.
18 */
19 class Checkout {
20
21 /**
22 * Setup hooks and filters.
23 *
24 * @since 1.6.0
25 *
26 * @return void
27 */
28 public function setup() {
29
30 add_action( 'before_woocommerce_init', array( $this, 'should_enforce' ) );
31 add_action( 'woo_additional_terms_enforce_terms', array( $this, 'enforce_terms' ) );
32 }
33
34 /**
35 * Determine if the additional terms should be enforced during the checkout.
36 *
37 * @since 1.6.0
38 *
39 * @return void
40 */
41 public function should_enforce() {
42
43 $status = woo_additional_terms()->service( 'options' )->get( 'status', '' );
44
45 // Bail early, in case settings status is not enabled.
46 if ( ! wc_string_to_bool( $status ) ) {
47 return;
48 }
49
50 /**
51 * Render the additional terms.
52 *
53 * @since 1.6.0
54 */
55 do_action( 'woo_additional_terms_enforce_terms' );
56 }
57
58 /**
59 * Add the additional terms to the checkout page.
60 *
61 * @since 1.6.0
62 *
63 * @return void
64 */
65 public function enforce_terms() {
66
67 add_filter( 'body_class', array( $this, 'body_classes' ) );
68 add_filter( 'woocommerce_checkout_show_terms', '__return_true' );
69 add_filter( 'woocommerce_enable_order_notes_field', '__return_true' );
70 add_filter( 'woocommerce_checkout_posted_data', array( $this, 'posted_data' ) );
71 add_action( 'woocommerce_checkout_after_terms_and_conditions', array( $this, 'show_checkbox' ) );
72 add_action( 'woocommerce_after_checkout_validation', array( $this, 'show_error' ), 10, 2 );
73 add_action( 'woocommerce_checkout_update_order_meta', array( $this, 'process_acceptance' ), 10, 2 );
74 }
75
76 /**
77 * Adds custom classes to the array of body classes.
78 *
79 * @since 1.6.0
80 *
81 * @param array $classes Classes for the body element.
82 * @return array
83 */
84 public function body_classes( $classes ) {
85
86 // Bail early, in case the checkout page is not available.
87 if ( ! is_checkout() ) {
88 return $classes;
89 }
90
91 /**
92 * Append a class to the body element when the additional terms are enforced.
93 */
94 $classes[] = sanitize_html_class( woo_additional_terms()->get_slug() . '-enforce-terms' );
95
96 return $classes;
97 }
98
99 /**
100 * Append additional terms data if submitted and available.
101 *
102 * @since 1.6.0
103 *
104 * @param array $posted_data Get posted data from the checkout form.
105 *
106 * @return array
107 */
108 public function posted_data( $posted_data ) {
109
110 // Exit early, in case the additional terms checkbox is not available.
111 // @phpcs:disable WordPress.Security.NonceVerification.Missing
112 if ( ! isset( $_POST['_woo_additional_terms'] ) ) {
113 return $posted_data;
114 }
115
116 $posted_data['_woo_additional_terms'] = wc_string_to_bool( wp_unslash( $_POST['_woo_additional_terms'] ) );
117 // @phpcs:enable WordPress.Security.NonceVerification.Missing
118
119 return apply_filters( 'woo_additional_terms_checkout_posted_data', $posted_data );
120 }
121
122 /**
123 * Show terms checkbox.
124 *
125 * @since 1.3.3
126 *
127 * @return void
128 */
129 public function show_checkbox() {
130
131 // Enqueue the scripts and styles.
132 wp_enqueue_style( 'woo-additional-terms' );
133 wp_enqueue_script( 'woo-additional-terms' );
134
135 // Render the checkbox template.
136 woo_additional_terms()->service( 'template_manager' )->echo_template(
137 'checkout/checkbox.php',
138 array(
139 'is_required' => woo_additional_terms()->service( 'options' )->get( 'required', false ),
140 'display_action' => woo_additional_terms()->service( 'options' )->get( 'action', 'embed' ),
141 'page_content' => woo_additional_terms()->service( 'terms' )->get( 'content' ),
142 'checkbox_label' => woo_additional_terms()->service( 'terms' )->get( 'label' ),
143 )
144 );
145 }
146
147 /**
148 * In case the required additional terms checkbox is not checked upon checking out,
149 * throw the error message to prevent checkout from being processed.
150 *
151 * @since 1.3.3
152 *
153 * @param array $fields Fields submitted via checkout form.
154 * @param object $errors Errors object.
155 *
156 * @return void
157 */
158 public function show_error( $fields, $errors ) {
159
160 $is_required = woo_additional_terms()->service( 'options' )->get( 'required', false );
161
162 // Bail early, in case the additional terms checkbox is not required.
163 if ( ! wc_string_to_bool( $is_required ) ) {
164 return;
165 }
166
167 // Bail early, in case the additional terms checkbox is already checked.
168 if ( ! empty( $fields['_woo_additional_terms'] ) ) {
169 return;
170 }
171
172 // Get the error message.
173 $error_message = woo_additional_terms()->service( 'options' )->get( 'error', __( 'Please accept the additional terms to continue.', 'woo-additional-terms' ) );
174
175 // Add the error message.
176 // Prevent the checkout process from continuing.
177 $errors->add( 'terms_error', $error_message );
178 }
179
180 /**
181 * Processes additional terms submissions after a new order being processed.
182 *
183 * @since 1.3.3
184 *
185 * @param int $order_id Current order id.
186 * @param array $fields Fields submitted via checkout form.
187 *
188 * @return void
189 */
190 public function process_acceptance( $order_id, $fields ) {
191
192 // Get the order object.
193 $order = wc_get_order( $order_id );
194
195 // Verify the order object.
196 if ( ! $order instanceof WC_Order ) {
197 return;
198 }
199
200 $has_accepted = isset( $fields['_woo_additional_terms'] );
201
202 /**
203 * Fires after additional terms submissions is about to be saved.
204 *
205 * @since 1.6.1
206 *
207 * @param bool $has_accepted Whether the additional terms has been accepted.
208 * @param WC_Order $order Order object.
209 */
210 do_action( 'woo_additional_terms_checkout_save_acceptance', $has_accepted, $order );
211 }
212 }
213