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