Skip to content

Commit

Permalink
support WooCommerce Checkout Block and stop declaring incompatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
dannyvankooten committed Sep 10, 2024
1 parent 32fe6ba commit d0450ed
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 48 deletions.
1 change: 1 addition & 0 deletions integrations/woocommerce/admin-after.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
?>

</select>
<p class="description"><?php esc_html_e('Select the location where you would like to show the sign-up checkbox. Note that only works if not using WooCommerce Checkout Block.', 'mailchimp-for-wp'); ?></p>
</td>
</tr>
</tbody>
Expand Down
84 changes: 44 additions & 40 deletions integrations/woocommerce/class-woocommerce.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

defined('ABSPATH') or exit;

use Automattic\WooCommerce\Blocks\Package;
use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields;

/**
* Class MC4WP_WooCommerce_Integration
*
Expand All @@ -17,7 +20,7 @@ class MC4WP_WooCommerce_Integration extends MC4WP_Integration
/**
* @var string
*/
public $description = "Subscribes people from WooCommerce's checkout form.";
public $description = "Subscribes people from WooCommerce's Checkout form or Checkout Block.";

/**
* @var string[]
Expand Down Expand Up @@ -58,43 +61,14 @@ public function add_hooks()
add_filter('kco_create_order', array($this, 'add_klarna_field'));
add_filter('klarna_after_kco_confirmation', array($this, 'subscribe_from_klarna_checkout'), 10, 2);

// hooks for when using WooCommerce Blocks
// TODO: Wait for this to stabilize in WooCommerce core and then work from the below
// add_action('woocommerce_init',
// function () {
// $params = array(
// 'id' => 'marketing/'.$this->checkbox_name,
// 'label' => $this->get_label_text(),
// 'location' => 'contact',
// 'type' => 'checkbox',
// 'required' => false,
// );
// // Function was marked experimental from between WooCommerce v8.3 - v8.8
// if (function_exists('__experimental_woocommerce_blocks_register_checkout_field')) {
// __experimental_woocommerce_blocks_register_checkout_field($params);
// }
// if (function_exists('woocommerce_blocks_register_checkout_field')) {
// woocommerce_blocks_register_checkout_field($params);
// }
// }
// );
// add_action('woocommerce_set_additional_field_value', array($this, 'woocommerce_set_additional_field_value'), 10, 4);
// hooks for when using WooCommerce Checkout Block
add_action('woocommerce_init', array($this, 'add_checkout_block_field'));
}

add_action('woocommerce_checkout_order_processed', array($this, 'subscribe_from_woocommerce_checkout'));
add_action('woocommerce_store_api_checkout_order_processed', array($this, 'subscribe_from_woocommerce_checkout'));
}

// public function woocommerce_set_additional_field_value( $key, $value, $group, $wc_object ) {
// if ( 'marketing/'.$this->checkbox_name !== $key ) {
// return;
// }

// if ($value) {
// $wc_object->update_meta_data( '_mc4wp_optin', $value, true );
// }

// }

/**
* Add default value for "position" setting
*
Expand All @@ -107,6 +81,24 @@ protected function get_default_options()
return $defaults;
}

public function add_checkout_block_field() {
// for compatibility with older WooCommerce versions
// check if function exists before calling
if (!function_exists('woocommerce_register_additional_checkout_field')) {
return;
}

woocommerce_register_additional_checkout_field(
array(
'id' => 'mc4wp/optin',
'location' => 'contact',
'type' => 'checkbox',
'label' => $this->get_label_text(),
'optionalLabel' => $this->get_label_text(),
),
);
}

public function add_klarna_field($create)
{
$create['options']['additional_checkbox']['text'] = $this->get_label_text();
Expand Down Expand Up @@ -141,8 +133,7 @@ public function save_woocommerce_checkout_checkbox_value($order_id)
/**
* {@inheritdoc}
*
* @param $order_id
*
* @param int|\WC_Order $order_id
* @return bool|mixed
*/
public function triggered($order_id = null)
Expand All @@ -156,7 +147,20 @@ public function triggered($order_id = null)
return false;
}

return $order->get_meta('_mc4wp_optin');
// value from default checkout form (shortcode)
$a = $order->get_meta('_mc4wp_optin');

// alternatively, value from Checkout Block field
$b = false;
if (class_exists(Package::class) && class_exists(CheckoutFields::class)) {
$checkout_fields = Package::container()->get(CheckoutFields::class);

if ($checkout_fields && method_exists($checkout_fields, 'get_field_from_object')) {
$b = $checkout_fields->get_field_from_object('mc4wp/optin', $order, 'contact');
}
}

return $a || $b;
}

public function subscribe_from_klarna_checkout($order_id, $klarna_order)
Expand All @@ -181,14 +185,14 @@ public function subscribe_from_klarna_checkout($order_id, $klarna_order)
}

/**
* @param int $order_id
* @param int|\WC_Order $order_id
* @return boolean
*/
public function subscribe_from_woocommerce_checkout($order_id)
{
if (!$this->triggered($order_id)) {
return false;
}
if (!$this->triggered($order_id)) {
return false;
}

$order = wc_get_order($order_id);
if (!$order) {
Expand Down
8 changes: 0 additions & 8 deletions integrations/woocommerce/woocommerce.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
<?php

// Explicitly declare incompatibility with WooCommerce Blocks
add_action('before_woocommerce_init', function () {
if (class_exists('\Automattic\WooCommerce\Utilities\FeaturesUtil')) {
\Automattic\WooCommerce\Utilities\FeaturesUtil::declare_compatibility('cart_checkout_blocks', MC4WP_PLUGIN_FILE, false);
}
});


mc4wp_register_integration('woocommerce', 'MC4WP_WooCommerce_Integration');

0 comments on commit d0450ed

Please sign in to comment.