Snippets

Visser Labs Hooking into the WooCommerce Order Completed action to play "Ka-ching!"

Created by Michael Visser

File functions.php Added

  • Ignore whitespace
  • Hide word diff
+<?php
+
+// Send a notification to Domoticz when a new Order is processing/completed
+function custom_woocommerce_order_status_completed_notification( $order_id = 0 ) {
+
+	// Bail out if no Order ID has been provided
+	if( empty( $order_id ) )
+		return;
+
+	// Bail out if it’s a free Order (0.00 total)
+    $order_total = get_post_meta( $order_id, '_order_total', false );
+    if( empty( $order_total ) || $order_total = '0.00' )
+	    return;	
+
+	// CURL notification to Domoticz, increase the Orders count by 1
+	$domoticz_url = 'http://nas.visser.io:8080';
+	$domoticz_username = 'username';
+	$domoticz_password = 'password';
+	$idx = 225;
+	$base64_string = base64_encode( $domoticz_username . ':' . $domoticz_password );
+	$args = array(
+		'headers' => array(
+			'Authorization' => 'Basic ' . $base64_string
+		)
+	);
+	$url = $domoticz_url . '/json.htm?type=command&param=udevice&idx=' . $idx . '&svalue=1';
+	wp_remote_get( $url, $args );
+
+}
+add_action( 'woocommerce_order_status_completed', 'custom_woocommerce_order_status_completed_notification' );
+
+?>
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.