Massale_Facbook_Connector - Version 0.1.0

Version Notes

Massale_FacbookConnector

Download this release

Release Info

Developer Magento Core Team
Extension Massale_Facbook_Connector
Version 0.1.0
Comparing to
See all releases


Version 0.1.0

app/code/local/Magestore/Mageconnector/Exception.php ADDED
@@ -0,0 +1,11 @@
1
+ <?php
2
+ /**
3
+ * Class manipulate with Exception in Magestore_Mageconnector module
4
+ *
5
+ * @category Magestore
6
+ * @package Magestore_Mageconnector
7
+ * @author Nguyen Minh Dung (http://oss.com.vn)
8
+ */
9
+ class Magestore_Mageconnector_Exception extends Mage_Core_Exception
10
+ {
11
+ }
app/code/local/Magestore/Mageconnector/Helper/Data.php ADDED
@@ -0,0 +1,71 @@
1
+ <?php
2
+
3
+ class Magestore_Mageconnector_Helper_Data extends Mage_Core_Helper_Abstract
4
+ {
5
+ public function getListStoreView() {
6
+ $collection = Mage::getResourceModel('core/website_collection');
7
+ $default_view = Mage::getStoreConfig('mageconnector/store/default_store_view');
8
+
9
+ $xml = '<?xml version="1.0" encoding="utf-8"?><views>';
10
+
11
+ $i = 0;
12
+ foreach($collection as $website) {
13
+
14
+ $goupcollection = $website->getGroupCollection();
15
+ foreach($goupcollection as $group) {
16
+
17
+ $storecollection = $group->getStoreCollection();
18
+ foreach ($storecollection as $store) {
19
+ $data = $store->toArray();
20
+
21
+ $isDefault = false;
22
+ if (($default_view != '' && $default_view == $data['code']) ||
23
+ ($default_view == '' && $website->__get('is_default') &&
24
+ $website->__get('default_group_id') == $group->getId() &&
25
+ $group->__get('default_store_id') == $store->getId())
26
+ ) {
27
+ $isDefault = true;
28
+ }
29
+
30
+ $totalProducts = count(Mage::getModel('catalog/product')->getCollection()
31
+ ->addAttributeToSelect('name')
32
+ ->addStoreFilter($store->getId())
33
+ ->addAttributeToFilter(array(
34
+ array(
35
+ 'attribute' => 'name',
36
+ 'like' => '%%'
37
+ ),
38
+ array(
39
+ 'attribute' => 'url_key',
40
+ 'like' => '%%'
41
+ ),
42
+ array(
43
+ 'attribute' => 'short_description',
44
+ 'like' => '%%'
45
+ )
46
+ ))
47
+ ->addAttributeToFilter(array(
48
+ array(
49
+ 'attribute' => 'visibility',
50
+ 'in' => array(3,4)
51
+ )
52
+ ))
53
+ ->addFieldToFilter("status", 1)
54
+ );
55
+
56
+ $xml .= '<view>'
57
+ .'<code><![CDATA['.$data['code'].']]></code>'
58
+ .'<name><![CDATA['.$data['name'].']]></name>'
59
+ .'<is_active>'.$data['is_active'].'</is_active>'
60
+ .'<is_default>'.$isDefault.'</is_default>'
61
+ .'<total_products>'.$totalProducts.'</total_products>'
62
+ .'<order>'.(++$i).'</order>'
63
+ .'</view>';
64
+ }
65
+ }
66
+ }
67
+
68
+ $xml .= '</views>';
69
+ return $xml;
70
+ }
71
+ }
app/code/local/Magestore/Mageconnector/Model/Abstract.php ADDED
@@ -0,0 +1,42 @@
1
+ <?php
2
+
3
+ /**
4
+ * Model abstract class for module
5
+ *
6
+ * @category Magestore
7
+ * @package Magestore_Mageconnector
8
+ * @author Nguyen Minh Dung (http://oss.com.vn)
9
+ */
10
+ abstract class Magestore_Mageconnector_Model_Abstract {
11
+
12
+ /**
13
+ * Export Product to XML Data by store code
14
+ *
15
+ * @param string $store_code
16
+ * @return array Store array data
17
+ */
18
+ public function getStore($store_code = null) {
19
+ if (is_null($store_code) || $store_code == '') {
20
+ $store_code = Mage::getStoreConfig('mageconnector/store/default_store_view');
21
+
22
+ if ($store_code == '') {
23
+ $options = Mage::getModel('mageconnector/importstore')->toOptionArray();
24
+
25
+ foreach ($options as $item) {
26
+ $store_code = $item['value'];
27
+ break;
28
+ }
29
+ }
30
+ }
31
+
32
+ $resource = Mage::getSingleton('core/resource');
33
+ $read = $resource->getConnection("core_read");
34
+ $getStore = Mage::getResourceModel('core/store_collection')
35
+ ->getSelect()
36
+ ->where('code = ?', $store_code);
37
+
38
+ $store = $read->fetchRow($getStore);
39
+ return $store;
40
+ }
41
+
42
+ }
app/code/local/Magestore/Mageconnector/Model/Cart.php ADDED
@@ -0,0 +1,268 @@
1
+ <?php
2
+
3
+ /**
4
+ * Export Category data to xml data
5
+ *
6
+ * @category Magestore
7
+ * @package Magestore_Mageconnector
8
+ * @author Nguyen Minh Dung (http://oss.com.vn)
9
+ */
10
+ class Magestore_Mageconnector_Model_Cart extends Magestore_Mageconnector_Model_Abstract {
11
+
12
+ /**
13
+ * Initialize product instance from request data
14
+ *
15
+ * @return Mage_Catalog_Model_Product || false
16
+ */
17
+ protected function _initProduct() {
18
+ $productId = (int) $_REQUEST['product'];
19
+ if ($productId) {
20
+ try {
21
+ $view_code = $_REQUEST['view'];
22
+ } catch (Exception $e) {
23
+ $view_code = '';
24
+ }
25
+ $store = $this->getStore($view_code);
26
+ $storeId = $store['store_id'];
27
+
28
+ $product = Mage::getModel('catalog/product')
29
+ ->setStoreId($storeId)
30
+ ->load($productId);
31
+ if ($product->getId()) {
32
+ return $product;
33
+ }
34
+ }
35
+ return false;
36
+ }
37
+
38
+ public function addToCart() {
39
+ $cart = Mage::getSingleton('checkout/cart');
40
+ $params = $_REQUEST;
41
+ try {
42
+ if (isset($params['qty'])) {
43
+ $filter = new Zend_Filter_LocalizedToNormalized(
44
+ array('locale' => Mage::app()->getLocale()->getLocaleCode())
45
+ );
46
+ $params['qty'] = $filter->filter($params['qty']);
47
+ }
48
+
49
+ $product = $this->_initProduct();
50
+ try {
51
+ $related = $params['related_product'];
52
+ } catch (Exception $e) {
53
+ $related = null;
54
+ }
55
+
56
+ /**
57
+ * Check product availability
58
+ */
59
+ if (!$product) {
60
+ return false;
61
+ }
62
+
63
+ $cart->addProduct($product, $params);
64
+ if (!empty($related) && !is_null($related)) {
65
+ $cart->addProductsByIds(explode(',', $related));
66
+ }
67
+
68
+ $cart->save();
69
+
70
+ Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
71
+
72
+ return true;
73
+ } catch (Exception $e) {
74
+ Zend_Debug::dump($e);
75
+ die();
76
+ }
77
+ return false;
78
+ }
79
+
80
+ public function updateCart() {
81
+ try {
82
+ $cartData = $_REQUEST['cart'];
83
+ if (is_array($cartData)) {
84
+ $filter = new Zend_Filter_LocalizedToNormalized(
85
+ array('locale' => Mage::app()->getLocale()->getLocaleCode())
86
+ );
87
+ foreach ($cartData as $index => $data) {
88
+ if (isset($data['qty'])) {
89
+ $cartData[$index]['qty'] = $filter->filter($data['qty']);
90
+ }
91
+ }
92
+ $cart = Mage::getSingleton('checkout/cart');
93
+ if (!$cart->getCustomerSession()->getCustomer()->getId() && $cart->getQuote()->getCustomerId()) {
94
+ $cart->getQuote()->setCustomerId(null);
95
+ }
96
+ $cart->updateItems($cartData)
97
+ ->save();
98
+ }
99
+ Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
100
+ return true;
101
+ } catch (Exception $e) {
102
+
103
+ }
104
+ return false;
105
+ }
106
+
107
+ public function deleteCartItem() {
108
+ $id = $_REQUEST['item'];
109
+ if ($id) {
110
+ try {
111
+ $cart = Mage::getSingleton('checkout/cart')->removeItem($id)
112
+ ->save();
113
+ return true;
114
+ } catch (Exception $e) {
115
+
116
+ }
117
+ }
118
+ return false;
119
+ }
120
+
121
+ public function exportXml() {
122
+ $cartXML = '<?xml version="1.0" encoding="UTF-8"?><massale-cart>';
123
+
124
+ $quote = Mage::getSingleton('checkout/session')->getQuote();
125
+ $items = $quote->getAllVisibleItems();
126
+
127
+ if (count($items) > 0) {
128
+ $cartXML .= '<products>';
129
+ $i = 0;
130
+ foreach ($items as $item) {
131
+ $product = $item->getProduct();
132
+
133
+ switch ($product->getTypeId()) {
134
+ case "configurable":
135
+ $blockId = "checkout/cart_item_renderer_configurable";
136
+ break;
137
+ case "bundle":
138
+ $blockId = "bundle/checkout_cart_item_renderer";
139
+ break;
140
+ case "downloadable":
141
+ $blockId = "downloadable/checkout_cart_item_renderer";
142
+ break;
143
+ default:
144
+ $blockId = "checkout/cart_item_renderer";
145
+ }
146
+
147
+ $block = Mage::getBlockSingleton($blockId)
148
+ ->setItem($item);
149
+
150
+ $cartXML .= '<product>'
151
+ . '<order>#' . (++$i) . '</order>'
152
+ . '<id>' . $block->getItem()->getId() . '</id>'
153
+ . '<name><![CDATA[' . $block->htmlEscape($block->getProductName()) . ']]></name>'
154
+ . '<image><![CDATA[' . $block->getProductThumbnail() . ']]></image>'
155
+ . '<url_key><![CDATA[' . $product->getUrlKey() . ']]></url_key>';
156
+
157
+ if ($_options = $block->getOptionList()):
158
+ $cartXML .= '<options>';
159
+ foreach ($_options as $_option):
160
+ $_formatedOptionValue = $block->getFormatedOptionValue($_option);
161
+ $cartXML .= '<option>'
162
+ . '<label><![CDATA[' . $block->htmlEscape($_option['label']) . ']]></label>'
163
+ . '<value><![CDATA[' . $_formatedOptionValue['value'] . ']]></value>'
164
+ . '</option>'
165
+ ;
166
+ endforeach;
167
+ $cartXML .= '</options>';
168
+ endif;
169
+
170
+ $cartXML .= '<qty>' . $item->getQty() . '</qty>';
171
+
172
+ $price = Mage::helper('core')
173
+ ->currency($item->getPrice(), true, false);
174
+ $rowsubtotal = Mage::helper('core')
175
+ ->currency($item->getRowTotal(), true, false);
176
+
177
+ $cartXML .= '<price><![CDATA[' . $price . ']]></price>';
178
+ $cartXML .= '<subtotal><![CDATA[' . $rowsubtotal . ']]></subtotal>';
179
+ $cartXML .= '</product>';
180
+ }
181
+ $cartXML .= '</products>';
182
+
183
+ if ($quote->getCouponCode() != '') {
184
+ $cartXML .= '<coupon>'
185
+ . '<code><![CDATA[' . $quote->getCouponCode() . ']]></code>'
186
+ . '<label><![CDATA[' . $quote->getCouponCode() . ']]></label>'
187
+ . '</coupon>';
188
+ }
189
+
190
+ $subtotal = $quote->getSubtotal();
191
+ $subtotal_withdiscount = $quote->getSubtotalWithDiscount();
192
+
193
+ $discount = floatval($subtotal_withdiscount) - floatval($subtotal);
194
+ $subtotal = Mage::helper('core')
195
+ ->currency($subtotal, true, false);
196
+ $cartXML .= '<subtotal><![CDATA[' . $subtotal . ']]></subtotal>';
197
+
198
+ if ($discount != 0) {
199
+ $discount = Mage::helper('core')
200
+ ->currency($discount, true, false);
201
+ $cartXML .= '<discount><![CDATA[' . $discount . ']]></discount>';
202
+ }
203
+
204
+ $grandtotal = $quote->getGrandTotal();
205
+ $tax = $grandtotal - $subtotal_withdiscount;
206
+
207
+ if ($tax != 0) {
208
+ $tax = Mage::helper('core')
209
+ ->currency($tax, true, false);
210
+ $cartXML .= '<tax><![CDATA[' . $tax . ']]></tax>';
211
+ }
212
+ $grandtotal = Mage::helper('core')
213
+ ->currency($grandtotal, true, false);
214
+
215
+ $cartXML .= '<grandtotal><![CDATA[' . $grandtotal . ']]></grandtotal>';
216
+ } else {
217
+ $cartXML .= 'empty';
218
+ }
219
+ $cartXML .= '</massale-cart>';
220
+ return $cartXML;
221
+ }
222
+
223
+ public function applyCoupon() {
224
+ if (!Mage::getSingleton('checkout/cart')->getQuote()->getItemsCount()) {
225
+ return false;
226
+ }
227
+
228
+ $couponCode = $_REQUEST['coupon_code'];
229
+ $oldCouponCode = Mage::getSingleton('checkout/cart')->getQuote()->getCouponCode();
230
+
231
+ if (!strlen($couponCode) && !strlen($oldCouponCode)) {
232
+ return false;
233
+ }
234
+
235
+ try {
236
+ Mage::getSingleton('checkout/cart')->getQuote()->getShippingAddress()->setCollectShippingRates(true);
237
+ Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode(strlen($couponCode) ? $couponCode : '')
238
+ ->collectTotals()
239
+ ->save();
240
+
241
+ if ($couponCode) {
242
+ if ($couponCode == Mage::getSingleton('checkout/cart')->getQuote()->getCouponCode()) {
243
+ return true;
244
+ }
245
+ }
246
+ } catch (Exception $e) {
247
+
248
+ }
249
+
250
+ return false;
251
+ }
252
+
253
+ public function cancelCoupon() {
254
+ try {
255
+ Mage::getSingleton('checkout/cart')->getQuote()->getShippingAddress()->setCollectShippingRates(true);
256
+ Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode('')
257
+ ->collectTotals()
258
+ ->save();
259
+
260
+ return true;
261
+ } catch (Exception $e) {
262
+
263
+ }
264
+
265
+ return false;
266
+ }
267
+
268
+ }
app/code/local/Magestore/Mageconnector/Model/Category.php ADDED
@@ -0,0 +1,74 @@
1
+ <?php
2
+ /**
3
+ * Export Category data to xml data
4
+ *
5
+ * @category Magestore
6
+ * @package Magestore_Mageconnector
7
+ * @author Nguyen Minh Dung (http://oss.com.vn)
8
+ */
9
+ class Magestore_Mageconnector_Model_Category extends Magestore_Mageconnector_Model_Abstract
10
+ {
11
+ /**
12
+ * Export Category to XML Data by store code
13
+ *
14
+ * @param string $store_code
15
+ */
16
+ public function exportXML($store_code = null) {
17
+ $store = $this->getStore($store_code);
18
+ $storeId = $store['store_id'];
19
+
20
+ $group = Mage::getModel('core/store_group')->load($store['group_id']);
21
+ $rootCategoryId = $group->getRootCategoryId();
22
+
23
+ $xml = '<?xml version="1.0"?><categories>';
24
+
25
+ $category = Mage::getModel('catalog/category')
26
+ ->setStoreId($storeId)
27
+ ->load($rootCategoryId);
28
+
29
+ if ($category instanceof Mage_Catalog_Model_Category) {
30
+ $data = $category->toArray();
31
+ if (array_key_exists('entity_id', $data)) {
32
+ $this->pushChildrenCategory($category
33
+ ->getChildrenCategories(), $xml, $storeId);
34
+ }
35
+ }
36
+
37
+ $xml .= '</categories>';
38
+ return $xml;
39
+ }
40
+
41
+ private function pushChildrenCategory($categoryCollection, &$xml, $storeId) {
42
+ foreach($categoryCollection as $category) {
43
+ if ($category instanceof Mage_Catalog_Model_Category) {
44
+ if ($category->getIsActive()) {
45
+ $productList = $category->getProductCollection();
46
+ $total_products = count($category
47
+ ->setStoreId($storeId)
48
+ ->getProductCollection()
49
+ ->addAttributeToFilter(array(
50
+ array(
51
+ 'attribute' => 'visibility',
52
+ 'in' => array(2,4)
53
+ )
54
+ ))
55
+ );
56
+ $url_key = $category->getRequestPath();
57
+ $url_key = str_replace('.html', '', $url_key);
58
+ $parts = split('/', $url_key);
59
+ $url_key = $parts[count($parts) - 1];
60
+
61
+ $xml .= '<category>'
62
+ .'<id>'.$category->getId().'</id>'
63
+ .'<name><![CDATA['.$category->getName().']]></name>'
64
+ .'<url_key><![CDATA['.$url_key.']]></url_key>'
65
+ .'<url><![CDATA['.$category->getUrl().']]></url>'
66
+ .'<total_products>'.$total_products.'</total_products>'
67
+ .'<children>';
68
+ $this->pushChildrenCategory($category->getChildrenCategories(), $xml, $storeId);
69
+ $xml .= '</children></category>';
70
+ }
71
+ }
72
+ }
73
+ }
74
+ }
app/code/local/Magestore/Mageconnector/Model/Importstore.php ADDED
@@ -0,0 +1,62 @@
1
+ <?php
2
+ /**
3
+ * Source for import Store to get store view default
4
+ *
5
+ * @category Magestore
6
+ * @package Magestore_Mageconnector
7
+ * @author Nguyen Minh Dung (http://magestore.com)
8
+ */
9
+ class Magestore_Mageconnector_Model_Importstore
10
+ {
11
+ public function toOptionArray()
12
+ {
13
+ $collection = Mage::getResourceModel('core/website_collection');
14
+
15
+ $options = array();
16
+
17
+ foreach($collection as $website) {
18
+
19
+ $goupcollection = $website->getGroupCollection();
20
+ foreach($goupcollection as $group) {
21
+
22
+ $storecollection = $group->getStoreCollection();
23
+ foreach ($storecollection as $store) {
24
+
25
+ if ($website->__get('is_default') &&
26
+ $website->__get('default_group_id') == $group->getId() &&
27
+ $group->__get('default_store_id') == $store->getId()) {
28
+ $options[] = array(
29
+ 'value' => $store->code,
30
+ 'label' => $website->getName().' / '.$group->getName().' / '.$store->getName()
31
+ );
32
+ break;
33
+ }
34
+ }
35
+ }
36
+ }
37
+
38
+ foreach($collection as $website) {
39
+
40
+ $goupcollection = $website->getGroupCollection();
41
+ foreach($goupcollection as $group) {
42
+
43
+ $storecollection = $group->getStoreCollection();
44
+ foreach ($storecollection as $store) {
45
+
46
+ if ($website->__get('is_default') &&
47
+ $website->__get('default_group_id') == $group->getId() &&
48
+ $group->__get('default_store_id') == $store->getId()) {
49
+ continue;
50
+ } else {
51
+ $options[] = array(
52
+ 'value' => $store->code,
53
+ 'label' => $website->getName().' / '.$group->getName().' / '.$store->getName()
54
+ );
55
+ }
56
+ }
57
+ }
58
+ }
59
+
60
+ return $options;
61
+ }
62
+ }
app/code/local/Magestore/Mageconnector/Model/Product.php ADDED
@@ -0,0 +1,884 @@
1
+ <?php
2
+
3
+ /**
4
+ * Export Product data to xml data
5
+ *
6
+ * @category Magestore
7
+ * @package Magestore_Mageconnector
8
+ * @author Nguyen Minh Dung (http://oss.com.vn)
9
+ */
10
+ class Magestore_Mageconnector_Model_Product extends Magestore_Mageconnector_Model_Abstract {
11
+
12
+ public function exportXMLProduct(Mage_Catalog_Model_Product $product, &$i, $isShowAllDetails = false) {
13
+ try {
14
+ $xml = '';
15
+ $inventory = Mage::getModel('cataloginventory/stock_item')
16
+ ->loadByProduct($product);
17
+
18
+ if ($product->getStatus()) {
19
+ $xml .= '<product>'
20
+ . '<order>#' . ++$i . '</order>'
21
+ . '<id>' . $product->getId() . '</id>'
22
+ . '<name><![CDATA[' . $product->getName() . ']]></name>';
23
+
24
+ if ($product->getTypeId() != 'grouped') {
25
+ $real_price = $product->getPrice();
26
+
27
+ if ($real_price > 0) {
28
+ $price = Mage::helper('core')
29
+ ->currency(Mage::helper('tax')
30
+ ->getPrice(
31
+ $product,
32
+ $product->getPrice()
33
+ )
34
+ , true, false);
35
+
36
+ $now = date('Y-m-d');
37
+ $special_from_date = split(' ', $product->getSpecialFromDate());
38
+ $special_from_date = $special_from_date[0];
39
+ $special_to_date = split(' ', $product->getSpecialToDate());
40
+ $special_to_date = $special_to_date[0];
41
+
42
+ $xml .= '<price><![CDATA[' . $price . ']]></price>';
43
+
44
+ if ($now >= $special_from_date && $now <= $special_to_date &&
45
+ $product->getSpecialPrice() < $product->getPrice()) {
46
+ $special_price = Mage::helper('tax')
47
+ ->getPrice(
48
+ $product,
49
+ $product->getSpecialPrice()
50
+ );
51
+
52
+ if ($product->getTypeId() == 'bundle') {
53
+ $special_price = $special_price / 100 * $real_price;
54
+ }
55
+ $real_price = $special_price;
56
+
57
+ $special_price = Mage::helper('core')
58
+ ->currency($special_price, true, false);
59
+
60
+ $xml .= '<special_price><![CDATA[' . $special_price . ']]></special_price>'
61
+ . '<special_from_date>' . $special_from_date . '</special_from_date>'
62
+ . '<special_to_date>' . $special_to_date . '</special_to_date>';
63
+ } else if ($product->getFinalPrice() < $product->getPrice()) {
64
+ $special_price = Mage::helper('tax')
65
+ ->getPrice(
66
+ $product,
67
+ $product->getFinalPrice()
68
+ );
69
+
70
+ if ($product->getTypeId() == 'bundle') {
71
+ $special_price = $special_price / 100 * $real_price;
72
+ }
73
+ $real_price = $special_price;
74
+
75
+ $special_price = Mage::helper('core')
76
+ ->currency($special_price, true, false);
77
+
78
+ $xml .= '<special_price><![CDATA[' . $special_price . ']]></special_price>';
79
+ }
80
+ }
81
+ if ($isShowAllDetails) {
82
+ $xml .= $this->addCustomOptions($product, $real_price)
83
+ . $this->addPriceTiers($product, $real_price);
84
+ }
85
+ }
86
+
87
+ $xml .= '<url_key><![CDATA[' . $product->getUrlKey() . ']]></url_key>'
88
+ . '<url><![CDATA[' . $product->getProductUrl() . ']]></url>'
89
+ . '<short_description><![CDATA[' . $product->getShortDescription() . ']]></short_description>'
90
+ . '<is_in_stock>' . $product->isInStock() . '</is_in_stock>'
91
+ . '<is_salable>' . $product->isSalable() . '</is_salable>'
92
+ . '<image><![CDATA[' . $product->getImageUrl() . ']]></image>'
93
+ . '<image_label><![CDATA[' . $product->getImageLabel() . ']]></image_label>';
94
+
95
+ $listImage = $product->getMediaGalleryImages();
96
+ $gallery = '';
97
+ foreach ($listImage as $image) {
98
+ $gallery .= '<image>'
99
+ . '<src><![CDATA[' . $image->getUrl() . ']]></src>'
100
+ . '<label><![CDATA[' . $image->getLabel() . ']]></label>'
101
+ . '</image>';
102
+ }
103
+ $xml .= '<image_gallery>' . $gallery . '</image_gallery>';
104
+
105
+ if ($isShowAllDetails) {
106
+ $xml .= '<description><![CDATA[' . $product->getDescription() . ']]></description>';
107
+
108
+ switch ($product->getTypeId()) {
109
+ case 'grouped':
110
+ $xml .= $this->addAssociatedProducts($product);
111
+ break;
112
+ case 'downloadable':
113
+ $xml .= $this->addLinks($product);
114
+ break;
115
+ case 'configurable':
116
+ $xml .= $this->addConfigurationAttributes($product, $real_price);
117
+ break;
118
+ }
119
+ $xml .= $this->addMoreInformation($product);
120
+ }
121
+
122
+ $xml .= '</product>';
123
+ }
124
+ return $xml;
125
+ } catch (Exception $e) {
126
+ Zend_Debug::dump($e);
127
+ die();
128
+ }
129
+ }
130
+
131
+ public function addLinks(Mage_Catalog_Model_Product $product) {
132
+ $xml = '<downloadable_links>';
133
+ try {
134
+ $xml .= '<links_purchased_separately>' . $product->getLinksPurchasedSeparately() . '</links_purchased_separately>'
135
+ . '<purchased_links_title>' . $product->getLinksTitle() . '</purchased_links_title>'
136
+ . '<links_form_key>links[]</links_form_key>'
137
+ . '<purchased_links>';
138
+ $links = $product->getTypeInstance(true)->getLinks($product);
139
+ foreach ($links as $item) {
140
+ if ($item instanceof Mage_Downloadable_Model_Link) {
141
+ $xml .= '<link><title><![CDATA[' . $item->getTitle() . ']]></title>';
142
+ if (floatval($item->getPrice()) != 0) {
143
+ $p = Mage::helper('core')
144
+ ->currency($item->getPrice(), true, false);
145
+ if (substr($p, 0, 1) != '-') {
146
+ $p = '+' . $p;
147
+ }
148
+
149
+ $xml .= '<option_price><![CDATA[' . $p . ']]></option_price>';
150
+ }
151
+ $xml .= '<form_value>' . $item->getId() . '</form_value>'
152
+ . '</link>';
153
+ }
154
+ }
155
+ $xml .= '</purchased_links>'
156
+ . '<sample_links_title>' . $product->getSamplesTitle() . '</sample_links_title>'
157
+ . '<sample_links>';
158
+ $samples = $product->getTypeInstance(true)->getSamples($product);
159
+ foreach ($samples as $item) {
160
+ if ($item instanceof Mage_Downloadable_Model_Sample) {
161
+ $xml .= '<link>'
162
+ . '<title><![CDATA[' . $item->getTitle() . ']]></title>'
163
+ . '<url><![CDATA[' . Mage::getBaseUrl() . 'downloadable/download/sample/sample_id/' . $item->getId() . ']]></url>'
164
+ . '</link>';
165
+ }
166
+ }
167
+ $xml .= '</sample_links>';
168
+ } catch (Exception $e) {
169
+
170
+ }
171
+ $xml .= '</downloadable_links>';
172
+ return $xml;
173
+ }
174
+
175
+ public function addMoreInformation(Mage_Catalog_Model_Product $product) {
176
+ $xml = '<more_attributes>';
177
+ try {
178
+ $data = $product->getData();
179
+
180
+ foreach ($data as $attrCode => $v) {
181
+ $attribute = $product->getResource()->getAttribute($attrCode);
182
+
183
+ if ($attribute instanceof Mage_Catalog_Model_Resource_Eav_Attribute) {
184
+ if ($attribute->getIsVisibleOnFront()) {
185
+ $value = $attribute->getFrontend()->getValue($product);
186
+ $label = $attribute->getFrontendLabel();
187
+
188
+ // TODO this is temporary skipping eco taxes
189
+ if (is_string($value)) {
190
+ if (strlen($value) && $product->hasData($attrCode)) {
191
+ if ($attribute->getFrontendInput() == 'price') {
192
+ $value = Mage::app()->getStore($product->getStoreId())->convertPrice($value, true);
193
+ } else {
194
+ $value = $value;
195
+ }
196
+ $xml .= '<attribute><label><![CDATA[' . $label . ']]></label>'
197
+ . '<value><![CDATA[' . $value . ']]></value></attribute>';
198
+ }
199
+ }
200
+ }
201
+ }
202
+ }
203
+ } catch (Exception $e) {
204
+ Zend_Debug::dump($e);
205
+ }
206
+
207
+ $xml .= '</more_attributes>';
208
+ return $xml;
209
+ }
210
+
211
+ /**
212
+ * Get list associated produts of grouped product
213
+ *
214
+ * @param Mage_Catalog_Model_Product $product
215
+ */
216
+ public function addAssociatedProducts(Mage_Catalog_Model_Product $product) {
217
+ $xml = '<associated_products>';
218
+
219
+ $grouped = $product->getTypeInstance(true);
220
+ $listAssociatedProducts = $grouped->getAssociatedProducts($product);
221
+ $i = 0;
222
+ foreach ($listAssociatedProducts as $associatedProduct) {
223
+ if ($associatedProduct->getStatus()) {
224
+ $xml .= '<associated_product>'
225
+ . '<order>#' . ++$i . '</order>'
226
+ . '<id>' . $associatedProduct->getId() . '</id>'
227
+ . '<name><![CDATA[' . $associatedProduct->getName() . ']]></name>';
228
+
229
+ $real_price = $associatedProduct->getPrice();
230
+
231
+ $price = Mage::helper('core')
232
+ ->currency(Mage::helper('tax')
233
+ ->getPrice(
234
+ $associatedProduct,
235
+ $associatedProduct->getPrice()
236
+ )
237
+ , true, false);
238
+
239
+ $now = date('Y-m-d');
240
+ $special_from_date = split(' ', $associatedProduct->getSpecialFromDate());
241
+ $special_from_date = $special_from_date[0];
242
+ $special_to_date = split(' ', $associatedProduct->getSpecialToDate());
243
+ $special_to_date = $special_to_date[0];
244
+
245
+ $xml .= '<price><![CDATA[' . $price . ']]></price>';
246
+
247
+ if ($now >= $special_from_date && $now <= $special_to_date) {
248
+ $special_price = Mage::helper('tax')
249
+ ->getPrice(
250
+ $associatedProduct,
251
+ $associatedProduct->getSpecialPrice()
252
+ );
253
+ $real_price = $special_price;
254
+
255
+ $special_price = Mage::helper('core')
256
+ ->currency($special_price, true, false);
257
+
258
+ $xml .= '<special_price><![CDATA[' . $special_price . ']]></special_price>'
259
+ . '<special_from_date>' . $special_from_date . '</special_from_date>'
260
+ . '<special_to_date>' . $special_to_date . '</special_to_date>';
261
+ }
262
+
263
+ $xml .= '<url_key><![CDATA[' . $associatedProduct->getUrlKey() . ']]></url_key>'
264
+ . '<url><![CDATA[' . $associatedProduct->getProductUrl() . ']]></url>'
265
+ . '<form_key>super_group[' . $associatedProduct->getId() . ']</form_key>'
266
+ . '</associated_product>';
267
+ }
268
+ }
269
+
270
+ $xml .= '</associated_products>';
271
+ return $xml;
272
+ }
273
+
274
+ public function addPriceTiers(Mage_Catalog_Model_Product $product, $real_price) {
275
+ $xml = '<price_tiers>';
276
+ try {
277
+
278
+ $tiers = $product->getTierPrice();
279
+ foreach ($tiers as $tier) {
280
+ if ($real_price > $tier['website_price']) {
281
+ $xml .= '<tier>'
282
+ . '<quantity>' . rtrim(rtrim($tier['price_qty'], '0'), '.') . '</quantity>';
283
+ if ($product->getTypeId() != 'bundle') {
284
+ $xml .= '<price>' . Mage::helper('core')
285
+ ->currency($tier['price'], true, false) . '</price>';
286
+ } else {
287
+ $xml .= '<discount>' . $tier['percent'] . '</discount>';
288
+ }
289
+ $xml .= '</tier>';
290
+ }
291
+ }
292
+ } catch (Exception $e) {
293
+
294
+ }
295
+
296
+ $xml .= '</price_tiers>';
297
+ return $xml;
298
+ }
299
+
300
+ public function addCustomOptions(Mage_Catalog_Model_Product $product, $real_price) {
301
+ $xml = '<custom_options>';
302
+ try {
303
+ $options = $product->getOptions();
304
+
305
+ foreach ($options as $o) {
306
+ if ($o instanceof Mage_Catalog_Model_Product_Option) {
307
+ $type = $o->getType();
308
+ switch ($type) {
309
+ case 'drop_down':
310
+ $type = 'dropdown';
311
+ break;
312
+ case 'field':
313
+ $type = 'textfield';
314
+ break;
315
+ case 'area':
316
+ $type = 'textarea';
317
+ break;
318
+ case 'multiple':
319
+ $type = 'multiselect';
320
+ break;
321
+ case 'date_time':
322
+ $type = 'datetime';
323
+ break;
324
+ }
325
+
326
+ $xml .= '<option><type>' . $type . '</type>'
327
+ . '<title><![CDATA[' . $o->getTitle() . ']]></title>'
328
+ . '<is_require>' . $o->getIsRequire() . '</is_require>';
329
+
330
+ if ($type == 'multiselect' || $type == 'checkbox') {
331
+ $xml .= '<form_key>options[' . $o->getId() . '][]</form_key>';
332
+ } else {
333
+ $xml .= '<form_key>options[' . $o->getId() . ']</form_key>';
334
+ }
335
+ if ($type == 'textfield' || $type == 'textarea' || $type == 'file'
336
+ || $type == 'date' || $type == 'datetime' || $type == 'time') {
337
+
338
+ $p = $o->getPrice();
339
+ if ($p == 0) {
340
+ $xml .= '<option_price/>';
341
+ } else {
342
+ $t = $o->getPriceType();
343
+ $op = $p;
344
+ if ($t == 'percent') {
345
+ $op = $p * $real_price / 100;
346
+ }
347
+ $p = Mage::helper('core')
348
+ ->currency($op, true, false);
349
+ if (substr($p, 0, 1) != '-') {
350
+ $p = '+' . $p;
351
+ }
352
+ $xml .= '<option_price><![CDATA[' . $p . ']]></option_price>';
353
+ }
354
+ }
355
+ $xml .= '<items>';
356
+
357
+ $values = $o->getValues();
358
+ foreach ($values as $item) {
359
+ $data = $item->getData();
360
+ $xml .= '<item>'
361
+ . '<title><![CDATA[' . $data['title'] . ']]></title>';
362
+
363
+ $p = $data['price'];
364
+ if ($p == 0) {
365
+ $xml .= '<option_price/>';
366
+ } else {
367
+ $t = $data['price_type'];
368
+ $op = $p;
369
+ if ($t == 'percent') {
370
+ $op = $p * $real_price / 100;
371
+ }
372
+ $p = Mage::helper('core')
373
+ ->currency($op, true, false);
374
+ if (substr($p, 0, 1) != '-') {
375
+ $p = '+' . $p;
376
+ }
377
+ $xml .= '<option_price><![CDATA[' . $p . ']]></option_price>';
378
+ }
379
+ $xml .= '<form_value>' . $item->getId() . '</form_value>';
380
+ $xml .= '</item>';
381
+ }
382
+ $xml .= '</items></option>';
383
+ }
384
+ }
385
+ } catch (Exception $e) {
386
+
387
+ }
388
+
389
+ $xml .= '</custom_options>';
390
+ return $xml;
391
+ }
392
+
393
+ /**
394
+ * Get list attributes of Configuration product
395
+ *
396
+ * @param Mage_Catalog_Model_Product $product
397
+ */
398
+ public function addConfigurationAttributes(Mage_Catalog_Model_Product $product, $real_price) {
399
+ try {
400
+ $xml = '<configuration_options>';
401
+ $options = $product->getTypeInstance(true)
402
+ ->getConfigurableAttributesAsArray($product);
403
+
404
+ foreach ($options as $o) {
405
+ $xml .= '<option><type>dropdown</type>'
406
+ . '<title><![CDATA[' . $o['frontend_label'] . ']]></title>'
407
+ . '<is_require>1</is_require>'
408
+ . '<form_key>super_attribute[' . $o['attribute_id'] . ']</form_key>'
409
+ . '<items>';
410
+
411
+ $values = $o['values'];
412
+ foreach ($values as $item) {
413
+ $xml .= '<item>'
414
+ . '<form_value><![CDATA[' . $item['value_index'] . ']]></form_value>'
415
+ . '<title><![CDATA[' . $item['store_label'] . ']]></title>';
416
+
417
+ $p = $item['pricing_value'];
418
+ if (!is_null($p)) {
419
+ $op = $p;
420
+ if ($item['is_percent'] == 1) {
421
+ $op = $p * $real_price / 100;
422
+ }
423
+ $p = Mage::helper('core')
424
+ ->currency($op, true, false);
425
+ if (substr($p, 0, 1) != '-') {
426
+ $p = '+' . $p;
427
+ }
428
+ $xml .= '<option_price><![CDATA[' . $p . ']]></option_price>';
429
+ }
430
+ $xml .= '</item>';
431
+ }
432
+ $xml .= '</items></option>';
433
+ }
434
+
435
+ $xml .= '</configuration_options>';
436
+ return $xml;
437
+ } catch (Exception $e) {
438
+ Zend_Debug::dump($e);
439
+ die();
440
+ }
441
+ return '';
442
+ }
443
+
444
+ /**
445
+ * Export Product to XML Data by store code
446
+ *
447
+ * @param Mage_Catalog_Model_Product_Collection $collection
448
+ * @param int $totalProducts
449
+ * @param int $totalPages
450
+ * @param int $currentPage
451
+ * @param int $pageSize
452
+ * @param string $orderBy
453
+ * @param string $typeOrder
454
+ */
455
+ public function exportXML($collection, $totalProducts = 1, $totalPages = 1, $currentPage = 1, $pageSize = 1, $orderBy = 'entity_id', $typeOrder = 'desc') {
456
+
457
+ $xml = '<?xml version="1.0"?><root><products>';
458
+
459
+ $i = $pageSize * ($currentPage - 1);
460
+ foreach ($collection as $product) {
461
+ $xml .= $this->exportXMLProduct($product, $i);
462
+ }
463
+
464
+ if ($orderBy == 'entity_id') {
465
+ $orderBy = 'new';
466
+ }
467
+
468
+ $xml .= '</products>'
469
+ . '<paginator><totalProducts>' . $totalProducts . '</totalProducts>'
470
+ . '<totalPages>' . $totalPages . '</totalPages>'
471
+ . '<currentPage>' . $currentPage . '</currentPage>'
472
+ . '<pageSize>' . $pageSize . '</pageSize>'
473
+ . '<orderBy>' . $orderBy . '</orderBy>'
474
+ . '<typeOrder>' . $typeOrder . '</typeOrder></paginator>'
475
+ . '</root>';
476
+
477
+ return $xml;
478
+ }
479
+
480
+ /**
481
+ * Loads product list by Category
482
+ *
483
+ * If category has no product and $showProductsInSubCategories = true, then shows
484
+ * all product in sub categories
485
+ *
486
+ * @param string $store_code
487
+ * @param string $category_url_key
488
+ * @param int $_currentPage
489
+ * @param int $_pageSize
490
+ * @param string $_orderBy
491
+ * @param string $_typeOrder
492
+ * @param boolean $showProductsInSubCategories
493
+ */
494
+ public function getProductListByCategory($store_code, $category_url_key, $_currentPage = 1, $_pageSize = 10, $_orderBy = 'new', $_typeOrder = 'desc', $showProductsInSubCategories = false) {
495
+ $store = $this->getStore($store_code);
496
+ $storeId = $store['store_id'];
497
+
498
+ if (is_null($_orderBy) || $_orderBy == '' || $_orderBy == 'new') {
499
+ $_orderBy = 'entity_id';
500
+ }
501
+ $_orderBy = strtolower($_orderBy);
502
+
503
+ if (is_null($_typeOrder) || $_typeOrder == '') {
504
+ $_typeOrder = 'desc';
505
+ }
506
+ $_typeOrder = strtolower($_typeOrder);
507
+
508
+ if (is_null($_currentPage) || intval($_currentPage) <= 0) {
509
+ $_currentPage = 1;
510
+ }
511
+
512
+ if (is_null($_pageSize) || $_pageSize == '') {
513
+ $_pageSize = 10;
514
+ }
515
+
516
+ $category = Mage::getModel('catalog/category')
517
+ ->loadByAttribute('url_key', $category_url_key);
518
+
519
+ if ($category instanceof Mage_Catalog_Model_Category) {
520
+ $totalProducts = count($category
521
+ ->setStoreId($storeId)
522
+ ->getProductCollection()
523
+ ->addAttributeToFilter(array(
524
+ array(
525
+ 'attribute' => 'visibility',
526
+ 'in' => array(2, 4)
527
+ )
528
+ ))
529
+ );
530
+ $collection = array();
531
+ if ($totalProducts > 0) {
532
+ $collection = $category->setStoreId($storeId)
533
+ ->getProductCollection()
534
+ ->addAttributeToFilter(array(
535
+ array(
536
+ 'attribute' => 'visibility',
537
+ 'in' => array(2, 4)
538
+ )
539
+ ))
540
+ ->addAttributeToSort($_orderBy, $_typeOrder)
541
+ ->setCurPage($_currentPage)
542
+ ->setPageSize($_pageSize);
543
+ } else if ($showProductsInSubCategories) {
544
+ $children_category_ids = $category->getChildren();
545
+ $resource = Mage::getSingleton("core/resource");
546
+ $read = $resource->getConnection("core_read");
547
+
548
+ $sql = Mage::getModel("catalog/product")
549
+ ->getCollection()
550
+ ->addStoreFilter($storeId)
551
+ ->addAttributeToFilter(array(
552
+ array(
553
+ 'attribute' => 'visibility',
554
+ 'in' => array(2, 4)
555
+ )
556
+ ))
557
+ ->addAttributeToSort($_orderBy, $_typeOrder)
558
+ ->getSelect()
559
+ ->distinct()
560
+ ->join(array('B' => Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index')),
561
+ 'e.entity_id = B.product_id', '')
562
+ ->where('B.category_id in (?)', split(',', $children_category_ids));
563
+
564
+ $rows = $read->fetchAll($sql->__toString());
565
+ $ids = array();
566
+ foreach ($rows as $row) {
567
+ $ids[] = $row['entity_id'];
568
+ }
569
+ $totalProducts = count($ids);
570
+
571
+ $collection = Mage::getModel("catalog/product")
572
+ ->getCollection()
573
+ ->addStoreFilter($storeId)
574
+ ->addAttributeToFilter(array(
575
+ array(
576
+ 'attribute' => 'visibility',
577
+ 'in' => array(2, 4)
578
+ )
579
+ ))
580
+ ->addAttributeToSort($_orderBy, $_typeOrder)
581
+ ->addAttributeToFilter('entity_id', array('in' => $ids))
582
+ ->setCurPage($_currentPage)
583
+ ->setPageSize($_pageSize);
584
+ }
585
+
586
+ $list = array();
587
+ foreach ($collection as $p) {
588
+ $product = Mage::getModel('catalog/product')
589
+ ->setStoreId($storeId)
590
+ ->load($p->getId());
591
+
592
+ $list[] = $product;
593
+ }
594
+
595
+ $totalPages = intval($totalProducts / $_pageSize);
596
+ if ($totalProducts % $_pageSize > 0) {
597
+ $totalPages++;
598
+ }
599
+
600
+ return $this->exportXML($list, $totalProducts, $totalPages, $_currentPage, $_pageSize, $_orderBy, $_typeOrder);
601
+ } else {
602
+ return '';
603
+ }
604
+ }
605
+
606
+ /**
607
+ * Searchs Products, if $category_url_key = '' then searchs all product by keyword
608
+ * else searchs product in category has url_key = $category_url_key
609
+ *
610
+ * If category has no product and $showProductsInSubCategories = true, then search
611
+ * all product in sub categories
612
+ *
613
+ * @param string $store_code
614
+ * @param string $keyword
615
+ * @param string $category_url_key
616
+ * @param int $_currentPage
617
+ * @param int $_pageSize
618
+ * @param string $_orderBy
619
+ * @param string $_typeOrder
620
+ * @param boolean $showProductsInSubCategories
621
+ */
622
+ public function searchProduct($store_code = null, $keyword = '', $category_url_key = '', $_currentPage = 1, $_pageSize = 10, $_orderBy = 'new', $_typeOrder = 'desc', $showProductsInSubCategories = false) {
623
+ $store = $this->getStore($store_code);
624
+ $storeId = $store['store_id'];
625
+
626
+ if (is_null($_currentPage) || $_currentPage == '') {
627
+ $_currentPage = 1;
628
+ }
629
+
630
+ if (is_null($_pageSize) || $_pageSize == '') {
631
+ $_pageSize = 10;
632
+ }
633
+
634
+ if (is_null($_orderBy) || $_orderBy == '' || $_orderBy == 'new') {
635
+ $_orderBy = 'entity_id';
636
+ }
637
+
638
+ if (is_null($_typeOrder) || $_typeOrder == '') {
639
+ $_typeOrder = 'desc';
640
+ }
641
+
642
+ $_orderBy = strtolower($_orderBy);
643
+ $_typeOrder = strtolower($_typeOrder);
644
+
645
+ if (is_null($category_url_key) || $category_url_key == '') {
646
+ $collection = Mage::getModel('catalog/product')->getCollection()
647
+ ->addAttributeToSelect('name')
648
+ ->addStoreFilter($storeId)
649
+ ->addAttributeToFilter(array(
650
+ array(
651
+ 'attribute' => 'name',
652
+ 'like' => '%' . $this->clean_url($keyword) . '%'
653
+ ),
654
+ array(
655
+ 'attribute' => 'url_key',
656
+ 'like' => '%' . $this->clean_url($keyword) . '%'
657
+ ),
658
+ array(
659
+ 'attribute' => 'short_description',
660
+ 'like' => '%' . $this->clean_url($keyword) . '%'
661
+ )
662
+ ))
663
+ ->addAttributeToFilter(array(
664
+ array(
665
+ 'attribute' => 'visibility',
666
+ 'in' => array(3, 4)
667
+ )
668
+ ))
669
+ ->addFieldToFilter("status", 1)
670
+ ->setOrder($_orderBy, $_typeOrder)
671
+ ->setCurPage($_currentPage)
672
+ ->setPageSize($_pageSize)
673
+ ;
674
+
675
+ $products = Mage::getModel('catalog/product')->getCollection()
676
+ ->addAttributeToSelect('name')
677
+ ->addStoreFilter($storeId)
678
+ ->addAttributeToFilter(array(
679
+ array(
680
+ 'attribute' => 'name',
681
+ 'like' => '%' . $this->clean_url($keyword) . '%'
682
+ ),
683
+ array(
684
+ 'attribute' => 'url_key',
685
+ 'like' => '%' . $this->clean_url($keyword) . '%'
686
+ ),
687
+ array(
688
+ 'attribute' => 'short_description',
689
+ 'like' => '%' . $this->clean_url($keyword) . '%'
690
+ )
691
+ ))
692
+ ->addAttributeToFilter(array(
693
+ array(
694
+ 'attribute' => 'visibility',
695
+ 'in' => array(3, 4)
696
+ )
697
+ ))
698
+ ->addFieldToFilter("status", 1);
699
+
700
+ $totalProducts = count($products);
701
+ } else {
702
+ $category = Mage::getModel('catalog/category')
703
+ ->loadByAttribute('url_key', $category_url_key);
704
+
705
+ $totalProducts = count($category
706
+ ->setStoreId($storeId)
707
+ ->getProductCollection()
708
+ ->addAttributeToFilter(array(
709
+ array(
710
+ 'attribute' => 'visibility',
711
+ 'in' => array(3, 4)
712
+ )
713
+ ))
714
+ ->addFieldToFilter("status", 1));
715
+ $collection = array();
716
+ if ($totalProducts > 0) {
717
+ $products = Mage::getModel('catalog/product')->getCollection()
718
+ ->addAttributeToSelect('name')
719
+ ->addStoreFilter($storeId)
720
+ ->addAttributeToFilter(array(
721
+ array(
722
+ 'attribute' => 'name',
723
+ 'like' => '%' . $this->clean_url($keyword) . '%'
724
+ ),
725
+ array(
726
+ 'attribute' => 'url_key',
727
+ 'like' => '%' . $this->clean_url($keyword) . '%'
728
+ ),
729
+ array(
730
+ 'attribute' => 'short_description',
731
+ 'like' => '%' . $this->clean_url($keyword) . '%'
732
+ )
733
+ ))
734
+ ->addCategoryFilter($category)
735
+ ->addAttributeToFilter(array(
736
+ array(
737
+ 'attribute' => 'visibility',
738
+ 'in' => array(3, 4)
739
+ )
740
+ ))
741
+ ->addFieldToFilter("status", 1);
742
+ $totalProducts = count($products);
743
+
744
+ $collection = Mage::getModel('catalog/product')->getCollection()
745
+ ->addAttributeToSelect('name')
746
+ ->addStoreFilter($storeId)
747
+ ->addAttributeToFilter(array(
748
+ array(
749
+ 'attribute' => 'name',
750
+ 'like' => '%' . $this->clean_url($keyword) . '%'
751
+ ),
752
+ array(
753
+ 'attribute' => 'url_key',
754
+ 'like' => '%' . $this->clean_url($keyword) . '%'
755
+ ),
756
+ array(
757
+ 'attribute' => 'short_description',
758
+ 'like' => '%' . $this->clean_url($keyword) . '%'
759
+ )
760
+ ))
761
+ ->addCategoryFilter($category)
762
+ ->addAttributeToFilter(array(
763
+ array(
764
+ 'attribute' => 'visibility',
765
+ 'in' => array(3, 4)
766
+ )
767
+ ))
768
+ ->addFieldToFilter("status", 1)
769
+ ->setOrder($_orderBy, $_typeOrder)
770
+ ->setCurPage($_currentPage)
771
+ ->setPageSize($_pageSize)
772
+ ;
773
+ } else if ($showProductsInSubCategories) {
774
+ $children_category_ids = $category->getChildren();
775
+ $resource = Mage::getSingleton("core/resource");
776
+ $read = $resource->getConnection("core_read");
777
+
778
+ $sql = Mage::getModel("catalog/product")
779
+ ->getCollection()
780
+ ->addStoreFilter($storeId)
781
+ ->addAttributeToFilter(array(
782
+ array(
783
+ 'attribute' => 'name',
784
+ 'like' => '%' . $this->clean_url($keyword) . '%'
785
+ ),
786
+ array(
787
+ 'attribute' => 'url_key',
788
+ 'like' => '%' . $this->clean_url($keyword) . '%'
789
+ ),
790
+ array(
791
+ 'attribute' => 'short_description',
792
+ 'like' => '%' . $this->clean_url($keyword) . '%'
793
+ )
794
+ ))
795
+ ->addAttributeToFilter(array(
796
+ array(
797
+ 'attribute' => 'visibility',
798
+ 'in' => array(3, 4)
799
+ )
800
+ ))
801
+ ->addFieldToFilter("status", 1)
802
+ ->addAttributeToSort($_orderBy, $_typeOrder)
803
+ ->getSelect()
804
+ ->distinct()
805
+ ->join(array('B' => Mage::getSingleton('core/resource')->getTableName('catalog_category_product_index')),
806
+ 'e.entity_id = B.product_id', '')
807
+ ->where('B.category_id in (?)', split(',', $children_category_ids));
808
+
809
+ $rows = $read->fetchAll($sql->__toString());
810
+ $ids = array();
811
+ foreach ($rows as $row) {
812
+ $ids[] = $row['entity_id'];
813
+ }
814
+ $totalProducts = count($ids);
815
+
816
+ $collection = Mage::getModel("catalog/product")
817
+ ->getCollection()
818
+ ->addStoreFilter($storeId)
819
+ ->addAttributeToFilter(array(
820
+ array(
821
+ 'attribute' => 'visibility',
822
+ 'in' => array(3, 4)
823
+ )
824
+ ))
825
+ ->addFieldToFilter("status", 1)
826
+ ->addAttributeToSort($_orderBy, $_typeOrder)
827
+ ->addAttributeToFilter('entity_id', array('in' => $ids))
828
+ ->setCurPage($_currentPage)
829
+ ->setPageSize($_pageSize);
830
+ }
831
+ }
832
+
833
+ $list = array();
834
+ foreach ($collection as $p) {
835
+ $product = Mage::getModel('catalog/product')
836
+ ->setStoreId($storeId)
837
+ ->load($p->getId());
838
+
839
+ $list[] = $product;
840
+ }
841
+
842
+ $totalPages = intval($totalProducts / $_pageSize);
843
+ if ($totalProducts % $_pageSize > 0) {
844
+ $totalPages++;
845
+ }
846
+
847
+ return $this->exportXML($list, $totalProducts, $totalPages, $_currentPage, $_pageSize, $_orderBy, $_typeOrder);
848
+ }
849
+
850
+ /**
851
+ * Get product detail
852
+ */
853
+ public function getProductDetail($store_code = null, $product_url_key = null) {
854
+ try {
855
+ $store = $this->getStore($store_code);
856
+ $storeId = $store['store_id'];
857
+ $product = Mage::getModel('catalog/product')
858
+ ->loadByAttribute('url_key', $product_url_key);
859
+
860
+ $product = Mage::getModel('catalog/product')
861
+ ->setStoreId($storeId)
862
+ ->load($product->getId());
863
+
864
+ $xml = '<?xml version="1.0"?><root>';
865
+ $i = 0;
866
+ $xml .= $this->exportXMLProduct($product, $i, true);
867
+ $xml .= '</root>';
868
+
869
+ return $xml;
870
+ } catch (Exception $e) {
871
+ Zend_Debug::dump($e);
872
+ die();
873
+ }
874
+ }
875
+
876
+ public function clean_url($text) {
877
+ $text = strtolower($text);
878
+ $code_entities_match = array(' ', '--', '&quot;', '!', '@', '#', '#x27;, '%', '^', '&', '*', '(', ')', '_', '+', '{', '}', '|', ':', '"', '<', '>', '?', '[', ']', '\\', ';', "'", ',', '.', '/', '*', '+', '~', '`', '=');
879
+ $code_entities_replace = array('-', '-', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '');
880
+ $text = str_replace($code_entities_match, $code_entities_replace, $text);
881
+ return $text;
882
+ }
883
+
884
+ }
app/code/local/Magestore/Mageconnector/controllers/IndexController.php ADDED
@@ -0,0 +1,269 @@
1
+ <?php
2
+
3
+ class Magestore_Mageconnector_IndexController extends Mage_Core_Controller_Front_Action {
4
+
5
+ public function indexAction() {
6
+ $action = $this->getRequest()->get('action');
7
+
8
+ switch ($action) {
9
+ case 'xml':
10
+ $this->xml();
11
+ break;
12
+ case 'view':
13
+ $this->storeView();
14
+ break;
15
+ case 'category':
16
+ $this->category();
17
+ break;
18
+ case 'addToCart':
19
+ $this->addToCart();
20
+ break;
21
+ case 'refreshCart':
22
+ $this->refreshCart();
23
+ break;
24
+ case 'updateCart':
25
+ $this->updateCart();
26
+ break;
27
+ case 'cartApplyCoupon':
28
+ $this->cartApplyCoupon();
29
+ break;
30
+ case 'cartCancelCoupon':
31
+ $this->cartCancelCoupon();
32
+ break;
33
+ case 'deleteCartItem':
34
+ $this->deleteCartItem();
35
+ break;
36
+ case 'checkout':
37
+ $this->checkout();
38
+ break;
39
+ default:
40
+ $this->product();
41
+ }
42
+ }
43
+
44
+ private function checkout() {
45
+ $view = $this->getRequest()->getParam('view');
46
+ if (!is_null($view) && $view != '') {
47
+ header("Location: " . Mage::getBaseUrl() . 'checkout/?___store=' . $view);
48
+ } else {
49
+ header("Location: " . Mage::getBaseUrl() . 'checkout/');
50
+ }
51
+ }
52
+
53
+ private function addCartDataToUrl(&$url) {
54
+ $cartData = Mage::getModel('mageconnector/cart')
55
+ ->exportXML();
56
+
57
+ $cartData = urlencode($cartData);
58
+ $url .= "&cart=$cartData";
59
+ }
60
+
61
+ private function addToCart() {
62
+ $result = Mage::getModel('mageconnector/cart')
63
+ ->addToCart();
64
+
65
+ $url = $_REQUEST['url'];
66
+ if ($result) {
67
+ $url .= "&addToCart=success";
68
+ } else {
69
+ $url .= "&addToCart=unsuccess";
70
+ }
71
+ $this->addCartDataToUrl($url);
72
+ header("Location: $url");
73
+ }
74
+
75
+ private function refreshCart() {
76
+ $url = $_REQUEST['url'];
77
+ $url .= "&refreshCart=success";
78
+ $this->addCartDataToUrl($url);
79
+ header("Location: $url");
80
+ }
81
+
82
+ private function cartApplyCoupon() {
83
+ $result = Mage::getModel('mageconnector/cart')
84
+ ->applyCoupon();
85
+
86
+ $url = $_REQUEST['couponUrl'];
87
+ if ($result) {
88
+ $url .= "&applyCoupon=success&couponCode=" . $_REQUEST['coupon_code'];
89
+ } else {
90
+ $url .= "&applyCoupon=unsuccess&couponCode=" . $_REQUEST['coupon_code'];
91
+ }
92
+ $this->addCartDataToUrl($url);
93
+ header("Location: $url");
94
+ }
95
+
96
+ private function cartCancelCoupon() {
97
+ $result = Mage::getModel('mageconnector/cart')
98
+ ->cancelCoupon();
99
+
100
+ $url = $_REQUEST['couponUrl'];
101
+ if ($result) {
102
+ $url .= "&cancelCoupon=success";
103
+ } else {
104
+ $url .= "&cancelCoupon=unsuccess";
105
+ }
106
+ $this->addCartDataToUrl($url);
107
+ header("Location: $url");
108
+ }
109
+
110
+ private function updateCart() {
111
+ $result = Mage::getModel('mageconnector/cart')
112
+ ->updateCart();
113
+
114
+ $url = $_REQUEST['url'];
115
+ if ($result) {
116
+ $url .= "&updateCart=success";
117
+ } else {
118
+ $url .= "&updateCart=unsuccess";
119
+ }
120
+ $this->addCartDataToUrl($url);
121
+ header("Location: $url");
122
+ }
123
+
124
+ private function deleteCartItem() {
125
+ $result = Mage::getModel('mageconnector/cart')
126
+ ->deleteCartItem();
127
+
128
+ $url = $_REQUEST['url'];
129
+ if ($result) {
130
+ $url .= "&deleteCartItem=success";
131
+ } else {
132
+ $url .= "&deleteCartItem=unsuccess";
133
+ }
134
+ $this->addCartDataToUrl($url);
135
+ header("Location: $url");
136
+ }
137
+
138
+ private function storeView() {
139
+ $output = Mage::helper('mageconnector')->getListStoreView();
140
+
141
+ $response = $this->getResponse();
142
+ $response->setHeader('Content-Type', 'text/xml; charset=utf-8')
143
+ ->setBody($output);
144
+ }
145
+
146
+ private function category() {
147
+ $store_code = $this->getRequest()->get('view');
148
+
149
+ if ($store_code == '') {
150
+ $store_code = null;
151
+ }
152
+
153
+ $output = Mage::getModel('mageconnector/category')
154
+ ->exportXML($store_code);
155
+
156
+ $response = $this->getResponse();
157
+ $response->setHeader('Content-Type', 'text/xml; charset=utf-8')
158
+ ->setBody($output);
159
+ }
160
+
161
+ private function product() {
162
+ $keyword = $this->getRequest()->get('keyword');
163
+ $category = $this->getRequest()->get('category');
164
+
165
+ if ($keyword != '') {
166
+ $output = $this->productSearch($keyword, $category);
167
+ } else {
168
+ $urlKey = $this->getRequest()->get('url_key');
169
+ if ($urlKey != '') {
170
+ $output = $this->productDetail($urlKey);
171
+ } else {
172
+ if ($category != '') {
173
+ $output = $this->productCategory($category);
174
+ } else {
175
+ $output = $this->allProducts();
176
+ }
177
+ }
178
+ }
179
+
180
+ $response = $this->getResponse();
181
+ $response->setHeader('Content-Type', 'text/xml; charset=utf-8')
182
+ ->setBody($output);
183
+ }
184
+
185
+ private function productCategory($category) {
186
+ $store_code = $this->getRequest()->get('view');
187
+
188
+ $currentPage = $this->getRequest()->get('page');
189
+ $pageSize = $this->getRequest()->get('pageSize');
190
+ $orderBy = $this->getRequest()->get('orderBy');
191
+ $typeOrder = $this->getRequest()->get('typeOrder');
192
+ $showProductsInSubCategories = $this->getRequest()->get('showProductsInSubCategories');
193
+
194
+ return Mage::getModel('mageconnector/product')
195
+ ->getProductListByCategory($store_code, $category, $currentPage, $pageSize, $orderBy, $typeOrder, $showProductsInSubCategories);
196
+ }
197
+
198
+ private function productSearch($keyword, $category) {
199
+ $store_code = $this->getRequest()->get('view');
200
+
201
+ $currentPage = $this->getRequest()->get('page');
202
+ $pageSize = $this->getRequest()->get('pageSize');
203
+ $orderBy = $this->getRequest()->get('orderBy');
204
+ $typeOrder = $this->getRequest()->get('typeOrder');
205
+ $showProductsInSubCategories = $this->getRequest()->get('showProductsInSubCategories');
206
+
207
+ return Mage::getModel('mageconnector/product')
208
+ ->searchProduct($store_code, $keyword, $category, $currentPage, $pageSize, $orderBy, $typeOrder, $showProductsInSubCategories);
209
+ }
210
+
211
+ private function productDetail($productUrlKey) {
212
+ $store_code = $this->getRequest()->get('view');
213
+
214
+ return Mage::getModel('mageconnector/product')
215
+ ->getProductDetail($store_code, $productUrlKey);
216
+ }
217
+
218
+ private function allProducts() {
219
+ $store_code = $this->getRequest()->get('view');
220
+
221
+ $currentPage = $this->getRequest()->get('page');
222
+ $pageSize = $this->getRequest()->get('pageSize');
223
+ $orderBy = $this->getRequest()->get('orderBy');
224
+ $typeOrder = $this->getRequest()->get('typeOrder');
225
+
226
+ return Mage::getModel('mageconnector/product')
227
+ ->searchProduct($store_code, '', '', $currentPage, $pageSize, $orderBy, $typeOrder);
228
+ }
229
+
230
+ public function xml() {
231
+ $products = Mage::getModel('catalog/product')->getCollection()
232
+ ->addAttributeToSelect('name')
233
+ ->addStoreFilter(1)
234
+ ->addAttributeToFilter(array(
235
+ array(
236
+ 'attribute' => 'visibility',
237
+ 'in' => array(3, 4)
238
+ )
239
+ ))
240
+ ->addFieldToFilter("status", 1);
241
+
242
+ $xml = '<?xml version="1.0"?><products>';
243
+
244
+ $i = 0;
245
+ foreach ($products as $p) {
246
+ $product = Mage::getModel('catalog/product')
247
+ ->setStoreId(1)
248
+ ->load($p->getId());
249
+ $xml .= '<product order="#' . ++$i . '">'
250
+ . '<id>' . $product->getId() . '</id>'
251
+ . '<name><![CDATA[' . $product->getName() . ']]></name>'
252
+ . '<price><![CDATA[' . Mage::helper('core')
253
+ ->currency(Mage::helper('tax')
254
+ ->getPrice(
255
+ $product,
256
+ $product->getFinalPrice()
257
+ )
258
+ , true, false) . ']]></price>'
259
+ . '<description><![CDATA[' . $product->getShortDescription() . ']]></description>'
260
+ . '</product>';
261
+ }
262
+
263
+ $xml .= '</products>';
264
+ $response = $this->getResponse();
265
+ $response->setHeader('Content-Type', 'text/xml; charset=utf-8')
266
+ ->setBody($xml);
267
+ }
268
+
269
+ }
app/code/local/Magestore/Mageconnector/etc/adminhtml.xml ADDED
@@ -0,0 +1,46 @@
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <menu>
4
+ <catalog>
5
+ <children>
6
+ <mageconnector>
7
+ <title>Magestore Connector Settings</title>
8
+ <sort_order>20</sort_order>
9
+ <action>adminhtml/system_config/edit/section/mageconnector</action>
10
+ </mageconnector>
11
+ </children>
12
+ </catalog>
13
+ </menu>
14
+ <acl>
15
+ <resources>
16
+ <all>
17
+ <title>Allow Everything</title>
18
+ </all>
19
+ <admin>
20
+ <children>
21
+ <catalog>
22
+ <children>
23
+ <mageconnector>
24
+ <title>Magestore Connector Settings</title>
25
+ <sort_order>20</sort_order>
26
+ <action>adminhtml/system_config/edit/section/mageconnector</action>
27
+ </mageconnector>
28
+ </children>
29
+ </catalog>
30
+ <system>
31
+ <children>
32
+ <config>
33
+ <children>
34
+ <mageconnector translate="title">
35
+ <title>Magestore Connector Settings</title>
36
+ <sort_order>50</sort_order>
37
+ </mageconnector>
38
+ </children>
39
+ </config>
40
+ </children>
41
+ </system>
42
+ </children>
43
+ </admin>
44
+ </resources>
45
+ </acl>
46
+ </config>
app/code/local/Magestore/Mageconnector/etc/config.xml ADDED
@@ -0,0 +1,135 @@
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Magestore_Mageconnector>
5
+ <version>0.1.0</version>
6
+ </Magestore_Mageconnector>
7
+ </modules>
8
+ <frontend>
9
+ <routers>
10
+ <mageconnector>
11
+ <use>standard</use>
12
+ <args>
13
+ <module>Magestore_Mageconnector</module>
14
+ <frontName>mageconnector</frontName>
15
+ </args>
16
+ </mageconnector>
17
+ </routers>
18
+ <layout>
19
+ <updates>
20
+ <mageconnector>
21
+ <file>mageconnector.xml</file>
22
+ </mageconnector>
23
+ </updates>
24
+ </layout>
25
+ </frontend>
26
+ <admin>
27
+ <routers>
28
+ <mageconnector>
29
+ <use>admin</use>
30
+ <args>
31
+ <module>Magestore_Mageconnector</module>
32
+ <frontName>mageconnector</frontName>
33
+ </args>
34
+ </mageconnector>
35
+ </routers>
36
+ </admin>
37
+ <adminhtml>
38
+ <menu>
39
+ <catalog>
40
+ <children>
41
+ <mageconnector>
42
+ <title>Magestore Connector Settings</title>
43
+ <sort_order>20</sort_order>
44
+ <action>adminhtml/system_config/edit/section/mageconnector</action>
45
+ </mageconnector>
46
+ </children>
47
+ </catalog>
48
+ </menu>
49
+ <acl>
50
+ <resources>
51
+ <all>
52
+ <title>Allow Everything</title>
53
+ </all>
54
+ <admin>
55
+ <children>
56
+ <catalog>
57
+ <children>
58
+ <mageconnector>
59
+ <title>Magestore Connector Settings</title>
60
+ <sort_order>20</sort_order>
61
+ <action>adminhtml/system_config/edit/section/mageconnector</action>
62
+ </mageconnector>
63
+ </children>
64
+ </catalog>
65
+ <system>
66
+ <children>
67
+ <config>
68
+ <children>
69
+ <mageconnector translate="title">
70
+ <title>Magestore Connector Settings</title>
71
+ <sort_order>50</sort_order>
72
+ </mageconnector>
73
+ </children>
74
+ </config>
75
+ </children>
76
+ </system>
77
+ </children>
78
+ </admin>
79
+ </resources>
80
+ </acl>
81
+ <layout>
82
+ <updates>
83
+ <mageconnector>
84
+ <file>mageconnector.xml</file>
85
+ </mageconnector>
86
+ </updates>
87
+ </layout>
88
+ </adminhtml>
89
+ <global>
90
+ <models>
91
+ <mageconnector>
92
+ <class>Magestore_Mageconnector_Model</class>
93
+ <resourceModel>mageconnector_mysql4</resourceModel>
94
+ </mageconnector>
95
+ <mageconnector_mysql4>
96
+ <class>Magestore_Mageconnector_Model_Mysql4</class>
97
+ <entities>
98
+ <mageconnector>
99
+ <table>mageconnector_featured_products</table>
100
+ </mageconnector>
101
+ </entities>
102
+ </mageconnector_mysql4>
103
+ </models>
104
+ <resources>
105
+ <mageconnector_setup>
106
+ <setup>
107
+ <module>Magestore_Mageconnector</module>
108
+ </setup>
109
+ <connection>
110
+ <use>core_setup</use>
111
+ </connection>
112
+ </mageconnector_setup>
113
+ <mageconnector_write>
114
+ <connection>
115
+ <use>core_write</use>
116
+ </connection>
117
+ </mageconnector_write>
118
+ <mageconnector_read>
119
+ <connection>
120
+ <use>core_read</use>
121
+ </connection>
122
+ </mageconnector_read>
123
+ </resources>
124
+ <blocks>
125
+ <mageconnector>
126
+ <class>Magestore_Mageconnector_Block</class>
127
+ </mageconnector>
128
+ </blocks>
129
+ <helpers>
130
+ <mageconnector>
131
+ <class>Magestore_Mageconnector_Helper</class>
132
+ </mageconnector>
133
+ </helpers>
134
+ </global>
135
+ </config>
app/code/local/Magestore/Mageconnector/etc/system.xml ADDED
@@ -0,0 +1,43 @@
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <tabs>
4
+ <magestore translate="label">
5
+ <label>Magestore Extension</label>
6
+ <sort_order>400</sort_order>
7
+ </magestore>
8
+ </tabs>
9
+ <sections>
10
+ <mageconnector translate="label" module="mageconnector">
11
+ <class>separator-top</class>
12
+ <label>Magestore Connector</label>
13
+ <tab>magestore</tab>
14
+ <frontend_type>text</frontend_type>
15
+ <sort_order>300</sort_order>
16
+ <show_in_default>1</show_in_default>
17
+ <show_in_website>1</show_in_website>
18
+ <show_in_store>1</show_in_store>
19
+ <groups>
20
+ <store translate="label">
21
+ <label>Store View Connector Information</label>
22
+ <frontend_type>text</frontend_type>
23
+ <sort_order>0</sort_order>
24
+ <show_in_default>1</show_in_default>
25
+ <show_in_website>1</show_in_website>
26
+ <show_in_store>1</show_in_store>
27
+ <fields>
28
+ <default_store_view translate="label">
29
+ <label>Default store view</label>
30
+ <frontend_type>select</frontend_type>
31
+ <source_model>mageconnector/importstore</source_model>
32
+ <sort_order>0</sort_order>
33
+ <show_in_default>1</show_in_default>
34
+ <show_in_website>1</show_in_website>
35
+ <show_in_store>1</show_in_store>
36
+ <comment>Default store view will export data went external website don't request view parameter</comment>
37
+ </default_store_view>
38
+ </fields>
39
+ </store>
40
+ </groups>
41
+ </mageconnector>
42
+ </sections>
43
+ </config>
app/code/local/Magestore/Mageconnector/sql/mageconnector_setup/mysql4-install-0.1.0.php ADDED
@@ -0,0 +1,17 @@
1
+ <?php
2
+ /**
3
+ * Create table data
4
+ *
5
+ * @category Magestore
6
+ * @package Magestore_Mageconnector
7
+ * @author Nguyen Minh Dung (http://oss.com.vn)
8
+ */
9
+ $installer = $this;
10
+
11
+ $installer->startSetup();
12
+
13
+ $installer->run("
14
+
15
+ ");
16
+
17
+ $installer->endSetup();
app/etc/modules/Magestore_Mageconnector.xml ADDED
@@ -0,0 +1,9 @@
1
+ <?xml version="1.0"?>
2
+ <config>
3
+ <modules>
4
+ <Magestore_Mageconnector>
5
+ <active>true</active>
6
+ <codePool>local</codePool>
7
+ </Magestore_Mageconnector>
8
+ </modules>
9
+ </config>
package.xml ADDED
@@ -0,0 +1,18 @@