/
[1.3.x] Sample Extension

[1.3.x] Sample Extension

This sample extension adds support for WooCommerce Bookings. Download the code from https://maileon.atlassian.net/wiki/spaces/MIFW/pages/698318861.

The functionality is implemented as a WordPress plugin, however similar code could be used to extend the plugin using a theme’s functions.php.

Functionality

The plugin implements two parts of the booking functionality; it adds booking.* properties to the Maileon transactions and implements the Booking Reminder email notification type.

Implementation

Extending default transactions

To extend the standard Maileon transaction you can use the wc_maileon_transaction_attributes and wc_maileon_transaction_content hooks. These hooks are called for all default WooCommerce emails.

/** * Modifies the global email transaction attributes * * @param array $attributes * @return array */ function wc_maileon_modify_transaction_attributes($attributes) { $attributes []= new AttributeType(null, 'booking.id', DataType::$STRING, false); $attributes []= new AttributeType(null, 'booking.start_date', DataType::$TIMESTAMP, false); $attributes []= new AttributeType(null, 'booking.end_date', DataType::$TIMESTAMP, false); return $attributes; } add_filter('wc_maileon_transaction_attributes', 'wc_maileon_modify_transaction_attributes'); /** * Modifies the global email transaction content * * @param array $content * @param \WC_Order $order * @return array */ function wc_maileon_modify_transaction_content($content, $order) { $bookings = \WC_Booking_Data_Store::get_booking_ids_from_order_id( $order->get_id() ); foreach ( $bookings as $booking_id ) { $booking = get_wc_booking( $booking_id ); $content['booking.id'] = (string)$booking_id; $content['booking.start_date'] = $booking->get_start_date(); $content['booking.end_date'] = $booking->get_end_date(); // only add the first booking break; } return $content; } add_filter('wc_maileon_transaction_content', 'wc_maileon_modify_transaction_content', 10, 2);

Adding a custom email type

Adding a custom email type is achieved using the wc_maileon_email_hooks filter. The keys of this array are the default WooCommerce email ids and the values are the handler classes.

function wc_maileon_extend_email_hooks($hooks) { $hooks['booking_reminder'] = BookingReminder::class; return $hooks; } add_filter('wc_maileon_email_hooks', 'wc_maileon_extend_email_hooks');

Custom email handler class

The code for the BookingReminder class looks like this:

class BookingReminder extends \Maileon\WooCommerce\Emails\OrderStatusChanged { /** * Trigger a transaction based on the given object * * @param mixed $object * @param string $to The email address to send message. * @param string $subject Optional. Email subject * @param string $message Optional. Message contents * @param string|array $headers Optional. Additional headers. * @param string|array $attachments Optional. Files to attach. * @return bool Whether the email contents were sent successfully. */ public function trigger($object, $to, $subject = '', $message = '', $headers = '', $attachments = array()) { $booking = get_wc_booking($object); if (is_a($booking, \WC_Booking::class)) { $content = $this->map_maileon_booking_transaction_content($booking); $order = $booking->get_order(); if(!is_object($order)) { return; } $customer_id = $order->get_customer_id(); return $this->trigger_maileon_transaction($to, $content, $customer_id, $object, $this->id); } else { throw new \Exception("Booking not found for id: " . $object); } } /** * Sets the default value for the Maileon transaction name * * @return void */ public function init_form_fields() { parent::init_form_fields(); $this->form_fields['maileon_transaction_name']['default'] = 'wc_booking_reminder'; } /** * Returns the maileon transaction type definition * * @return array */ public function get_transaction_attributes() { return [ new AttributeType(null, 'booking.id', DataType::$STRING, false), new AttributeType(null, 'booking.start_date', DataType::$TIMESTAMP, false), new AttributeType(null, 'booking.end_date', DataType::$TIMESTAMP, false), new AttributeType(null, 'booking.zoom.link', DataType::$STRING, false), ]; } /** * Maps the underlying booking to the transaction * * @param \WC_Booking $booking * @return array */ public function map_maileon_booking_transaction_content($booking) { return [ 'booking.id' => (string)$booking->get_id(), 'booking.start_date' => $booking->get_start_date(), 'booking.end_date' => $booking->get_end_date(), 'booking.zoom.link' => WC_Bookings_Maileon()->get_zoom_browser_join_link($booking->get_id()) ]; } }

init_form_fields
Can be used to override default form fields for the settings of this type of transaction.

get_transaction_attributes
This method should return an array of Maileon attribute types which define the structure of the transaction type.

trigger
This method should trigger a Maileon transaction based on the supplied arguments.

Related content