Skip to content

Instantly share code, notes, and snippets.

@saas786
Forked from danielbitzer/functions.php
Created August 26, 2022 11:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save saas786/ceeac7a7c58902a22833fb6ceb637846 to your computer and use it in GitHub Desktop.
Save saas786/ceeac7a7c58902a22833fb6ceb637846 to your computer and use it in GitHub Desktop.
Function to programmatically run a specific workflow for a specific order
<?php
/**
* Run a specific workflow for a specific order.
*
* @param int $workflow_id
* @param int $order_id
*
* @throws \Exception
*/
function automatewoo_run_order_workflow( int $workflow_id, int $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
throw new \Exception;
}
$customer = \AutomateWoo\Customer_Factory::get_by_order( $order );
// If the required data items for the trigger are not set here the workflow will not run
$workflow_data = new Data_Layer(
[
\AutomateWoo\DataTypes\DataTypes::ORDER => $order,
\AutomateWoo\DataTypes\DataTypes::CUSTOMER => $customer
]
);
$workflow = \AutomateWoo\Workflows\Factory::get( $workflow_id );
if ( ! $workflow ) {
throw new \Exception;
}
$workflow->maybe_run( $workflow_data );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment