Strategery_GmailInboxActions - Version 0.2.0

Version Notes

First release

Download this release

Release Info

Developer Gabriel Somoza
Extension Strategery_GmailInboxActions
Version 0.2.0
Comparing to
See all releases


Version 0.2.0

app/code/community/Strategery/GmailInboxActions/Helper/Data.php ADDED
@@ -0,0 +1,10 @@
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * ${FILE_NAME}
4
+ * @author Gabriel Somoza <gabriel@usestrategery.com>
5
+ * @date 10/16/2014 6:08 PM
6
+ * @copyright Copyright (c) 2014
7
+ */
8
+ class Strategery_GmailInboxActions_Helper_Data extends Mage_Core_Helper_Abstract {
9
+
10
+ }
app/code/community/Strategery/GmailInboxActions/Model/Email/Template.php ADDED
@@ -0,0 +1,148 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?php
2
+ /**
3
+ * Template.php
4
+ * @author Gabriel Somoza <gabriel@usestrategery.com>
5
+ * @date 10/16/2014 6:38 PM
6
+ * @copyright Copyright (c) 2014
7
+ */
8
+
9
+ class Strategery_GmailInboxActions_Model_Email_Template extends Mage_Core_Model_Email_Template {
10
+
11
+ /**
12
+ * Post-processes the template to add the Schema.org markup before body end.
13
+ *
14
+ * @param array $variables
15
+ *
16
+ * @return string
17
+ * @throws Exception
18
+ */
19
+ public function getProcessedTemplate(array $variables = array())
20
+ {
21
+ $template = parent::getProcessedTemplate($variables);
22
+ return $this->_addSchemaMarkup($template, $variables);
23
+ }
24
+
25
+ /**
26
+ * Adds the Schema.org markup to emails before body end.
27
+ *
28
+ * @param $template
29
+ * @param array $variables
30
+ *
31
+ * @return mixed
32
+ */
33
+ protected function _addSchemaMarkup($template, array $variables = array())
34
+ {
35
+ $metadata = $this->_generateMetadata($variables) . '</body>';
36
+ return $metadata ? str_ireplace('</body>', $metadata, $template) : $template;
37
+ }
38
+
39
+ /**
40
+ * Generates the Schema.org metadata based on the available variables
41
+ *
42
+ * @param $variables
43
+ *
44
+ * @return string
45
+ */
46
+ protected function _generateMetadata($variables)
47
+ {
48
+ $result = false;
49
+ $json = null;
50
+ if(isset($variables['order'])) {
51
+ /** @var Mage_Sales_Model_Order $order */
52
+ $order = $variables['order'];
53
+ $json = array(
54
+ '@context' => 'http://schema.org',
55
+ '@type' => 'Order',
56
+ 'merchant' => array(
57
+ '@type' => 'Organization', //TODO: Allow customizing this
58
+ 'name' => Mage::app()->getStore($order->getStore())->getFrontendName(),
59
+ ),
60
+ 'acceptedOffer' => $this->_getAcceptedOffers($order),
61
+ 'orderNumber' => $order->getIncrementId(),
62
+ 'priceCurrency' => $order->getOrderCurrency()->toString(),
63
+ 'price' => number_format($order->getGrandTotal(),2), //TODO: make decimal points a config option?
64
+ 'url' => Mage::app()->getStore($order->getStore())->getUrl('sales/order/view', array('order_id' => $order->getId(), '_secure' => true)),
65
+ 'orderStatus' => $this->_getOrderStatus($order),
66
+ //'paymentMethod' => $this->_getPaymentMethod($order),
67
+ 'orderDate' => $order->getCreatedAtStoreDate()->toString(),
68
+ //'priceSpecification' //TODO: add PriceSpecification (recommended for Google Now)
69
+ );
70
+ }
71
+ if($json) {
72
+ $result = '<script type="application/ld+json">'. PHP_EOL .
73
+ json_encode($json, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . PHP_EOL .
74
+ '</script>';
75
+ }
76
+ return $result;
77
+ }
78
+
79
+ /**
80
+ * @param Mage_Sales_Model_Order $order
81
+ *
82
+ * @return array
83
+ */
84
+ protected function _getAcceptedOffers(Mage_Sales_Model_Order $order)
85
+ {
86
+ $offers = array();
87
+ $currencyCode = $order->getOrderCurrency()->getCurrencyCode();
88
+ foreach($order->getAllVisibleItems() as $item) {
89
+ /** @var Mage_Sales_Model_Order_Item $item */
90
+ $productImage = Mage::helper('catalog/image')->init($item->getProduct(), 'image')->resize(265)->__toString();
91
+ $offers[] = array(
92
+ '@type' => 'Offer',
93
+ 'itemOffered' => array(
94
+ '@type' => 'Product', //TODO: Allow customizing this
95
+ 'name' => $item->getName(),
96
+ 'sku' => $item->getSku(),
97
+ 'url' => $item->getProduct()->getProductUrl(),
98
+ 'image' => $productImage,
99
+ ),
100
+ 'price' => number_format($item->getPrice(), 2),
101
+ 'priceCurrency' => $currencyCode,
102
+ 'eligibleQuantity' => array(
103
+ '@type' => 'QuantitativeValue',
104
+ 'value' => $item->getQtyOrdered()
105
+ ),
106
+ 'seller' => array(
107
+ '@type' => 'Organization', //TODO: Allow customizing this
108
+ 'name' => Mage::app()->getStore($order->getStore())->getFrontendName()
109
+ ),
110
+ );
111
+ }
112
+ return $offers;
113
+ }
114
+
115
+ /**
116
+ * Maps the Magento order State to the proposed Schema.org order statuses
117
+ * TODO: migth be a good idea to make this mapping configurable
118
+ *
119
+ * @param Mage_Sales_Model_Order $order
120
+ *
121
+ * @return string
122
+ */
123
+ protected function _getOrderStatus(Mage_Sales_Model_Order $order)
124
+ {
125
+ $state = $order->getState();
126
+ $mapping = array(
127
+ 'new' => 'Processing',
128
+ 'pending_payment' => 'ProblemWithOrder',
129
+ 'processing' => 'Processing',
130
+ 'complete' => 'Delivered',
131
+ 'closed' => 'Cancelled',
132
+ 'cancelled' => 'Cancelled',
133
+ 'holded' => 'ProblemWithOrder',
134
+ );
135
+ return 'http://schema.org/OrderStatus/' . $mapping[$state];
136
+ }
137
+
138
+ /**
139
+ * FIXME: Support this, will probably have to expose some config to users
140
+ * @return string
141
+ */
142
+ protected function _getPaymentMethod(Mage_Sales_Model_Order $order)
143
+ {
144
+ //$order->getPayment()->getMethod();
145
+ return 'http://schema.org/CreditCard';
146
+ }
147
+
148
+ }
app/code/community/Strategery/GmailInboxActions/etc/config.xml ADDED
@@ -0,0 +1,34 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Strategery_GmailInboxActions>
5
+ <version>0.2.0</version>
6
+ </Strategery_GmailInboxActions>
7
+ </modules>
8
+ <global>
9
+ <models>
10
+ <strategery_gmailinboxactions>
11
+ <class>Strategery_GmailInboxActions_Model</class>
12
+ <resourceModel>strategery_gmailinboxactions_resource</resourceModel>
13
+ </strategery_gmailinboxactions>
14
+ <strategery_gmailinboxactions_resource>
15
+ <class>Strategery_GmailInboxActions_Model_Resource</class>
16
+ </strategery_gmailinboxactions_resource>
17
+ <core>
18
+ <rewrite>
19
+ <email_template>Strategery_GmailInboxActions_Model_Email_Template</email_template>
20
+ </rewrite>
21
+ </core>
22
+ </models>
23
+ <blocks>
24
+ <strategery_gmailinboxactions>
25
+ <class>Strategery_GmailInboxActions_Block</class>
26
+ </strategery_gmailinboxactions>
27
+ </blocks>
28
+ <helpers>
29
+ <strategery_gmailinboxactions>
30
+ <class>Strategery_GmailInboxActions_Helper</class>
31
+ </strategery_gmailinboxactions>
32
+ </helpers>
33
+ </global>
34
+ </config>
app/etc/modules/Strategery_GmailInboxActions.xml ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Strategery_GmailInboxActions>
5
+ <active>true</active>
6
+ <codePool>community</codePool>
7
+ <depends>
8
+ <Mage_Newsletter/>
9
+ <Mage_Sendfriend/>
10
+ <Mage_Core/>
11
+ </depends>
12
+ </Strategery_GmailInboxActions>
13
+ </modules>
14
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0"?>
2
+ <package>
3
+ <name>Strategery_GmailInboxActions</name>
4
+ <version>0.2.0</version>
5
+ <stability>stable</stability>
6
+ <license uri="http://www.gnu.org/copyleft/gpl.html">GPL</license>
7
+ <channel>community</channel>
8
+ <extends/>
9
+ <summary>Adds Schema.org markup to outgoing emails for Gmails "Actions in the Inbox" features.</summary>
10
+ <description>Adds Schema.org markup to outgoing emails. This allows Gmail, for example, to show a special "View Order" button on order update emails. More Gmail features are planned for the future. For more information, visit https://developers.google.com/gmail/actions/overview</description>
11
+ <notes>First release</notes>
12
+ <authors><author><name>Gabriel Somoza</name><user>gabrielsomoza</user><email>work@gabrielsomoza.com</email></author></authors>
13
+ <date>2014-10-16</date>
14
+ <time>19:06:16</time>
15
+ <contents><target name="magecommunity"><dir name="Strategery"><dir name="GmailInboxActions"><dir name="Helper"><file name="Data.php" hash="ea26444d38e2841e571e97b537978052"/></dir><dir name="Model"><dir name="Email"><file name="Template.php" hash="ff8abba9364c7c32905b9f93e62c9bae"/></dir></dir><dir name="etc"><file name="config.xml" hash="df09b6f775a5a3ff8dbb9940b9603e7c"/></dir></dir></dir></target><target name="mageetc"><dir name="modules"><file name="Strategery_GmailInboxActions.xml" hash="2ac16b76cd078456d1280196700f35a0"/></dir></target></contents>
16
+ <compatible/>
17
+ <dependencies><required><php><min>5.3.0</min><max>5.6.1</max></php></required></dependencies>
18
+ </package>