/
[1.2.x] Sample Extension

[1.2.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 handler classes and the values are the replacement classes.

function wc_maileon_extend_email_hooks($hooks) { $hooks[\WC_Email_Booking_Reminder::class] = 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\Transactional { /** * Trigger email sending * * @param integer $booking_id * @return void */ public function send_booking_reminder($booking_id) { $this->trigger($booking_id); } /** * Gets the WooCommerce object for this email * * @param integer $id * @param boolean $object * @return \WC_Booking */ protected function get_object($id, $object = false) { return get_wc_booking($id); } /** * Gets the Maileon transaction name for this email * * @return string */ public function get_transaction_name() { return '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_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()) ]; } }

get_transaction_name
This method should return the name of the transaction type for this class.

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

get_object
This method should convert the id received by the trigger method to a relevant object (\WC_Booking in this example). If it is not overridden a \WC_Order object is returned by default.

map_transaction_content
This method should map the object of this email to a Maileon transaction. The object received by is the type given by get_object. By default this method receives a \WC_Order object.

send_booking_reminder
This is the fucntion that handles the wc-booking-action. In this example it simply forwards to the default trigger method defined in the parent class.

Related content