choiceai - Version 0.0.9

Version Notes

Initial Code

Download this release

Release Info

Developer MineWhat Inc.
Extension choiceai
Version 0.0.9
Comparing to
See all releases


Version 0.0.9

Files changed (22) hide show
  1. app/code/community/ChoiceAI/Personalisation/Block/Base/Script.php +31 -0
  2. app/code/community/ChoiceAI/Personalisation/Block/Event/Catalog/Product/View.php +39 -0
  3. app/code/community/ChoiceAI/Personalisation/Block/Event/Checkout/Cart/Index.php +32 -0
  4. app/code/community/ChoiceAI/Personalisation/Block/Event/Checkout/Onepage/Success.php +118 -0
  5. app/code/community/ChoiceAI/Personalisation/Helper/Data.php +39 -0
  6. app/code/community/ChoiceAI/Personalisation/Model/Observer.php +66 -0
  7. app/code/community/ChoiceAI/Personalisation/controllers/ApiController.php +488 -0
  8. app/code/community/ChoiceAI/Personalisation/etc/adminhtml.xml +30 -0
  9. app/code/community/ChoiceAI/Personalisation/etc/config.xml +84 -0
  10. app/code/community/ChoiceAI/Personalisation/etc/system.xml +51 -0
  11. app/design/frontend/base/default/layout/choiceai_personalisation.xml +39 -0
  12. app/design/frontend/base/default/template/choiceai/personalisation/base/script.phtml +17 -0
  13. app/design/frontend/base/default/template/choiceai/personalisation/event/catalog/product/view.phtml +61 -0
  14. app/design/frontend/base/default/template/choiceai/personalisation/event/checkout/cart/index.phtml +17 -0
  15. app/design/frontend/base/default/template/choiceai/personalisation/event/checkout/onepage/success.phtml +21 -0
  16. app/design/frontend/default/default/layout/choiceai_personalisation.xml +39 -0
  17. app/design/frontend/default/default/template/choiceai/personalisation/base/script.phtml +17 -0
  18. app/design/frontend/default/default/template/choiceai/personalisation/event/catalog/product/view.phtml +61 -0
  19. app/design/frontend/default/default/template/choiceai/personalisation/event/checkout/cart/index.phtml +17 -0
  20. app/design/frontend/default/default/template/choiceai/personalisation/event/checkout/onepage/success.phtml +20 -0
  21. app/etc/modules/ChoiceAI_Personalisation.xml +10 -0
  22. package.xml +18 -0
app/code/community/ChoiceAI/Personalisation/Block/Base/Script.php ADDED
@@ -0,0 +1,31 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * @category ChoiceAI
5
+ * @package ChoiceAI_Personalisation
6
+ * @copyright Copyright (c) MineWhat
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ class ChoiceAI_Personalisation_Block_Base_Script extends Mage_Core_Block_Template {
10
+
11
+ protected function _construct() {
12
+ parent::_construct();
13
+ $this->setTemplate('choiceai/personalisation/base/script.phtml');
14
+ }
15
+
16
+ protected function _toHtml() {
17
+ if (!$this->helper('choiceai_personalisation')->isModuleOutputEnabled()) {
18
+ return '';
19
+ }
20
+ return parent::_toHtml();
21
+ }
22
+
23
+ public function getUser() {
24
+ if (Mage::getSingleton('customer/session')->isLoggedIn()) {
25
+ $user = Mage::getSingleton('customer/session')->getCustomer();
26
+ return $user;
27
+ }
28
+ return null;
29
+ }
30
+
31
+ }
app/code/community/ChoiceAI/Personalisation/Block/Event/Catalog/Product/View.php ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * @category ChoiceAI
5
+ * @package ChoiceAI_Personalisation
6
+ * @copyright Copyright (c) MineWhat
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ class ChoiceAI_Personalisation_Block_Event_Catalog_Product_View extends Mage_Core_Block_Template {
10
+
11
+ protected function _construct() {
12
+ parent::_construct();
13
+ $this->setTemplate('choiceai/personalisation/event/catalog/product/view.phtml');
14
+ }
15
+
16
+ public function getCurrentProduct() {
17
+ return Mage::registry('current_product');
18
+ }
19
+
20
+ public function getAssociatedProducts($_product) {
21
+
22
+ if($_product->getTypeId() == "configurable") {
23
+ $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($_product);
24
+ $simple_collection = $conf->getUsedProductCollection()->addAttributeToSelect(array('id', 'sku', 'price'))->addFilterByRequiredOptions();
25
+ return $simple_collection;
26
+ } else {
27
+ return array();
28
+ }
29
+
30
+ }
31
+
32
+ protected function _toHtml() {
33
+ if (!$this->helper('choiceai_personalisation')->isModuleOutputEnabled()) {
34
+ return '';
35
+ }
36
+ return parent::_toHtml();
37
+ }
38
+
39
+ }
app/code/community/ChoiceAI/Personalisation/Block/Event/Checkout/Cart/Index.php ADDED
@@ -0,0 +1,32 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * @category ChoiceAI
5
+ * @package ChoiceAI_Personalisation
6
+ * @copyright Copyright (c) MineWhat
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ class ChoiceAI_Personalisation_Block_Event_Checkout_Cart_Index extends Mage_Core_Block_Template {
10
+
11
+ protected function _construct() {
12
+ parent::_construct();
13
+ $this->setTemplate('choiceai/personalisation/event/checkout/cart/index.phtml');
14
+ }
15
+
16
+ public function getProductToShoppingCart() {
17
+ if (($product = Mage::getModel('core/session')->getProductToShoppingCart())) {
18
+ Mage::getModel('core/session')->unsProductToShoppingCart();
19
+ return $product;
20
+ }
21
+
22
+ return null;
23
+ }
24
+
25
+ protected function _toHtml() {
26
+ if (!$this->helper('choiceai_personalisation')->isModuleOutputEnabled()) {
27
+ return '';
28
+ }
29
+ return parent::_toHtml();
30
+ }
31
+
32
+ }
app/code/community/ChoiceAI/Personalisation/Block/Event/Checkout/Onepage/Success.php ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * @category ChoiceAI
5
+ * @package ChoiceAI_Personalisation
6
+ * @copyright Copyright (c) MineWhat
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ class ChoiceAI_Personalisation_Block_Event_Checkout_Onepage_Success extends Mage_Core_Block_Template {
10
+
11
+ protected function _construct() {
12
+ parent::_construct();
13
+ $this->setTemplate('choiceai/personalisation/event/checkout/onepage/success.phtml');
14
+ }
15
+
16
+ public function getOrderInfo() {
17
+ try {
18
+ $lastOrderId = Mage::getSingleton('checkout/session')->getLastOrderId();
19
+
20
+ $order = Mage::getSingleton('sales/order');
21
+ $order->load($lastOrderId);
22
+
23
+
24
+ if ($order && $order->getId()) {
25
+
26
+ $orderInfo['items'] = array();
27
+
28
+ $orderedItems = $order->getAllItems();
29
+
30
+ foreach($orderedItems as $item) {
31
+
32
+ if($item->getProductType() == "bundle") {
33
+
34
+ $orderInfo['items'][$item->getItemId()] = array(
35
+ 'id' => $item->getProductId(),
36
+ 'parentId' => '',
37
+ 'sku' => $item->getSku(),
38
+ 'qty' => $item->getQtyOrdered(),
39
+ 'price' => $item->getPrice(),
40
+ 'bundle' => array()
41
+ );
42
+
43
+ } else if($item->getProductType() != "configurable") {
44
+
45
+ if($orderInfo['items'][$item->getParentItemId()] != null) {
46
+ $bundleItems = $orderInfo['items'][$item->getParentItemId()]['bundle'];
47
+ $bundleItem = array(
48
+ 'pid' => $item->getProductId(),
49
+ 'sku' => $item->getSku(),
50
+ 'qty' => $item->getQtyOrdered(),
51
+ 'price' => $item->getPrice()
52
+ );
53
+ $bundleItems[] = $bundleItem;
54
+ $orderInfo['items'][$item->getParentItemId()]['bundle'] = $bundleItems;
55
+
56
+ } else {
57
+
58
+ $parentId = '';
59
+ $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($item->getProductId());
60
+ if($parentIds != null && count($parentIds) > 0) {
61
+ $parentId = $parentIds[0];
62
+ }
63
+ $orderInfo['items'][] = array(
64
+ 'id' => $item->getProductId(),
65
+ 'parentId' => $parentId,
66
+ 'sku' => $item->getSku(),
67
+ 'qty' => $item->getQtyOrdered(),
68
+ 'price' => $item->getPrice(),
69
+ 'bundle' => array()
70
+ );
71
+
72
+
73
+ }
74
+
75
+ }
76
+
77
+ }
78
+
79
+ $orderInfo['orderId'] = $order->getIncrementId();
80
+ $orderInfo['email'] = $order->getCustomerEmail();
81
+ $orderInfo['createdAt'] = $order->getCreatedAt();
82
+
83
+ $currency = $order->getOrderCurrency();
84
+ if (is_object($currency)) {
85
+ $orderInfo['currency'] = $currency->getCurrencyCode();
86
+ }
87
+ $paymentMethod = $order->getPayment()->getMethodInstance()->getTitle();
88
+ $orderInfo['paymentMethod'] = $paymentMethod;
89
+
90
+ return $orderInfo;
91
+ }
92
+ } catch (Exception $e) {}
93
+
94
+
95
+ return null;
96
+ }
97
+
98
+ protected function _toHtml() {
99
+ if (!$this->helper('choiceai_personalisation')->isModuleOutputEnabled() || !$this->isOrderConfirmation()) {
100
+ return '';
101
+ }
102
+ return parent::_toHtml();
103
+ }
104
+
105
+ protected function isOrderConfirmation() {
106
+ return strpos($this->_getRouteName(), 'checkout') !== false
107
+ && $this->_getActionName() == 'success';
108
+ }
109
+
110
+ protected function _getRouteName() {
111
+ return $this->getRequest()->getRequestedRouteName();
112
+ }
113
+
114
+ protected function _getActionName() {
115
+ return $this->getRequest()->getRequestedActionName();
116
+ }
117
+
118
+ }
app/code/community/ChoiceAI/Personalisation/Helper/Data.php ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * @category ChoiceAI
5
+ * @package ChoiceAI_Personalisation
6
+ * @copyright Copyright (c) MineWhat
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ class ChoiceAI_Personalisation_Helper_Data extends Mage_Core_Helper_Data {
10
+
11
+ const CONFIG_ACTIVE = 'choiceai_personalisation/settings/active';
12
+ const CONFIG_API_KEY = 'choiceai_personalisation/settings/api_key';
13
+
14
+ public function isModuleEnabled($moduleName = null) {
15
+ if (Mage::getStoreConfig(self::CONFIG_ACTIVE) == '0') {
16
+ return false;
17
+ }
18
+
19
+ return parent::isModuleEnabled($moduleName = null);
20
+ }
21
+
22
+ public function getBaseScript($store = null) {
23
+
24
+ $base_script = "";
25
+
26
+ try {
27
+
28
+ $base_script = "\n<!-- ChoiceAI Script begins -->\n<script type='text/javascript'>!function(){function t(){if(!window.MWSDK){var t=document.createElement('script'),n='beaconhttp.minewhat.com';t.type='text/javascript',t.async=!0,'https:'==location.protocol&&(n='d2ft2mgd1hddln.cloudfront.net'),t.src='//'+n+'/site/ethno/ORG_HANDLE/minewhat.js';var e=document.getElementsByTagName('script')[0];e.parentNode.insertBefore(t,e)}}window.MWSDK&&window.MWSDK.reinit&&window.MWSDK.reinit(),window.attachEvent?window.attachEvent('onload',t):window.addEventListener('load',t,!1)}();</script>\n<!-- ChoiceAI Script ends -->\n";
29
+ $base_script = str_replace("ORG_HANDLE", split("_", Mage::getStoreConfig(self::CONFIG_API_KEY, $store))[0], $base_script);
30
+
31
+ } catch (Exception $e) {
32
+ $base_script = "";
33
+ }
34
+
35
+ return $base_script;
36
+
37
+ }
38
+
39
+ }
app/code/community/ChoiceAI/Personalisation/Model/Observer.php ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ class ChoiceAI_Personalisation_Model_Observer {
4
+
5
+ const CONFIG_ACTIVE = 'choiceai_personalisation/settings/active';
6
+ const CONFIG_API_KEY = 'choiceai_personalisation/settings/api_key';
7
+
8
+ public function onSaveSettings($observer) {
9
+
10
+ $enabled = Mage::getStoreConfig(self::CONFIG_ACTIVE);
11
+ $api_key = Mage::getStoreConfig(self::CONFIG_API_KEY);
12
+ $store_url = Mage::getBaseUrl();
13
+ $magento_version = Mage::getVersion();
14
+
15
+ file_get_contents("https://app.choice.ai/stats/magentoinstall?enabled=".$enabled."&api_key=".$api_key."&store_url=".$store_url."&magentoversion=".$magento_version);
16
+
17
+ }
18
+
19
+ public function logCartAdd($observer) {
20
+
21
+ if (!$observer->getQuoteItem()->getProduct()->getId()) {
22
+ return;
23
+ }
24
+
25
+ $product = $observer->getProduct();
26
+ $id = $observer->getQuoteItem()->getProduct()->getId();
27
+ $bundle = array();
28
+
29
+ if($product->getTypeId() == 'bundle') {
30
+
31
+ $id = $product->getId();
32
+ $optionCollection = $product->getTypeInstance()->getOptionsCollection();
33
+ $selectionCollection = $product->getTypeInstance()->getSelectionsCollection($product->getTypeInstance()->getOptionsIds());
34
+ $options = $optionCollection->appendSelections($selectionCollection);
35
+ foreach( $options as $option )
36
+ {
37
+ $_selections = $option->getSelections();
38
+ foreach( $_selections as $selection )
39
+ {
40
+ $bundleItem = array();
41
+ $bundleItem['pid'] = $selection->getId();
42
+ $bundleItem['sku'] = $selection->getSku();
43
+ $bundleItem['price'] = $selection->getPrice();
44
+ $bundle[] = $bundleItem;
45
+ }
46
+ }
47
+
48
+ }
49
+
50
+ $parentId = '';
51
+ $parentIds = Mage::getModel('catalog/product_type_configurable')->getParentIdsByChild($id);
52
+ if($parentIds != null && count($parentIds) > 0) {
53
+ $parentId = $parentIds[0];
54
+ }
55
+ Mage::getModel('core/session')->setProductToShoppingCart(
56
+ array(
57
+ 'id' => $id,
58
+ 'sku' => $product->getSku(),
59
+ 'parentId' => $parentId,
60
+ 'qty' => $product->getQty(),
61
+ 'bundle' => $bundle
62
+ )
63
+ );
64
+
65
+ }
66
+ }
app/code/community/ChoiceAI/Personalisation/controllers/ApiController.php ADDED
@@ -0,0 +1,488 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+
3
+ /**
4
+ * @category ChoiceAI
5
+ * @package ChoiceAI_Personalisation
6
+ * @copyright Copyright (c) MineWhat
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+
10
+ class ChoiceAI_Personalisation_ApiController extends Mage_Core_Controller_Front_Action {
11
+
12
+ const CONFIG_API_KEY = 'choiceai_personalisation/settings/api_key';
13
+
14
+ public function _authorise() {
15
+
16
+ $API_KEY = Mage::getStoreConfig(self::CONFIG_API_KEY);
17
+
18
+ // Check for api access
19
+ if(!$API_KEY && strlen($API_KEY) === 0) {
20
+ // Api access disabled
21
+ $this->getResponse()
22
+ ->setBody(json_encode(array('status' => 'error', 'message' => 'API access disabled', 'version' => 2)))
23
+ ->setHttpResponseCode(403)
24
+ ->setHeader('Content-type', 'application/json', true);
25
+ return false;
26
+ }
27
+
28
+ $authHeader = $this->getRequest()->getHeader('mwauth');
29
+
30
+ // fallback
31
+ if (!$authHeader || strlen($authHeader) == 0) {
32
+ $authHeader = $this->getRequest()->getParam('mwauth');
33
+ }
34
+
35
+ if (!$authHeader) {
36
+ Mage::log('Unable to extract authorization header from request', null, 'choiceai.log');
37
+ // Internal server error
38
+ $this->getResponse()
39
+ ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error, Authorization header not found', 'version' => 2)))
40
+ ->setHttpResponseCode(500)
41
+ ->setHeader('Content-type', 'application/json', true);
42
+ return false;
43
+ }
44
+
45
+ if(trim($authHeader) !== trim($API_KEY)) {
46
+ // Api access denied
47
+ $this->getResponse()
48
+ ->setBody(json_encode(array('status' => 'error', 'message' => 'Api access denied', 'version' => 2)))
49
+ ->setHttpResponseCode(401)
50
+ ->setHeader('Content-type', 'application/json', true);
51
+ return false;
52
+ }
53
+
54
+ return true;
55
+
56
+ }
57
+
58
+ public function ordersAction() {
59
+
60
+ try {
61
+
62
+ if(!$this->_authorise()) {
63
+ return $this;
64
+ }
65
+
66
+ $sections = explode('/', trim($this->getRequest()->getPathInfo(), '/'));
67
+
68
+ if(isset($sections[3])) {
69
+ // Looking for a specific order
70
+ $orderId = $sections[3];
71
+
72
+ $order = Mage::getModel('sales/order')->load($orderId, 'increment_id');
73
+
74
+ $extras = $this->getRequest()->getParam('extras');
75
+ $debug = $this->getRequest()->getParam('debug', 'false') === 'true';
76
+ if($extras && strlen($extras)) {
77
+ $extras = explode(',', $extras);
78
+ for($i = 0;$i < sizeof($extras);$i++) {
79
+ $extras[$i] = trim($extras[$i]);
80
+ }
81
+ }
82
+
83
+ $items = array();
84
+
85
+ $orderItems = $order->getItemsCollection()->load();
86
+
87
+ foreach($orderItems as $key => $orderItem) {
88
+ $items[] = array(
89
+ 'name' => $orderItem->getName(),
90
+ 'pid' => $orderItem->getProductId(),
91
+ 'sku' => $orderItem->getSku(),
92
+ 'qty' => $orderItem->getQtyOrdered(),
93
+ 'price' => $orderItem->getPrice()
94
+ );
95
+ }
96
+
97
+ $responseObj = array(
98
+ 'order_id' => $orderId,
99
+ 'items' => $items,
100
+ 'ip' => $order->getRemoteIp()
101
+ );
102
+
103
+ $attributes = $order->debug();
104
+ if($debug) {
105
+ $responseObj['extras'] = $attributes;
106
+ } else {
107
+ foreach($extras as $key) {
108
+ $responseObj['extras'][$key] = $attributes[$key];
109
+ }
110
+ }
111
+
112
+ $responseObj['version'] = 2;
113
+ $this->getResponse()
114
+ ->setBody(json_encode($responseObj))
115
+ ->setHttpResponseCode(200)
116
+ ->setHeader('Content-type', 'application/json', true);
117
+ } else {
118
+ // Looking for a list of orders
119
+ $currentTime = time();
120
+ $fromDate = $this->getRequest()->getParam('fromDate', date('Y-m-d', ($currentTime - 86400)));
121
+ $toDate = $this->getRequest()->getParam('toDate', date('Y-m-d', $currentTime));
122
+
123
+ $orders = array();
124
+
125
+ $ordersCollection = Mage::getModel('sales/order')->getCollection()
126
+ //->addFieldToFilter('status', 'complete')
127
+ ->addAttributeToSelect('customer_email')
128
+ ->addAttributeToSelect('created_at')
129
+ ->addAttributeToSelect('increment_id')
130
+ ->addAttributeToSelect('status')
131
+ ->addAttributeToFilter('created_at', array('from' => $fromDate, 'to' => $toDate))
132
+ ;
133
+
134
+ foreach ($ordersCollection as $order) {
135
+ $orders[] = array(
136
+ 'order_id' => $order->getIncrementId(),
137
+ 'status' => $order->getStatus(),
138
+ 'email' => $order->getCustomerEmail(),
139
+ 'created_at' => $order->getCreatedAt()
140
+ );
141
+ }
142
+
143
+ $this->getResponse()
144
+ ->setBody(json_encode(array('orders' => $orders, 'fromDate' => $fromDate, 'toDate' => $toDate, 'version' => 2)))
145
+ ->setHttpResponseCode(200)
146
+ ->setHeader('Content-type', 'application/json', true);
147
+
148
+ }
149
+
150
+ } catch(Exception $e) {
151
+ $this->getResponse()
152
+ ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => 2)))
153
+ ->setHttpResponseCode(500)
154
+ ->setHeader('Content-type', 'application/json', true);
155
+ }
156
+
157
+ return this;
158
+
159
+ }
160
+
161
+ public function productsAction() {
162
+
163
+ try {
164
+
165
+ if(!$this->_authorise()) {
166
+ return $this;
167
+ }
168
+
169
+ $sections = explode('/', trim($this->getRequest()->getPathInfo(), '/'));
170
+ $products = array();
171
+
172
+ $attributes = array(
173
+ 'name',
174
+ 'sku',
175
+ 'image',
176
+ 'manufacturer',
177
+ 'price',
178
+ 'final_price',
179
+ 'special_price',
180
+ 'short_description'
181
+ );
182
+
183
+ $extras = $this->getRequest()->getParam('extras');
184
+ $debug = $this->getRequest()->getParam('debug', 'false') === 'true';
185
+
186
+ if($extras && strlen($extras)) {
187
+ $extras = explode(',', $extras);
188
+ for($i = 0;$i < sizeof($extras);$i++) {
189
+ $extras[$i] = trim($extras[$i]);
190
+ $attributes[] = $extras[$i];
191
+ }
192
+ }
193
+
194
+ if(isset($sections[3])) {
195
+ // Looking for a specific product
196
+ $productId = $sections[3];
197
+
198
+ $product = Mage::getModel('catalog/product')->load($productId);
199
+
200
+ $product = $this->getFormattedProduct($product, $extras, $debug);
201
+ if($product !== null) {
202
+ $products[] = $product;
203
+ }
204
+
205
+ } else {
206
+ // Looking for a list of products
207
+ $limit = $this->getRequest()->getParam('limit', 100);
208
+ $offset = $this->getRequest()->getParam('offset', 1);
209
+
210
+ $productsCollection = Mage::getModel('catalog/product')->getCollection();
211
+ $productsCollection
212
+ ->addAttributeToSelect($attributes)
213
+ ->getSelect()->limit($limit, $offset) //we can specify how many products we want to show on this page
214
+ ;
215
+
216
+ foreach($productsCollection as $product) {
217
+ $product = $this->getFormattedProduct($product, $extras, $debug);
218
+ if($product !== null) {
219
+ $products[] = $product;
220
+ }
221
+ }
222
+
223
+ }
224
+
225
+ $currency = Mage::app()->getStore()->getCurrentCurrencyCode();
226
+
227
+ $this->getResponse()
228
+ ->setBody(json_encode(array('products' => $products, 'currency' => $currency, 'version' => 2)))
229
+ ->setHttpResponseCode(200)
230
+ ->setHeader('Content-type', 'application/json', true);
231
+
232
+
233
+ } catch(Exception $e) {
234
+ $this->getResponse()
235
+ ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => 2)))
236
+ ->setHttpResponseCode(500)
237
+ ->setHeader('Content-type', 'application/json', true);
238
+ }
239
+
240
+ return $this;
241
+
242
+ }
243
+
244
+
245
+ public function categoriesAction() {
246
+
247
+ try {
248
+
249
+ if(!$this->_authorise()) {
250
+ return $this;
251
+ }
252
+
253
+ $attributes = array(
254
+ 'id',
255
+ 'name',
256
+ 'image',
257
+ 'url',
258
+ 'level',
259
+ 'is_active',
260
+ 'created_at',
261
+ 'updated_at'
262
+ );
263
+
264
+ $sections = explode('/', trim($this->getRequest()->getPathInfo(), '/'));
265
+ $categories = array();
266
+
267
+ $level = $this->getRequest()->getParam('level');
268
+ $active = $this->getRequest()->getParam('active', 'false') === 'true';
269
+
270
+ if($level && strlen($level)) {
271
+ $level = intval($level);
272
+ } else {
273
+ $level = null;
274
+ }
275
+
276
+ if(isset($sections[3])) {
277
+ // Looking for a specific category
278
+ $categoryId = $sections[3];
279
+
280
+ $category = Mage::getModel('catalog/category')->load($categoryId);
281
+
282
+ $category = $this->getFormattedCategory($category);
283
+ if($category !== null) {
284
+ $categories[] = $category;
285
+ }
286
+
287
+ } else {
288
+ // Looking for a list of categories
289
+ $limit = $this->getRequest()->getParam('limit', 100);
290
+ $offset = $this->getRequest()->getParam('offset', 1);
291
+
292
+ $categoriesCollection = Mage::getModel('catalog/category')->getCollection();
293
+
294
+ if($level != null) {
295
+ $categoriesCollection
296
+ ->addAttributeToFilter('level', $level) //we can specify the level of categories to be fetched
297
+ ;
298
+ }
299
+
300
+ if($active != null) {
301
+ $categoriesCollection
302
+ ->addAttributeToFilter('is_active', 1) //if you want only active categories
303
+ ;
304
+ }
305
+
306
+ $categoriesCollection
307
+ ->addAttributeToSelect($attributes)
308
+ ->getSelect()->limit($limit, $offset) //we can specify how many categories we want to show on this page
309
+ ;
310
+
311
+ foreach($categoriesCollection as $category) {
312
+ $category = $this->getFormattedCategory($category);
313
+ if($category !== null) {
314
+ $categories[] = $category;
315
+ }
316
+ }
317
+
318
+ }
319
+
320
+ $this->getResponse()
321
+ ->setBody(json_encode(array('categories' => $categories, 'version' => 2)))
322
+ ->setHttpResponseCode(200)
323
+ ->setHeader('Content-type', 'application/json', true);
324
+
325
+
326
+ } catch(Exception $e) {
327
+ $this->getResponse()
328
+ ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => 2)))
329
+ ->setHttpResponseCode(500)
330
+ ->setHeader('Content-type', 'application/json', true);
331
+ }
332
+
333
+ return $this;
334
+
335
+ }
336
+
337
+
338
+ public function stockAction() {
339
+
340
+ try {
341
+
342
+ if(!$this->_authorise()) {
343
+ return $this;
344
+ }
345
+
346
+ $productId = $this->getRequest()->getParam('pid');
347
+ $sku = $this->getRequest()->getParam('sku', 'false') === 'true';
348
+
349
+
350
+ if(!$productId || strlen($productId) <= 0) {
351
+
352
+ $this->getResponse()
353
+ ->setBody(json_encode(array('status' => 'error', 'message' => 'product id required', 'version' => 2)))
354
+ ->setHttpResponseCode(500)
355
+ ->setHeader('Content-type', 'application/json', true);
356
+
357
+ } else {
358
+
359
+ // load product if sku is given
360
+ if($sku) {
361
+ $product = Mage::getModel('catalog/product')->loadByAttribute('sku', $productId);
362
+ if($product == null) {
363
+ $this->getResponse()
364
+ ->setBody(json_encode(array('status' => 'error', 'message' => 'invalid sku', 'version' => 2)))
365
+ ->setHttpResponseCode(500)
366
+ ->setHeader('Content-type', 'application/json', true);
367
+ return $this;
368
+ }
369
+ $productId = $product->getId();
370
+ }
371
+
372
+ // get stock info
373
+ $stockObj = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
374
+
375
+ $stock = $stockObj->getQty();
376
+
377
+ $this->getResponse()
378
+ ->setBody(json_encode(array('id' => $productId, 'stock' => $stock, 'version' => 2)))
379
+ ->setHttpResponseCode(200)
380
+ ->setHeader('Content-type', 'application/json', true);
381
+
382
+ }
383
+
384
+ } catch(Exception $e) {
385
+ $this->getResponse()
386
+ ->setBody(json_encode(array('status' => 'error', 'message' => 'Internal server error', 'version' => 2)))
387
+ ->setHttpResponseCode(500)
388
+ ->setHeader('Content-type', 'application/json', true);
389
+ }
390
+
391
+ return $this;
392
+
393
+ }
394
+
395
+
396
+ private function getFormattedProduct($product, $extras, $debug) {
397
+
398
+ $formattedProduct = null;
399
+
400
+ try {
401
+ $formattedProduct = array(
402
+ 'id' => $product->getId(),
403
+ 'sku' => $product->getSku(),
404
+ 'name' => $product->getName(),
405
+ 'cat' => array(),
406
+ 'manufacturer' => $product->getAttributeText('manufacturer'),
407
+ 'price' => $product->getPrice(),
408
+ 'final_price' => $product->getFinalPrice(),
409
+ 'special_price' => $product->getSpecialPrice(),
410
+ 'image' => $product->getImageUrl(),
411
+ 'url' => $product->getProductUrl(),
412
+ 'info' => $product->getShortDescription(),
413
+ 'status' => $product->getStatus(),
414
+ 'type' => $product->getTypeId(),
415
+ 'created_at' => $product->getCreatedAt(),
416
+ 'updated_at' => $product->getUpdatedAt()
417
+ );
418
+ if(!$formattedProduct['manufacturer'] || strlen($formattedProduct['manufacturer']) === 0) {
419
+ $product = Mage::getModel('catalog/product')->load($product->getId());
420
+ $formattedProduct['manufacturer'] = $product->getAttributeText('manufacturer');
421
+ }
422
+
423
+ if($formattedProduct['type'] == "configurable") {
424
+ // get associated product ids
425
+ $associatedProducts = Mage::getModel('catalog/product_type_configurable')->getChildrenIds($formattedProduct['id']);
426
+ $formattedProduct['associated_products'] = $associatedProducts;
427
+ }
428
+
429
+ // get stock info
430
+ $stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product);
431
+ $formattedProduct['stock'] = $stock->getQty();
432
+
433
+ if($debug) {
434
+ $attributes = $product->getAttributes();
435
+ foreach($attributes as $key => $value) {
436
+ $formattedProduct['extras'][$key] = $product->getAttributeText($key);
437
+ }
438
+ } else {
439
+ foreach($extras as $key) {
440
+ $formattedProduct['extras'][$key] = $product->getAttributeText($key);
441
+ }
442
+ }
443
+
444
+ $categories = $product->getCategoryCollection()
445
+ ->addAttributeToSelect('id')
446
+ ->addAttributeToSelect('name')
447
+ ->addAttributeToSelect('path')
448
+ ->addAttributeToSelect('level');
449
+ foreach($categories as $category) {
450
+ $formattedCategory = array();
451
+ $formattedCategory['id'] = $category->getId();
452
+ $formattedCategory['name'] = $category->getName();
453
+ $formattedCategory['level'] = $category->getLevel();
454
+ $formattedCategory['path'] = $category->getPath();
455
+ $formattedProduct['cat'][$formattedCategory['id']] = $formattedCategory;
456
+ }
457
+
458
+ } catch(Exception $e) {}
459
+
460
+ return $formattedProduct;
461
+
462
+ }
463
+
464
+ private function getFormattedCategory($category) {
465
+
466
+ $formattedCategory = null;
467
+
468
+ try {
469
+
470
+ $formattedCategory = array(
471
+ 'id' => $category->getId(),
472
+ 'name' => $category->getName(),
473
+ 'image' => $category->getImageUrl(),
474
+ 'url' => $category->getUrl(),
475
+ 'level' => $category->getLevel(),
476
+ 'is_active' => $category->getIsActive(),
477
+ 'created_at' => $category->getCreatedAt(),
478
+ 'updated_at' => $category->getUpdatedAt()
479
+ );
480
+
481
+ } catch(Exception $e) {}
482
+
483
+ return $formattedCategory;
484
+
485
+ }
486
+
487
+ }
488
+
app/code/community/ChoiceAI/Personalisation/etc/adminhtml.xml ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * @category ChoiceAI
5
+ * @package ChoiceAI_Personalisation
6
+ * @copyright Copyright (c) MineWhat
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ -->
10
+ <config>
11
+ <acl>
12
+ <resources>
13
+ <admin>
14
+ <children>
15
+ <system>
16
+ <children>
17
+ <config>
18
+ <children>
19
+ <choiceai_personalisation translate="title" module="choiceai_personalisation">
20
+ <title>Choice AI</title>
21
+ </choiceai_personalisation>
22
+ </children>
23
+ </config>
24
+ </children>
25
+ </system>
26
+ </children>
27
+ </admin>
28
+ </resources>
29
+ </acl>
30
+ </config>
app/code/community/ChoiceAI/Personalisation/etc/config.xml ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * @category ChoiceAI
5
+ * @package ChoiceAI_Personalisation
6
+ * @copyright Copyright (c) MineWhat
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ -->
10
+ <config>
11
+ <modules>
12
+ <ChoiceAI_Personalisation>
13
+ <version>0.0.9</version>
14
+ </ChoiceAI_Personalisation>
15
+ </modules>
16
+ <global>
17
+ <models>
18
+ <choiceai_personalisation>
19
+ <class>ChoiceAI_Personalisation_Model</class>
20
+ </choiceai_personalisation>
21
+ </models>
22
+ <blocks>
23
+ <choiceai_personalisation>
24
+ <class>ChoiceAI_Personalisation_Block</class>
25
+ </choiceai_personalisation>
26
+ </blocks>
27
+ <helpers>
28
+ <choiceai_personalisation>
29
+ <class>ChoiceAI_Personalisation_Helper</class>
30
+ </choiceai_personalisation>
31
+ </helpers>
32
+
33
+
34
+ <events>
35
+ <admin_system_config_changed_section_choiceai_personalisation>
36
+ <observers>
37
+ <choiceai_personalisation_save_settings>
38
+ <type>singleton</type>
39
+ <class>choiceai_personalisation/observer</class>
40
+ <method>onSaveSettings</method>
41
+ </choiceai_personalisation_save_settings>
42
+ </observers>
43
+ </admin_system_config_changed_section_choiceai_personalisation>
44
+ </events>
45
+
46
+ </global>
47
+ <frontend>
48
+ <routers>
49
+ <personalisation>
50
+ <use>standard</use>
51
+ <args>
52
+ <module>ChoiceAI_Personalisation</module>
53
+ <frontName>choiceai</frontName>
54
+ </args>
55
+ </personalisation>
56
+ </routers>
57
+ <layout>
58
+ <updates>
59
+ <choiceai_personalisation>
60
+ <file>choiceai_personalisation.xml</file>
61
+ </choiceai_personalisation>
62
+ </updates>
63
+ </layout>
64
+ <events>
65
+ <checkout_cart_product_add_after>
66
+ <observers>
67
+ <choiceai_personalisation_log_cart_add>
68
+ <type>singleton</type>
69
+ <class>choiceai_personalisation/observer</class>
70
+ <method>logCartAdd</method>
71
+ </choiceai_personalisation_log_cart_add>
72
+ </observers>
73
+ </checkout_cart_product_add_after>
74
+ </events>
75
+ </frontend>
76
+ <default>
77
+ <choiceai_personalisation>
78
+ <settings>
79
+ <active>0</active>
80
+ <base_script></base_script>
81
+ </settings>
82
+ </choiceai_personalisation>
83
+ </default>
84
+ </config>
app/code/community/ChoiceAI/Personalisation/etc/system.xml ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * @category ChoiceAI
5
+ * @package ChoiceAI_Personalisation
6
+ * @copyright Copyright (c) MineWhat
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ -->
10
+ <config>
11
+ <sections>
12
+ <choiceai_personalisation translate="label" module="choiceai_personalisation">
13
+ <label>Choice AI</label>
14
+ <tab>service</tab>
15
+ <frontend_type>text</frontend_type>
16
+ <sort_order>200</sort_order>
17
+ <show_in_default>1</show_in_default>
18
+ <show_in_website>1</show_in_website>
19
+ <show_in_store>1</show_in_store>
20
+ <groups>
21
+ <settings translate="label">
22
+ <label>ChoiceAI Settings</label>
23
+ <frontend_type>text</frontend_type>
24
+ <sort_order>10</sort_order>
25
+ <show_in_default>1</show_in_default>
26
+ <show_in_website>1</show_in_website>
27
+ <show_in_store>1</show_in_store>
28
+ <fields>
29
+ <active translate="label">
30
+ <label>Enabled</label>
31
+ <frontend_type>select</frontend_type>
32
+ <source_model>adminhtml/system_config_source_yesno</source_model>
33
+ <sort_order>10</sort_order>
34
+ <show_in_default>1</show_in_default>
35
+ <show_in_website>1</show_in_website>
36
+ <show_in_store>1</show_in_store>
37
+ </active>
38
+ <api_key translate="label">
39
+ <label>Domain Key</label>
40
+ <frontend_type>text</frontend_type>
41
+ <sort_order>20</sort_order>
42
+ <show_in_default>1</show_in_default>
43
+ <show_in_website>1</show_in_website>
44
+ <show_in_store>1</show_in_store>
45
+ </api_key>
46
+ </fields>
47
+ </settings>
48
+ </groups>
49
+ </choiceai_personalisation>
50
+ </sections>
51
+ </config>
app/design/frontend/base/default/layout/choiceai_personalisation.xml ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * @category ChoiceAI
5
+ * @package ChoiceAI_Personalisation
6
+ * @copyright Copyright (c) MineWhat
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ -->
10
+ <layout version="0.1.0">
11
+
12
+ <catalog_product_view>
13
+ <reference name="head">
14
+ <block type="choiceai_personalisation/event_catalog_product_view" name="choiceai_personalisation_event_catalog_product_view" />
15
+ </reference>
16
+ </catalog_product_view>
17
+
18
+ <checkout_cart_index>
19
+ <reference name="head">
20
+ <block type="choiceai_personalisation/event_checkout_cart_index" name="choiceai_personalisation_event_checkout_cart_index" />
21
+ </reference>
22
+ </checkout_cart_index>
23
+
24
+ <!-- <checkout_onepage_success>
25
+ <reference name="head">
26
+ <block type="choiceai_personalisation/event_checkout_onepage_success" name="choiceai_personalisation_event_checkout_onepage_success" />
27
+ </reference>
28
+ </checkout_onepage_success> -->
29
+
30
+ <default>
31
+ <reference name="head">
32
+ <block type="choiceai_personalisation/base_script" name="choiceai_personalisation_base_script" />
33
+ </reference>
34
+ <reference name="head">
35
+ <block type="choiceai_personalisation/event_checkout_onepage_success" name="choiceai_personalisation_event_checkout_onepage_success" />
36
+ </reference>
37
+ </default>
38
+
39
+ </layout>
app/design/frontend/base/default/template/choiceai/personalisation/base/script.phtml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php echo Mage::helper('choiceai_personalisation')->getBaseScript() ?>
2
+ <?php
3
+ $user = $this->getUser();
4
+ if($user) {
5
+ $email = $user->getEmail();
6
+ if ($email && strlen($email) > 0) {
7
+ ?>
8
+ <script type="text/javascript">
9
+ //<![CDATA[
10
+ window._mwapi = window._mwapi || [];
11
+ _mwapi.push(['trackUser', '<?php echo $email ?>']);
12
+ //]]>
13
+ </script>
14
+ <?php
15
+ }
16
+ }
17
+ ?>
app/design/frontend/base/default/template/choiceai/personalisation/event/catalog/product/view.phtml ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @category ChoiceAI
4
+ * @package ChoiceAI_Personalisation
5
+ * @copyright Copyright (c) MineWhat
6
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
7
+ */
8
+ ?>
9
+ <?php $_product = $this->getCurrentProduct() ?>
10
+ <?php if ($_product) { ?>
11
+ <?php
12
+ $_associated_products = $this->getAssociatedProducts($_product);
13
+ $associated_ids = array();
14
+
15
+ $mwdata = array();
16
+ $mwdata['product'] = array();
17
+ $mwdata['associated_products'] = array();
18
+
19
+ $mwdata['product']['id'] = $_product->getId();
20
+ $mwdata['product']['sku'] = $_product->getSku();
21
+
22
+ $categories = $_product->getCategoryCollection()->addAttributeToSelect('name');
23
+
24
+ $mwdata['product']['cat'] = array();
25
+ foreach($categories as $category) {
26
+ $mwdata['product']['cat'][] = $category->getName();
27
+ }
28
+
29
+ $mwdata['product']['price'] = $_product->getPrice();
30
+
31
+ foreach($_associated_products as $simple_product) {
32
+ $product_info = array();
33
+ $associated_ids[] = $simple_product->getId();
34
+ $product_info['id'] = $simple_product->getId();
35
+ $product_info['sku'] = $simple_product->getSku();
36
+ $product_info['price'] = $simple_product->getPrice();
37
+ $mwdata['associated_products'][] = $product_info;
38
+ }
39
+
40
+ ?>
41
+ <script type="text/choiceai_data">
42
+ //<![CDATA[
43
+
44
+ <?php
45
+ try {
46
+ echo json_encode($mwdata);
47
+ } catch(Exception $exception) {
48
+
49
+ }
50
+ ?>
51
+
52
+ //]]>
53
+ </script>
54
+
55
+ <script type="text/javascript">
56
+ //<![CDATA[
57
+ var _mwapi = _mwapi || [];
58
+ _mwapi.push(['trackEvent', 'product', {pid: '<?php echo $_product->getId() ?>', associated_ids: '<?php echo json_encode($associated_ids) ?>'}]);
59
+ //]]>
60
+ </script>
61
+ <?php } ?>
app/design/frontend/base/default/template/choiceai/personalisation/event/checkout/cart/index.phtml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @category ChoiceAI
4
+ * @package ChoiceAI_Personalisation
5
+ * @copyright Copyright (c) MineWhat
6
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
7
+ */
8
+ ?>
9
+ <?php $_product = $this->getProductToShoppingCart() ?>
10
+ <?php if ($_product && $_product["id"]) { ?>
11
+ <script type="text/javascript">
12
+ //<![CDATA[
13
+ var _mwapi = _mwapi || [];
14
+ _mwapi.push(['trackEvent', 'addtocart', {pid: '<?php echo $_product["id"] ?>', sku: '<?php echo $_product["sku"] ?>', parent_pid: '<?php echo $_product["parentId"] ?>', qty: '<?php echo $_product["qty"] ?>', bundle: '<?php echo json_encode($_product["bundle"]) ?>'}]);
15
+ //]]>
16
+ </script>
17
+ <?php } ?>
app/design/frontend/base/default/template/choiceai/personalisation/event/checkout/onepage/success.phtml ADDED
@@ -0,0 +1,21 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @category ChoiceAI
4
+ * @package ChoiceAI_Personalisation
5
+ * @copyright Copyright (c) MineWhat
6
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
7
+ */
8
+ ?>
9
+ <?php if (($orderInfo = $this->getOrderInfo())) { ?>
10
+ <script type="text/javascript">
11
+ //<![CDATA[
12
+ var _mwapi = _mwapi || [];
13
+ var products = [];
14
+ <?php foreach ($orderInfo['items'] as $product) { ?>
15
+ products.push({pid: '<?php echo $product["id"] ?>', qty: '<?php echo intval($product["qty"]) ?>', sku: '<?php echo $product["sku"] ?>', price: '<?php echo $product["price"] ?>', parent_pid: '<?php echo $product["parentId"] ?>', bundle: '<?php echo json_encode($product["bundle"]) ?>'});
16
+ <?php } ?>
17
+ _mwapi.push(['trackEvent', 'buy', {products: products, order: {order_number: '<?php echo $orderInfo["orderId"] ?>', payment: '<?php echo $orderInfo["paymentMethod"] ?>', created_at: '<?php echo $orderInfo["createdAt"] ?>', email: '<?php echo $orderInfo["email"] ?>'}, platform: 'magento', currency: '<?php echo $orderInfo["currency"] ?>'}]);
18
+
19
+ //]]>
20
+ </script>
21
+ <?php } ?>
app/design/frontend/default/default/layout/choiceai_personalisation.xml ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <!--
3
+ /**
4
+ * @category ChoiceAI
5
+ * @package ChoiceAI_Personalisation
6
+ * @copyright Copyright (c) MineWhat
7
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
8
+ */
9
+ -->
10
+ <layout version="0.1.0">
11
+
12
+ <catalog_product_view>
13
+ <reference name="head">
14
+ <block type="choiceai_personalisation/event_catalog_product_view" name="choiceai_personalisation_event_catalog_product_view" />
15
+ </reference>
16
+ </catalog_product_view>
17
+
18
+ <checkout_cart_index>
19
+ <reference name="head">
20
+ <block type="choiceai_personalisation/event_checkout_cart_index" name="choiceai_personalisation_event_checkout_cart_index" />
21
+ </reference>
22
+ </checkout_cart_index>
23
+
24
+ <!-- <checkout_onepage_success>
25
+ <reference name="head">
26
+ <block type="choiceai_personalisation/event_checkout_onepage_success" name="choiceai_personalisation_event_checkout_onepage_success" />
27
+ </reference>
28
+ </checkout_onepage_success> -->
29
+
30
+ <default>
31
+ <reference name="head">
32
+ <block type="choiceai_personalisation/base_script" name="choiceai_personalisation_base_script" />
33
+ </reference>
34
+ <reference name="head">
35
+ <block type="choiceai_personalisation/event_checkout_onepage_success" name="choiceai_personalisation_event_checkout_onepage_success" />
36
+ </reference>
37
+ </default>
38
+
39
+ </layout>
app/design/frontend/default/default/template/choiceai/personalisation/base/script.phtml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php echo Mage::helper('choiceai_personalisation')->getBaseScript() ?>
2
+ <?php
3
+ $user = $this->getUser();
4
+ if($user) {
5
+ $email = $user->getEmail();
6
+ if ($email && strlen($email) > 0) {
7
+ ?>
8
+ <script type="text/javascript">
9
+ //<![CDATA[
10
+ window._mwapi = window._mwapi || [];
11
+ _mwapi.push(['trackUser', '<?php echo $email ?>']);
12
+ //]]>
13
+ </script>
14
+ <?php
15
+ }
16
+ }
17
+ ?>
app/design/frontend/default/default/template/choiceai/personalisation/event/catalog/product/view.phtml ADDED
@@ -0,0 +1,61 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @category ChoiceAI
4
+ * @package ChoiceAI_Personalisation
5
+ * @copyright Copyright (c) MineWhat
6
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
7
+ */
8
+ ?>
9
+ <?php $_product = $this->getCurrentProduct() ?>
10
+ <?php if ($_product) { ?>
11
+ <?php
12
+ $_associated_products = $this->getAssociatedProducts($_product);
13
+ $associated_ids = array();
14
+
15
+ $mwdata = array();
16
+ $mwdata['product'] = array();
17
+ $mwdata['associated_products'] = array();
18
+
19
+ $mwdata['product']['id'] = $_product->getId();
20
+ $mwdata['product']['sku'] = $_product->getSku();
21
+
22
+ $categories = $_product->getCategoryCollection()->addAttributeToSelect('name');
23
+
24
+ $mwdata['product']['cat'] = array();
25
+ foreach($categories as $category) {
26
+ $mwdata['product']['cat'][] = $category->getName();
27
+ }
28
+
29
+ $mwdata['product']['price'] = $_product->getPrice();
30
+
31
+ foreach($_associated_products as $simple_product) {
32
+ $product_info = array();
33
+ $associated_ids[] = $simple_product->getId();
34
+ $product_info['id'] = $simple_product->getId();
35
+ $product_info['sku'] = $simple_product->getSku();
36
+ $product_info['price'] = $simple_product->getPrice();
37
+ $mwdata['associated_products'][] = $product_info;
38
+ }
39
+
40
+ ?>
41
+ <script type="text/choiceai_data">
42
+ //<![CDATA[
43
+
44
+ <?php
45
+ try {
46
+ echo json_encode($mwdata);
47
+ } catch(Exception $exception) {
48
+
49
+ }
50
+ ?>
51
+
52
+ //]]>
53
+ </script>
54
+
55
+ <script type="text/javascript">
56
+ //<![CDATA[
57
+ var _mwapi = _mwapi || [];
58
+ _mwapi.push(['trackEvent', 'product', {pid: '<?php echo $_product->getId() ?>', associated_ids: '<?php echo json_encode($associated_ids) ?>'}]);
59
+ //]]>
60
+ </script>
61
+ <?php } ?>
app/design/frontend/default/default/template/choiceai/personalisation/event/checkout/cart/index.phtml ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @category ChoiceAI
4
+ * @package ChoiceAI_Personalisation
5
+ * @copyright Copyright (c) MineWhat
6
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
7
+ */
8
+ ?>
9
+ <?php $_product = $this->getProductToShoppingCart() ?>
10
+ <?php if ($_product && $_product["id"]) { ?>
11
+ <script type="text/javascript">
12
+ //<![CDATA[
13
+ var _mwapi = _mwapi || [];
14
+ _mwapi.push(['trackEvent', 'addtocart', {pid: '<?php echo $_product["id"] ?>', sku: '<?php echo $_product["sku"] ?>', parent_pid: '<?php echo $_product["parentId"] ?>', qty: '<?php echo $_product["qty"] ?>', bundle: '<?php echo json_encode($_product["bundle"]) ?>'}]);
15
+ //]]>
16
+ </script>
17
+ <?php } ?>
app/design/frontend/default/default/template/choiceai/personalisation/event/checkout/onepage/success.phtml ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * @category ChoiceAI
4
+ * @package ChoiceAI_Personalisation
5
+ * @copyright Copyright (c) MineWhat
6
+ * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
7
+ */
8
+ ?>
9
+ <?php if (($orderInfo = $this->getOrderInfo())) { ?>
10
+ <script type="text/javascript">
11
+ //<![CDATA[
12
+ var _mwapi = _mwapi || [];
13
+ var products = [];
14
+ <?php foreach ($orderInfo['items'] as $product) { ?>
15
+ products.push({pid: '<?php echo $product["id"] ?>', qty: '<?php echo intval($product["qty"]) ?>', sku: '<?php echo $product["sku"] ?>', price: '<?php echo $product["price"] ?>', parent_pid: '<?php echo $product["parentId"] ?>', bundle: '<?php echo json_encode($product["bundle"]) ?>'});
16
+ <?php } ?>
17
+ _mwapi.push(['trackEvent', 'buy', {products: products, order: {order_number: '<?php echo $orderInfo["orderId"] ?>', payment: '<?php echo $orderInfo["paymentMethod"] ?>', created_at: '<?php echo $orderInfo["createdAt"] ?>', email: '<?php echo $orderInfo["email"] ?>'}, platform: 'magento', currency: '<?php echo $orderInfo["currency"] ?>'}]);
18
+ //]]>
19
+ </script>
20
+ <?php } ?>
app/etc/modules/ChoiceAI_Personalisation.xml ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+
3
+ <config>
4
+ <modules>
5
+ <ChoiceAI_Personalisation>
6
+ <active>true</active>
7
+ <codePool>community</codePool>
8
+ </ChoiceAI_Personalisation>
9
+ </modules>
10
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>choiceai</name>
4
+ <version>0.0.9</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://opensource.org/licenses/osl-3.0.php">osl</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Choice AI</summary>
10
+ <description>Choice AI</description>
11
+ <notes>Initial Code</notes>
12
+ <authors><author><name>MineWhat Inc.</name><user>MineWhat</user><email>plugins@minewhat.com</email></author></authors>
13
+ <date>2016-01-16</date>
14
+ <time>10:15:21</time>
15
+ <contents><target name="magecommunity"><dir name="ChoiceAI"><dir name="Personalisation"><dir name="Block"><dir name="Base"><file name="Script.php" hash="24c1cdae505fcf94f9fe467067c0c571"/></dir><dir name="Event"><dir name="Catalog"><dir name="Product"><file name="View.php" hash="7e82742fc897a5b17bd56c10c260307f"/></dir></dir><dir name="Checkout"><dir name="Cart"><file name="Index.php" hash="81b7a6ad147113b922a0760b7ef343af"/></dir><dir name="Onepage"><file name="Success.php" hash="dd3ef1b6f8d75b776ffae7ef02544383"/></dir></dir></dir></dir><dir name="Helper"><file name="Data.php" hash="cf4b31be87dab7526412b275b963b5f5"/></dir><dir name="Model"><file name="Observer.php" hash="c925d38a26eaadcb0adf98ce543f558d"/></dir><dir name="controllers"><file name="ApiController.php" hash="e59bd21617a8d26efb0c2c6ad0003f66"/></dir><dir name="etc"><file name="adminhtml.xml" hash="4dca155e478a77913408db92813bdc88"/><file name="config.xml" hash="aec954df9af5cc3f20efd1f0627f9bc4"/><file name="system.xml" hash="a77d909799be50a3f1b1b8eda3071bcf"/></dir></dir></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="layout"><file name="choiceai_personalisation.xml" hash="209eb2196a79df4955c6e95c3a69409c"/></dir><dir name="template"><dir name="choiceai"><dir name="personalisation"><dir name="base"><file name="script.phtml" hash="a2b23688405e17b1fc49a6943735b9ac"/></dir><dir name="event"><dir name="catalog"><dir name="product"><file name="view.phtml" hash="d94d3cedac65003959bd690be3b4c45c"/></dir></dir><dir name="checkout"><dir name="cart"><file name="index.phtml" hash="e13b859530d2773812cc4d77c0abffe8"/></dir><dir name="onepage"><file name="success.phtml" hash="cc1091ce97cb41b4285f949683e7a94e"/></dir></dir></dir></dir></dir></dir></dir></dir><dir name="default"><dir name="default"><dir name="layout"><file name="choiceai_personalisation.xml" hash="841523cb4b558eb20955269a62ce1758"/></dir><dir name="template"><dir name="choiceai"><dir name="personalisation"><dir name="base"><file name="script.phtml" hash="a2b23688405e17b1fc49a6943735b9ac"/></dir><dir name="event"><dir name="catalog"><dir name="product"><file name="view.phtml" hash="d94d3cedac65003959bd690be3b4c45c"/></dir></dir><dir name="checkout"><dir name="cart"><file name="index.phtml" hash="e13b859530d2773812cc4d77c0abffe8"/></dir><dir name="onepage"><file name="success.phtml" hash="cffd776a8961160425a35ad95efa96c7"/></dir></dir></dir></dir></dir></dir></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="ChoiceAI_Personalisation.xml" hash="8434d7f1c26915daeb4699e9a4e66155"/></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.2.5</min><max>6.0.0</max></php></required></dependencies>
18
+ </package>