1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
<?php
namespace SIW\WooCommerce\Checkout;
class Newsletter{
public static function init() {
$self = new self();
add_action( 'woocommerce_after_checkout_billing_form', [ $self, 'show_newsletter_signup_checkbox'] );
add_filter( 'woocommerce_checkout_posted_data', [ $self, 'capture_newsletter_signup'] );
add_action( 'woocommerce_checkout_order_processed', [ $self, 'process_newsletter_signup'], 10, 3 );
}
public function show_newsletter_signup_checkbox( \WC_Checkout $checkout ) {
woocommerce_form_field( 'newsletter_signup', [
'type' => 'checkbox',
'class' => ['form-row-wide'],
'clear' => true,
'label' => __( 'Ja, ik wil graag de SIW nieuwsbrief ontvangen', 'siw' ),
], $checkout->get_value( 'newsletter_signup' )
);
}
public function capture_newsletter_signup( array $data ) {
$data['newsletter_signup'] = (int) isset( $_POST['newsletter_signup'] );
return $data;
}
public function process_newsletter_signup( int $order_id, array $posted_data, \WC_Order $order ) {
if ( 1 != $posted_data['newsletter_signup'] ) {
return;
}
siw_newsletter_subscribe(
$order->get_billing_email(),
siw_get_option( 'newsletter_list' ),
[
'firstname' => $order->get_billing_first_name(),
'lastname' => $order->get_billing_last_name(),
]
);
}
}