I would like to share a code snippet that establishes a custom WordPress hook designed to trigger when an order is marked as "completed" in WooCommerce. The purpose of this code is to enable others to utilize it for their requirements.
The provided code snippet facilitates the creation of a custom hook that retrieves the customer's email address from the order and subsequently checks whether any products within the order belong to specific categories. In the event that products from the specified categories are found, the code queries the Mailster plugin's database to identify the subscriber ID associated with the given email address. If a subscriber ID is discovered, a custom hook is triggered, allowing for further processing within Mailster's mailing campaigns. Essentially, this code facilitates the implementation of targeted email campaigns based on customers' purchases of products from specific categories in WooCommerce.
Please be advised that the successful implementation of this code requires the following steps:
- Ensure that the actual customer is registered as a subscriber in the
Mailster database. This can be achieved by adding customers as
subscribers when the WooCommerce order is created. The setting for
this can be located on the Mailster settings page under WooCommerce,
specifically the first option.
- Set the category slugs in the designated array according to the
desired product categories.
- Define the hook name that will be utilized to initiate the Mailster
autoresponder campaign.
- Create a Mailster autoresponder campaign as part of the setup
process.
- Utilize the specified hook name as set within the provided code
snippet.
- Ensure you have a working WP-Cron setup
It is crucial to emphasize that the code is provided without any guarantees or warranties, and its implementation may require customization to suit specific use cases or future updates.
The code:
// Hook into WooCommerce when an order is completed
add_action('woocommerce_order_status_completed', 'get_mailster_subscriber_id_from_order', 10, 1);
// Function to get the Mailster subscriber ID from an order
function get_mailster_subscriber_id_from_order($order_id) {
// Get the order object
$order = wc_get_order($order_id);
// Check if the order contains products from specific categories
$specific_categories = array('PRODUCT_CATEGORY_SLUG1', 'PRODUCT_CATEGORY_SLUG2', 'ETC. ETC.'); // Replace with your desired category slug(s)
$order_contains_specific_category = false;
// Iterate through each item in the order
foreach ($order->get_items() as $item_id => $item) {
// Get the product ID for the current item
$product_id = $item->get_product_id();
// Get the product categories as slugs
$product_categories = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'slugs'));
// Check if the item's product categories intersect with the specific categories
if (array_intersect($specific_categories, $product_categories)) {
// If any item matches the specific categories, set the flag to true and break the loop
$order_contains_specific_category = true;
break;
}
}
// If the order doesn't contain products from specific categories, return early
if (!$order_contains_specific_category) {
return;
}
// Get the customer email associated with the order
$customer_email = $order->get_billing_email();
// Use the function to get the subscriber ID based on the email address
$subscriber_id = get_mailster_subscriber_id_by_email($customer_email);
// If the subscriber ID is found, trigger a custom hook with the ID
if ($subscriber_id) {
do_action('YOUR_CUSTOM_HOOK_NAME', $subscriber_id);
}
}
// Function to get the Mailster subscriber ID based on the email address
function get_mailster_subscriber_id_by_email($email) {
global $wpdb;
// Get the actual 'wp_mailster_subscribers' table name
$subscriber_table = $wpdb->prefix . 'mailster_subscribers';
// Query to get the subscriber ID based on the email address
$subscriber_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT ID FROM $subscriber_table WHERE email = %s",
$email
)
);
return $subscriber_id;
}