| 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, 'save_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 |
/** |
| 87 |
* Append a class to the body element when the additional terms are enforced. |
| 88 |
*/ |
| 89 |
$classes[] = sanitize_html_class( woo_additional_terms()->get_slug() . '-enforce-terms' ); |
| 90 |
|
| 91 |
return $classes; |
| 92 |
} |
| 93 |
|
| 94 |
/** |
| 95 |
* Append additional terms data if submitted and available. |
| 96 |
* |
| 97 |
* @since 1.6.0 |
| 98 |
* |
| 99 |
* @param array $posted_data Get posted data from the checkout form. |
| 100 |
* |
| 101 |
* @return array |
| 102 |
*/ |
| 103 |
public function posted_data( $posted_data ) { |
| 104 |
|
| 105 |
// Exit early, in case the additional terms checkbox is not available. |
| 106 |
// @phpcs:disable WordPress.Security.NonceVerification.Missing |
| 107 |
if ( ! isset( $_POST['_woo_additional_terms'] ) ) { |
| 108 |
return $posted_data; |
| 109 |
} |
| 110 |
|
| 111 |
$posted_data['_woo_additional_terms'] = wc_string_to_bool( wp_unslash( $_POST['_woo_additional_terms'] ) ); |
| 112 |
// @phpcs:enable WordPress.Security.NonceVerification.Missing |
| 113 |
|
| 114 |
return apply_filters( 'woo_additional_terms_checkout_posted_data', $posted_data ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Show terms checkbox. |
| 119 |
* |
| 120 |
* @since 1.3.3 |
| 121 |
* |
| 122 |
* @return void |
| 123 |
*/ |
| 124 |
public function show_checkbox() { |
| 125 |
|
| 126 |
// Enqueue the scripts and styles. |
| 127 |
wp_enqueue_style( 'woo-additional-terms' ); |
| 128 |
wp_enqueue_script( 'woo-additional-terms' ); |
| 129 |
|
| 130 |
// Render the checkbox template. |
| 131 |
woo_additional_terms()->service( 'template_manager' )->echo_template( |
| 132 |
'checkout/checkbox.php', |
| 133 |
array( |
| 134 |
'is_required' => woo_additional_terms()->service( 'options' )->get( 'required', false ), |
| 135 |
'display_action' => woo_additional_terms()->service( 'options' )->get( 'action', 'embed' ), |
| 136 |
'page_content' => woo_additional_terms()->service( 'terms' )->get( 'content' ), |
| 137 |
'checkbox_label' => woo_additional_terms()->service( 'terms' )->get( 'label' ), |
| 138 |
) |
| 139 |
); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* In case the required additional terms checkbox is not checked upon checking out, |
| 144 |
* throw the error message to prevent checkout from being processed. |
| 145 |
* |
| 146 |
* @since 1.3.3 |
| 147 |
* |
| 148 |
* @param array $fields Fields submitted via checkout form. |
| 149 |
* @param object $errors Errors object. |
| 150 |
* |
| 151 |
* @return void |
| 152 |
*/ |
| 153 |
public function show_error( $fields, $errors ) { |
| 154 |
|
| 155 |
$is_required = woo_additional_terms()->service( 'options' )->get( 'required', false ); |
| 156 |
|
| 157 |
// Bail early, in case the additional terms checkbox is not required. |
| 158 |
if ( ! wc_string_to_bool( $is_required ) ) { |
| 159 |
return; |
| 160 |
} |
| 161 |
|
| 162 |
// Bail early, in case the additional terms checkbox is already checked. |
| 163 |
if ( ! empty( $fields['_woo_additional_terms'] ) ) { |
| 164 |
return; |
| 165 |
} |
| 166 |
|
| 167 |
// Get the error message. |
| 168 |
$error_message = woo_additional_terms()->service( 'options' )->get( 'error', __( 'Please accept the additional terms to continue.', 'woo-additional-terms' ) ); |
| 169 |
|
| 170 |
// Add the error message. |
| 171 |
// Prevent the checkout process from continuing. |
| 172 |
$errors->add( 'terms_error', $error_message ); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Stores additional terms submissions after a new order being processed. |
| 177 |
* |
| 178 |
* @since 1.3.3 |
| 179 |
* |
| 180 |
* @param int $order_id Current order id. |
| 181 |
* @param array $fields Fields submitted via checkout form. |
| 182 |
* |
| 183 |
* @return void |
| 184 |
*/ |
| 185 |
public function save_acceptance( $order_id, $fields ) { |
| 186 |
|
| 187 |
// Get the order object. |
| 188 |
$order = wc_get_order( $order_id ); |
| 189 |
|
| 190 |
// Verify the order object. |
| 191 |
if ( ! $order instanceof WC_Order ) { |
| 192 |
return; |
| 193 |
} |
| 194 |
|
| 195 |
// Save the additional terms checkbox value as order meta. |
| 196 |
$order->update_meta_data( '_woo_additional_terms', wc_bool_to_string( isset( $fields['_woo_additional_terms'] ) ) ); |
| 197 |
|
| 198 |
// If the additional terms checkbox is checked, add note that customer accepted if not add note tha customer didn't accept. |
| 199 |
if ( isset( $fields['_woo_additional_terms'] ) && wc_string_to_bool( $fields['_woo_additional_terms'] ) ) { |
| 200 |
$order->add_order_note( |
| 201 |
__( 'Customer accepted the additional terms.', 'woo-additional-terms' ) |
| 202 |
); |
| 203 |
|
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
$order->add_order_note( |
| 208 |
__( 'Customer did not accept the additional terms.', 'woo-additional-terms' ) |
| 209 |
); |
| 210 |
} |
| 211 |
} |
| 212 |
|