Solved

Create custom event from new WooCommerce Orders

  • 21 March 2022
  • 2 replies
  • 823 views

Userlevel 5
Badge +19

What I want to do is seemingly very easy, and I’m sure I’m missing one very obvious, very simple step for seasoned developers, but I’m very much a self-taught, trial and error kind of guy. So, here’s what I’m trying to do….

My client has a bunch of custom fields that get added to each WooCommerce order; however, the native plugin doesn’t allow us to pass them into the event data. I’m trying to create a second event that gets triggered whenever there’s a new order and pass those extra fields in as their own event.

I’m following the PHP code example found here in the Track API doc, and hooking into the WooCommerce New order action. My current code, which does not work, looks like this:

add_action( 'woocommerce_new_order', 'lgs_custom_klaviyo_event',  1, 2  );
function lgs_custom_klaviyo_event( $order_id, $order ) {

require_once('vendor/autoload.php');

$client = new \GuzzleHttp\Client();

$response = $client->request('POST', 'https://a.klaviyo.com/api/track', [
'form_params' => [
'data' => '{"token": "MY_API", "event": "Love Jar Purchase", "customer_properties": {"$email": "MY_EMAIL"}, "properties": {"$event_id": $order_id,"Jar recipient": "Tovita","$value": 100}}'
],
'headers' => [
'Accept' => 'text/html',
'Content-Type' => 'application/x-www-form-urlencoded',
],
]);

echo $response->getBody();

};

The WooCommerce error log states this:

CRITICAL require_once(): Failed opening required 'vendor/autoload.php' (include_path='.:/usr/share/php') in /www/wp-content/themes/custom-child-theme/functions.php on line 476

Which corresponds to the line of code: require_once(‘vendor/autoload.php’);

From what I gather, it has something to do with this little message above the code example on the Track API page:

 

I’ve tried searching and figuring out how and where and what the composer is or how and where to install it, but I don’t even know if that’s the thing I need to be chasing down.

Anyways, once I can send this tiny little test packet, figuring out the actual JSON payload for the custom fields is the easy part.

 

Then again, maybe I’m way off base and need a lot more help than I think. heh

 

Thank you!

icon

Best answer by Chuck Curtis 22 March 2022, 20:24

View original

2 replies

Userlevel 1
Badge +1

Hi David,

Thank you for posting!  In regards to Composer, I would recommend learning about how to use Composer on their website.

The line

require_once('vendor/autoload.php');

is throwing the error because Composer is not installed in your WordPress environment.  Additional details about what this line is for is also detailed on the Composer website.

Once you have Composer setup, you should be all set!

Userlevel 5
Badge +19

For anyone else facing this problem and not familiar with Composer, I was able to use the PHP CURL example found on the Track API page just by copy/pasting directly into functions.php (and then updating with my specific functions, hooks, properties, and variables).

Reply