-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Proxeuse/release1.0.0
Release1.0.0
- Loading branch information
Showing
2 changed files
with
157 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# Woocommerce-Rocket.Chat-Notifications | ||
This WordPress plugin notifies you of new orders placed by your customers through one of your Rocket.Chat channels. The plugin uses the `woocommerce_thankyou` hook and therefore only runs if a customer places a order in the frontend. | ||
The plugin does not have a WordPress settings page and must therefore be configured in the PHP file. However, it is a simple process that will take you less than 5 minutes to complete. | ||
|
||
## Installation | ||
Download the GitHub repository by cloning it or [downloading it as a zip file](https://github.com/Proxeuse/Woocommerce-Rocket.Chat-Notifications/archive/master.zip). Then extract the folder and upload the `woocommerce-rocket-chat-order-notifications` folder to your `/wp-content/plugins/` directory. The plugin is now installed but not yet configured. | ||
|
||
## Configuration | ||
Before you can edit the `woocommerce-rocket-chat-order-notifications.php` file and enter your credentials you should have setup a (seperate) user for your Rocket.Chat instance. We assume that you have created a user with the BOT role and have access to the username, email and password. | ||
Firstly you should open the `woocommerce-rocket-chat-order-notifications.php` so that you can edit it. You should then scroll down to line 27 where the variables are set. Replace the existing Rocket.Chat URL with your own (e.g. [chat.proxeuse.com](https://chat.proxeuse.com/)). Please **do not** enter a trailing slash, you can however enter a port if your Rocket.Chat instance doesn't use the regular HTTP(s) ports (80 or 443). Then enter the username and password of the user that you've created before. Then enter a channel (#) or direct-message (@) at the `$rocketChatChannel` variable. More information about the `$rocketChatChannel` variable can be found here: [https://docs.rocket.chat/api/rest-api/methods/chat/postmessage](https://docs.rocket.chat/api/rest-api/methods/chat/postmessage). If your user has the BOT role you can assign an Alias for the user, if your user doesn't have the permissions please enter two quotes followed by a semicolon `"";` | ||
That should be it for the configuration part. If you are a more experienced PHP developer/user you can edit other things such as the message send to the Rocket.Chat API. | ||
|
||
## Testing | ||
In order for you to test the new plugin we recommend you to uncomment (add to slashes in front of) line 131 `// $order->update_meta_data( '_thankyou_action_done', true );` | ||
If you have made changes to the Module settings you can refresh the Thank You page to post a new message in your Rocket.Chat channel. | ||
You can create a 100% discount code for your user in WooCommerce to be able to test the plugin functionality without actually having to pay. | ||
|
||
## Troubleshooting | ||
Do you have troubles with using the plugin? [Submit an Issue on GitHub](https://github.com/Proxeuse/Woocommerce-Rocket.Chat-Notifications/issues/new) or contact us directly at [[email protected]](mailto:[email protected]). | ||
You can also use `ECHO` statements to output text to the Thank You page. | ||
|
||
## Contributors | ||
[@lomars](https://github.com/lomars) - Thank you for writing the original [WooCommerce hook](https://stackoverflow.com/a/42533543) that allowed us to further develop the plugin. |
134 changes: 134 additions & 0 deletions
134
woocommerce-rocket-chat-order-notifications/woocommerce-rocket-chat-order-notifications.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
/* | ||
Plugin Name: WooCommerce Rocket.Chat Order Notifications | ||
Plugin URI: https://www.proxeuse.com/en/wordpress-plugins/woocommerce/rocket-chat-order-notifications/ | ||
Description: This plugin will notify you of new WooCommerce orders by posting in one of your Rocket.Chat channels. You can adjust a lot to meet your requirements. | ||
Version: 1.0.0 | ||
Author: Proxeuse | ||
Author URI: https://www.proxeuse.com/ | ||
License: GPLv2 or later | ||
Text Domain: woocommerce-rocket-chat-order-notifications | ||
*/ | ||
|
||
/** | ||
* Massive thanks to LoicTheAztec (StackOverFlow) for sharing his WooCommerce hook. | ||
* His code: https://stackoverflow.com/a/42533543 | ||
* GitHub: https://github.com/lomars | ||
*/ | ||
add_action('woocommerce_thankyou', 'rocketchatapi', 10, 1); | ||
function rocketchatapi( $order_id ) { | ||
if ( ! $order_id ) | ||
return; | ||
|
||
// Allow code execution only once | ||
if( ! get_post_meta( $order_id, '_thankyou_action_done', true ) ) { | ||
|
||
// Set the variables below: | ||
$rocketChatURL = "http://127.0.0.1:3000"; | ||
$rocketChatUsername = "woocommerce"; | ||
$rocketChatPassword = "PasswordHere"; | ||
$rocketChatChannel = "#woocommerce"; | ||
$rocketChatAlias = "WooCommerce Bot"; | ||
|
||
// Get an instance of the WC_Order object | ||
$order = wc_get_order( $order_id ); | ||
|
||
// Request User ID and Auth Token by Rocket.Chat API | ||
// start curl request | ||
$ch = curl_init(); | ||
// set curl url | ||
curl_setopt($ch, CURLOPT_URL, "$rocketChatURL/api/v1/login"); | ||
// set POST variables | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array("user"=>$rocketChatUsername, "password"=>$rocketChatPassword))); | ||
// we want a response | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
// execute curl command and save to $authOutput | ||
$authOutput = curl_exec($ch); | ||
// close curl connection | ||
curl_close($ch); | ||
// decode authOutput so we can get data from it | ||
$authArray = json_decode($authOutput, true); | ||
|
||
// save userid and authtoken to be used later | ||
$userid = $authArray['data']['userId']; | ||
$authtoken = $authArray['data']['authToken']; | ||
|
||
// get customers personal information | ||
$fullname = $order->get_formatted_billing_full_name(); | ||
$company = $order->get_billing_company(); | ||
$country = $order->get_billing_country(); | ||
|
||
// get order totals | ||
$ordersubtotal = html_entity_decode(strip_tags($order->get_subtotal_to_display())); | ||
$odervat = html_entity_decode(strip_tags(wc_price($order->get_total_tax()))); | ||
$ordertotal = html_entity_decode(strip_tags($order->get_formatted_order_total())); | ||
$discount = html_entity_decode(strip_tags($order->get_discount_to_display())); | ||
|
||
// get other data | ||
$ordercount = $order->get_item_count(); | ||
$orderid = $order->get_id(); | ||
$orderURL = $order->get_view_order_url(); | ||
|
||
// set start of message | ||
$message = "Hi there! A new order has been placed by $fullname ($company) from $country.\n The client has ordered $ordercount product(s): "; | ||
|
||
// Loop through order items | ||
foreach ( $order->get_items() as $item_id => $item ) { | ||
// if only one product in order | ||
if($ordercount = 1){ | ||
// add product name to the message | ||
$message .= $item->get_name(); | ||
} | ||
// if more products in order | ||
else{ | ||
// add product to array | ||
$productsArray[] = $item->get_name(); | ||
} | ||
} | ||
// implode array to string | ||
$products = implode(",", $productsArray); | ||
// add string to message | ||
$message .= $products; | ||
|
||
// add a link to WooCommerce view order page | ||
$message .= "\n $orderURL \n \n"; | ||
|
||
// if discount applies | ||
if($order->get_discount_total() > 0){ | ||
// add to message | ||
$message .= "*Subtotal:* $ordersubtotal \n *Discount:* $discount \n *Taxes:* $odervat \n *Total:* $ordertotal \n"; | ||
} | ||
// no discount | ||
else{ | ||
// add to message | ||
$message .= "*Subtotal:* $ordersubtotal \n *Taxes:* $odervat \n *Total:* $ordertotal \n"; | ||
} | ||
|
||
// Send message to Rocket.Chat channel by using the Rocket.Chat API | ||
// start curl request | ||
$ch = curl_init(); | ||
// set curl url | ||
curl_setopt($ch, CURLOPT_URL, "$rocketChatURL/api/v1/chat.postMessage"); | ||
// set POST variables | ||
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query(array("channel"=>$rocketChatChannel, "text"=>"$message", "alias"=>$rocketChatAlias))); | ||
// set http headers | ||
curl_setopt($ch, CURLOPT_HTTPHEADER, array( | ||
// set auth token required by rocketchat api | ||
"X-Auth-Token: $authtoken", | ||
// set userid also required by rocketchat api | ||
"X-User-Id: $userid", | ||
)); | ||
// we want a response | ||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | ||
// execute curl command and save to $messageOutput | ||
$messageOutput = curl_exec($ch); | ||
// close curl connection | ||
curl_close($ch); | ||
// decode authOutput so we can get data from it | ||
$messageArray = json_decode($messageOutput,true); | ||
|
||
// Flag the action as done (to avoid repetitions on reload for example) | ||
$order->update_meta_data( '_thankyou_action_done', true ); | ||
$order->save(); | ||
} | ||
} |