Custom Functions

Note:

This is a Developer level doc. If you are unfamiliar working with code and resolving potential conflicts, we recommend you work with a Woo Agency Partner for larger projects, or find a WooCommerce developer on Codeable for smaller customizations. We are unable to provide support for customizations under our Support Policy.

You can use Custom Function action with any AutomateWoo triggers.

Here’s a quick example of how to use a workflow with a Custom Function action to create an abandoned cart log.

We recommend adding any custom functions via a child theme. Adding them to the AutomateWoo plugin folder or your parent theme’s functions.php file should be avoided.

Create a Workflow

↑ Back to top

Start by creating a new Workflow in AutomateWoo by heading to AutomateWoo > Workflows > Add Workflow.

  • Title your workflow accordingly.
  • Select one of the Cart Abandoned Triggers
  • Set a Customer pause period
  • Select the Custom Function action
  • Enter the name of your custom function
Screenshot of an AutomateWoo workflow using a custom function.

Create your custom function

↑ Back to top

Your custom function should be added to the functions.php file of a child theme, or via a plugin that allows adding custom code snippets. We have a guide for setting up a child theme.

Adding custom code to your site’s parent theme should be avoided, since any changes will be lost when updating the theme.

<?php
/**
 * Custom function to log abandoned carts via AutomateWoo action
 * @param $workflow AutomateWoo\Workflow
 */
function my_automatewoo_action_log_abandoned_carts( $workflow ) {

	// retrieve the workflow data from the data layer
	$cart = $workflow->data_layer()->get_cart();
	$customer = $workflow->data_layer()->get_customer();

	// The data layer will return a valid data item or false
	if ( ! $cart || ! $customer ) {
		return;
	}
	
	if ( ! $customer->is_registered() ) {
		return; // don't store for guest customers
	}

	$log = get_user_meta( $customer->get_user_id(), 'aw_abandoned_carts_log', true );

	if ( ! $log ) {
		$log = array();
	}

	$log[] = array(
		'cart' => $cart,
		'date' => current_time( 'mysql', true )
	);

	update_user_meta( $customer->get_user_id(), 'aw_abandoned_carts_log', $log );
}
Use of your personal data
We and our partners process your personal data (such as browsing data, IP Addresses, cookie information, and other unique identifiers) based on your consent and/or our legitimate interest to optimize our website, marketing activities, and your user experience.