├── readme.md └── woocommerce_mautic └── woocommerce-mautic.php /readme.md: -------------------------------------------------------------------------------- 1 |

Woocommerce Mautic Plugin

2 | 3 | This is a basic plugin that passes customers first name, last name and email address to Mautic. It is a very rough implementation, which is the result of me (a Node developer) knowing very little about the WP platform or PHP. However, it does work (at least for me!). 4 | 5 | Please do jump in with a PR if you are able to improve something! 6 | 7 |

Install

8 | The same way you would any other wordpress plugin. FTP/SSH to wp-content/plugins. More info on how to install plugins here: https://codex.wordpress.org/Managing_Plugins#Manual_Plugin_Installation 9 | 10 |

Set up

11 | 12 | 1. Create a new Mautic form that will be used to submit woocommerce customers. 13 | 2. On the new form add the following fields: 14 | first_name - text field, Matching lead field = First Name 15 | last_name - text field, Matching lead field = Last Name 16 | email - email field, Matching lead field = Email 17 | 3. Save your form and make a note of the ID of the form. 18 | 4. Install the Mautic Woocommerce plugin and activate it. 19 | 5. In WP (from the dashboard) got to Settings>WC Mautic and enter in the url of your copy of Mautic and the ID of the form as previously noted. 20 | 6. Save and test. Enjoy! 21 | 22 | If you feel that my plugin has helped you, and you would like to make a kind donation for my work then please do :) 23 | 24 | Click here to lend your support to: Woocommerce Mautic Plugin and make a donation at pledgie.com ! 25 | -------------------------------------------------------------------------------- /woocommerce_mautic/woocommerce-mautic.php: -------------------------------------------------------------------------------- 1 | 20 |
21 |

WC Mautic Settings

22 | 23 |
24 | 25 | 26 | 27 | 28 |
29 | 30 | All of the settings to allow WC to connect to Mautic forms.

'; 46 | } 47 | 48 | function plugin_setting_string() { 49 | $options = get_option('plugin_options'); 50 | echo ""; 51 | } 52 | 53 | function plugin_setting_string2() { 54 | $options = get_option('plugin_options'); 55 | echo ""; 56 | } 57 | 58 | function plugin_options_validate($input) { 59 | $newinput['text_string'] = trim($input['text_string']); 60 | 61 | $newinput['mautic_id'] = trim($input['mautic_id']); 62 | 63 | 64 | if(!preg_match('/^(https?:\/\/)/', $newinput['text_string'])) { 65 | $newinput['text_string'] = 'http://' . $newinput['text_string']; 66 | 67 | } 68 | 69 | $newinput['text_string'] = rtrim($newinput['text_string'], '/'); 70 | return $newinput; 71 | } 72 | 73 | 74 | 75 | 76 | 77 | 78 | // hook into woocommerce update order because name etc is not available during checkout 79 | add_action( 'woocommerce_checkout_update_order_meta','order_status_changed', 10, 3 ); 80 | 81 | //add_action( 'woocommerce_checkout_update_order_meta', 'order_status_changed' 1000, 1 ); 82 | 83 | function order_status_changed( $id, $status = 'new', $new_status = 'pending' ) { 84 | 85 | // // Get WC order 86 | $order = wc_get_order( $id ); 87 | // $order = WC_Order( $id ); 88 | 89 | // // subscribe 90 | subscribe( $order->id, $order->billing_first_name, $order->billing_last_name, $order->billing_email); 91 | } 92 | 93 | 94 | 95 | 96 | 97 | 98 | function subscribe( $order_id, $first_name, $last_name, $email) { 99 | 100 | $stuff = array('first_name' => $first_name, 'last_name' => $last_name, 'email' => $email); 101 | 102 | $currentOptions = get_option( 'plugin_options', false ); 103 | 104 | pushMauticForm($stuff); 105 | 106 | } 107 | 108 | function pushMauticForm($data, $ip = null) 109 | { 110 | // Get IP from $_SERVER 111 | if (!$ip) { 112 | $ipHolders = array( 113 | 'HTTP_CLIENT_IP', 114 | 'HTTP_X_FORWARDED_FOR', 115 | 'HTTP_X_FORWARDED', 116 | 'HTTP_X_CLUSTER_CLIENT_IP', 117 | 'HTTP_FORWARDED_FOR', 118 | 'HTTP_FORWARDED', 119 | 'REMOTE_ADDR' 120 | ); 121 | foreach ($ipHolders as $key) { 122 | if (!empty($_SERVER[$key])) { 123 | $ip = $_SERVER[$key]; 124 | if (strpos($ip, ',') !== false) { 125 | // Multiple IPs are present so use the last IP which should be the most reliable IP that last connected to the proxy 126 | $ips = explode(',', $ip); 127 | array_walk($ips, create_function('&$val', '$val = trim($val);')); 128 | $ip = end($ips); 129 | } 130 | $ip = trim($ip); 131 | break; 132 | } 133 | } 134 | } 135 | 136 | $currentOptions = get_option ( 'plugin_options', 'not working'); 137 | $formId = $currentOptions['mautic_id']; 138 | $theUrl = $currentOptions['text_string']; 139 | 140 | $data['formId'] = $formId; 141 | // return has to be part of the form data array 142 | if (!isset($data['return'])) { 143 | $data['return'] = 'http://www.somewhere.com'; 144 | } 145 | $data = array('mauticform' => $data); 146 | // $url = get_option( 'mautic_url', 'not working1' ); 147 | 148 | // $theFormId = get_option( 'mautic_id', 'not working2'); 149 | 150 | 151 | // Change [path-to-mautic] to URL where your Mautic is 152 | 153 | 154 | 155 | $formUrl = $theUrl . '/form/submit?formId=' . $formId; 156 | 157 | $ch = curl_init(); 158 | curl_setopt($ch, CURLOPT_URL, $formUrl); 159 | curl_setopt($ch, CURLOPT_POST, 1); 160 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); 161 | curl_setopt($ch, CURLOPT_HTTPHEADER, array("X-Forwarded-For: $ip")); 162 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 163 | $response = curl_exec($ch); 164 | curl_close($ch); 165 | 166 | 167 | return $response; 168 | } 169 | 170 | ?> --------------------------------------------------------------------------------