Reputation: 13
we're setting up an autoresponder from mailster
which will get triggered when woocommerce
order status is updated to 'completed'.
We can't seem to figure out to trigger the autoresponder.
This is the guide we are following: https://kb.mailster.co/working-with-action-hook-auto-responders/
Any help is appreciated as we've been working on this for a month now without any luck. Also just to inform you that I'm not a developer but I'm trying my best to fix this. This is my first post and registered here just for the support of the community.
This is for woocommerce
that will be trigger action hook to send autoresponder via mailster
. we're using 'My custom functions' plugin to integrate the code but then the code doesn't seem to work properly.
// define the woocommerce_order_status_completed callback
function my_custom_hook() {
$order->has_status( 'completed' );
do_action( 'my_custom_hook' );
}
// add the action
add_action( 'woocommerce_order_status_completed', 'my_custom_hook', 10, 1 );
we expect when woocommerce order status is changed to completed, the mailster autresponder will trigger and will send the follow up email immediately.
Upvotes: 0
Views: 736
Reputation: 1
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:
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;
}
Upvotes: 0
Reputation: 830
You need to create the autoresponder on the options page. if you're using 'my_custom_hook' you'll need to change the name of your function.
This will work for you:
function trigger_autoresponder($order_id) {
$subscriber_id = mailster_get_current_user_id();
do_action( 'my_custom_hook', $subscriber_id );
}
// add the action
add_action( 'woocommerce_order_status_completed', 'trigger_autoresponder', 10, 1 );
You don't need a conditional for order status because it only triggers on order_completed status. If it doesn't work, you probably have a issue with the autoresponder, so post a screenshot of its settings and we can further assist.
Upvotes: 0