Poq_Integration - Version 2.0.0.1.1

Version Notes

Category sort index support added.

Download this release

Release Info

Developer Oyvind Henriksen
Extension Poq_Integration
Version 2.0.0.1.1
Comparing to
See all releases


Code changes from version 2.0.0.0.0 to 2.0.0.1.1

app/code/community/Poq/Integration/.DS_Store CHANGED
Binary file
app/code/community/Poq/Integration/Helper/Data.php CHANGED
@@ -116,7 +116,7 @@ class Poq_Integration_Helper_Data extends Mage_Core_Helper_Data {
116
 
117
  // Get safe referer list
118
  $this->safe_referer_list = Mage::getStoreConfig('integration/integration_checkoutsecurity_group/integration_checkout_safereferrerlist_input', Mage::app()->getStore());
119
- $this->safe_referer_list = split(',', $this->safe_referer_list);
120
 
121
  // Get tracking code
122
  $this->tracking_code = Mage::getStoreConfig('integration/integration_checkout_group/integration_checkout_trackingcode_input', Mage::app()->getStore());
116
 
117
  // Get safe referer list
118
  $this->safe_referer_list = Mage::getStoreConfig('integration/integration_checkoutsecurity_group/integration_checkout_safereferrerlist_input', Mage::app()->getStore());
119
+ $this->safe_referer_list = explode(',', $this->safe_referer_list);
120
 
121
  // Get tracking code
122
  $this->tracking_code = Mage::getStoreConfig('integration/integration_checkout_group/integration_checkout_trackingcode_input', Mage::app()->getStore());
app/code/community/Poq/Integration/controllers/IndexController.php CHANGED
@@ -14,14 +14,7 @@ class Poq_Integration_IndexController extends Mage_Core_Controller_Front_Action
14
  public function indexAction()
15
  {
16
 
17
- // Get layout
18
- $this->loadLayout();
19
-
20
- // Create static block dynmacially
21
- $block = $this->getLayout()->createBlock('Mage_Core_Block_Template', 'Integration Settings', array('template' => 'poqintegration/index.phtml'));
22
-
23
- // Push block to layout
24
- $this->getLayout()->getBlock('content')->append($block);
25
 
26
  // Show settings
27
  //Set CSV content type
@@ -31,17 +24,12 @@ class Poq_Integration_IndexController extends Mage_Core_Controller_Front_Action
31
  $settings = Mage::helper('poq_integration')->getSettings();
32
  var_dump($settings);
33
  }
34
-
35
- // Render page
36
- $this->renderLayout();
37
  }
38
 
39
-
40
  /**
41
  * Gets all categories indexed by id
42
  * @return array
43
  */
44
-
45
  private function getCategories()
46
  {
47
  // Init category name and url arrays indexed by id
@@ -106,13 +94,19 @@ class Poq_Integration_IndexController extends Mage_Core_Controller_Front_Action
106
  $url .= $activeCategoryUrlsById[$parent] . '>';
107
  }
108
 
 
 
 
 
 
109
  // Index category data
110
  $categories[$categoryId] = array(
111
  'name' => substr($name, 0, -1),
112
  'level' => $category['level'],
113
  'ids' => str_replace('/', '>', $path),
114
  'url' => substr($url, 0, -1),
115
- 'id' => $categoryId
 
116
  );
117
  }
118
  }
@@ -120,6 +114,11 @@ class Poq_Integration_IndexController extends Mage_Core_Controller_Front_Action
120
  return $categories;
121
  }
122
 
 
 
 
 
 
123
  /**
124
  * Create csv feed file with respecto extension settings
125
  */
@@ -127,391 +126,444 @@ class Poq_Integration_IndexController extends Mage_Core_Controller_Front_Action
127
  {
128
 
129
  // Get extension settings
130
- $settings = Mage::helper('poq_integration')->getSettings();
131
 
132
- //Configuration values
133
- $store_id = $settings->store_id;
134
- $image_base_url = $settings->image_base_url;
135
- $image_ignore_strings = $settings->image_ignore_string;
136
- $tax_rates_enabled = $settings->tax_enabled;
137
 
138
- //Trying to get default values
139
- if ($store_id == 0)
 
140
  {
141
- $store_id = Mage::app()->getStore()->getStoreId();
142
  }
143
 
144
- //Set CSV content type
145
- if ($_GET['debug'] == '1')
 
146
  {
147
- header("Content-type: text/plain");
148
  }
149
- else
150
  {
151
- header("Content-type: text/csv");
152
- header("Content-Disposition: attachment; filename=feed.csv");
153
  }
154
- header("Pragma: no-cache");
155
- header("Expires: 0");
156
-
157
-
158
- // Init product attribute arrays
159
- $productSizeDescriptions = array();
160
- $parentProductIds = array();
161
- $productColors = array();
162
-
163
-
164
- // Attribute label configurations
165
- // These can be moved to integration settings
166
- // They always be lowercase for code convention
167
- $colorAttributeLabel = "color";
168
- $sizeAttributeLabel = "size";
169
-
170
- $conn = Mage::getSingleton('core/resource')->getConnection('core_setup');
171
- $collection = Mage::getModel('catalog/product')->getCollection()
172
- ->joinField(
173
- 'qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'
174
- )
175
- ->addAttributeToFilter('status', 1) // enabled
176
- ->addUrlRewrite()
177
- ->addPriceData()
178
- ->addStoreFilter($store_id)
179
- ->addAttributeToSelect('*');
180
- Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($collection);
181
- Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
182
- $collection->setOrder('sku', 'desc');
183
 
184
- if ($_GET['debug'] == '1')
185
  {
186
- //only get the first hundred rows, useful for faster testing. Also enable profiler, so we can see performance data.
187
- $collection->setPageSize(200)->setCurPage(1);
188
- $conn->getProfiler()->setEnabled(true);
189
- echo $collection->getSelect()->__toString() . "\n\n";
190
  }
191
 
192
- //Read the dataset into memory
193
- $collection->load();
194
-
195
- //Preload the media_gallery image data into memory
196
- //For use with Option #1 below
197
- $_mediaGalleryAttributeId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'media_gallery')->getAttributeId();
198
- $_read = Mage::getSingleton('core/resource')->getConnection('catalog_read');
199
- $_mediaGalleryData = $_read->fetchAll('SELECT
200
- main.entity_id, `main`.`value_id`, `main`.`value` AS `file`,
201
- `value`.`label`, `value`.`position`, `value`.`disabled`, `default_value`.`label` AS `label_default`,
202
- `default_value`.`position` AS `position_default`,
203
- `default_value`.`disabled` AS `disabled_default`
204
- FROM `catalog_product_entity_media_gallery` AS `main`
205
- LEFT JOIN `catalog_product_entity_media_gallery_value` AS `value`
206
- ON main.value_id=value.value_id AND value.store_id=' . $store_id . '
207
- LEFT JOIN `catalog_product_entity_media_gallery_value` AS `default_value`
208
- ON main.value_id=default_value.value_id AND default_value.store_id=0
209
- WHERE (
210
- main.attribute_id = ' . $_read->quote($_mediaGalleryAttributeId) . ')
211
- AND (main.entity_id IN (' . $_read->quote($collection->getAllIds()) . '))
212
- ORDER BY IF(value.position IS NULL, default_value.position, value.position) ASC
213
- ');
214
- $_mediaGalleryByProductId = array();
215
- foreach ($_mediaGalleryData as $_galleryImage)
216
  {
217
- $k = $_galleryImage['entity_id'];
218
- unset($_galleryImage['entity_id']);
219
- if (!isset($_mediaGalleryByProductId[$k]))
220
- {
221
- $_mediaGalleryByProductId[$k] = array();
222
- }
223
- $_mediaGalleryByProductId[$k][] = $_galleryImage;
224
  }
225
- unset($_mediaGalleryData);
226
- foreach ($collection as &$_product)
227
  {
228
- $_productId = $_product->getData('entity_id');
229
- if (isset($_mediaGalleryByProductId[$_productId]))
 
 
 
 
 
 
 
 
230
  {
231
- $_product->setData('media_gallery', array('images' => $_mediaGalleryByProductId[$_productId]));
232
  }
233
- }
234
- unset($_mediaGalleryByProductId);
235
-
236
-
237
- // End of media_gallery queries for Option #1
238
- //Go through configurable products, and preload data into $parentProductIds and $productSizeDescriptions
239
- foreach ($collection as $product)
240
- {
241
- if ($product->getTypeId() == "configurable")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
242
  {
 
 
 
 
 
243
 
244
- //Get list of related sub-products
245
- $productids = $product->getTypeInstance()->getUsedProductIds();
246
- foreach ($productids as $productid)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
247
  {
248
- $parentProductIds[$productid] = $product->getId(); //Add to array of related products, so we can look up when we loop through the main products later.
249
  }
 
 
 
 
 
 
 
 
 
 
 
 
250
 
251
- // Collect options applicable to the configurable product
252
- $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($product);
253
-
254
- // Get all attributes of the super products
255
- $productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
256
 
257
- // Group attributes of children products
258
- foreach ($productAttributeOptions as $productAttribute)
 
 
 
259
  {
260
 
261
- $code = $productAttribute["attribute_code"];
 
 
 
 
 
262
 
263
- // Find attribute contains "color"
264
- if (strpos($code, $colorAttributeLabel) !== false)
 
 
 
 
 
 
265
  {
266
- // Get children products used attribute code
267
- $col = $conf->getUsedProductCollection()->addAttributeToSelect($code)->addFilterByRequiredOptions();
268
- foreach ($col as $simple_product)
 
 
269
  {
270
- $productColors[$simple_product->getId()] = $simple_product->getAttributeText($code);
 
 
 
 
 
271
  }
272
- }
273
 
274
 
275
- // Find attribute contains "size"
276
- if (strpos($code, $sizeAttributeLabel) !== false)
277
- {
278
- // Get children products used attribute code
279
- $col = $conf->getUsedProductCollection()->addAttributeToSelect($code)->addFilterByRequiredOptions();
280
- foreach ($col as $simple_product)
281
  {
282
- $productSizeDescriptions[$simple_product->getId()] = $simple_product->getAttributeText($code);
 
 
 
 
 
283
  }
284
  }
285
  }
286
  }
287
- }
288
-
289
- // Get all active categories
290
- $categories = $this->getCategories();
291
-
292
-
293
-
294
- //Add one line of headers above the actual content.
295
- echo "\"id\"";
296
- echo ",\"name\"";
297
- echo ",\"description\"";
298
- echo ",\"price\"";
299
- echo ",\"specialprice\"";
300
- echo ",\"parentproductid\"";
301
- echo ",\"sku\"";
302
- echo ",\"isinstock\"";
303
- echo ",\"quantity\"";
304
- echo ",\"size\"";
305
- echo ",\"color\"";
306
- echo ",\"colorGroupId\"";
307
- echo ",\"productURL\"";
308
- echo ",\"pictureurls\"";
309
- echo ",\"categoryid\"";
310
- echo ",\"categoryname\"";
311
- echo ",\"categoryurl\"";
312
- echo "\n";
313
-
314
- // Get tax rates if enabled
315
- $tax_classes;
316
-
317
- if ($tax_rates_enabled)
318
- {
319
 
320
- // Get all tax classes with their values
321
- $tax_classes = Mage::helper('tax')->getAllRatesByProductClass();
322
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
323
 
324
- //Loop through the products, get the values and write them in CSV format
325
- foreach ($collection as $product)
326
- {
327
 
328
- $description = strip_tags($product->getDescription()); //Remove HTML tags from the description
329
- $description = addslashes($description); //Escape quotes etc from the text
330
- $description = preg_replace('#\s{2,}#', '\\n', $description); //Remove line breaks, "\\n" will be put back in as line breaks when importing.
331
- //$description = preg_replace('/(?<!,)"(?!,)/', '""', $description); //double up any double quote that is not immediately preceded or followed by a comma
332
- $description = preg_replace('/\"/', '""', $description); //double up any double quote
333
 
334
- echo "\"" . $product->getId() . "\"";
335
- echo ",\"" . addslashes($product->getName()) . "\"";
336
- echo ",\"" . $description . "\"";
337
 
338
- // Original price might have been tax rates applied on live site
339
- // Fix by macromania
340
- //echo ",\"" . $product->getPrice() . "\""; )
341
- try
342
  {
343
 
344
- // Original price without tax
345
- $product_price = $product->getPrice();
346
- $special_price = $product->getFinalPrice();
 
 
347
 
348
- if ($tax_rates_enabled)
 
 
 
 
 
 
 
349
  {
350
 
351
- // Get product tax class id
352
- $tax_class_id = $_product->getData('tax_class_id');
 
 
 
 
353
 
354
- // $tax_classes is returned as string. So parsing is needed to get the value
355
- // an example $tax_classes could be like {"value_2":20,"value_4":20,"value_5":20,"value_6":0,"value_7":5}
356
- $tax_classes = str_replace('{', '', $tax_classes);
357
- $tax_classes = str_replace('}', '', $tax_classes);
358
- $tax_class_value_array = explode(',', $tax_classes);
359
 
360
- // Tax value
361
- $product_tax_value = 0;
 
 
 
362
 
363
- foreach ($tax_class_value_array as $tax_class)
364
- {
365
 
366
- $values = explode(':', $tax_class);
367
- if ($values[0] == '"value_' . $tax_class_id . '"')
368
  {
369
- // Get the rate
370
- $product_tax_value = $values[1];
 
 
 
 
 
 
 
 
 
 
 
 
371
  }
372
- }
373
 
374
- // Apply tax rate
375
- if ($product_tax_value > 0)
 
 
376
  {
377
- $product_price += $product_price * $product_tax_value / 100;
378
- $special_price += $special_price * $product_tax_value / 100;
379
  }
380
 
381
- echo ",\"" . ceil($product_price) . "\"";
382
- echo ",\"" . ceil($special_price) . "\"";
 
383
  }
384
- else
385
  {
386
- echo ",\"" . $product_price . "\"";
387
- echo ",\"" . $special_price . "\"";
388
  }
389
 
390
 
391
-
392
- //echo '<p style="color:red">------<br/>Caught exception: ', ceil($product_price),'----',$product->getPrice(), '<br/>-----</p>';
393
- }
394
- catch (Exception $eproductprice)
395
- {
396
- echo '<p style="color:red">------<br/>Caught exception: ', $e2->getMessage(), '<br/>-----</p>';
397
- }
398
-
399
-
400
- // Parent product id
401
- $parentProductIdCombined = $parentProductIds[$product->getId()];
402
- if ($productColors[$product->getId()])
403
- {
404
- if (!empty($productColors[$product->getId()]))
405
  {
406
- $parentProductIdCombined .= "-" . $productColors[$product->getId()];
 
 
 
407
  }
408
- }
409
 
410
 
411
- echo ",\"" . $parentProductIdCombined . "\"";
412
 
413
- // SKU
414
- echo ",\"" . $product->getSku() . "\"";
415
 
416
- // Stock
417
- echo ",\"" . $product->getStockItem()->getIsInStock() . "\"";
418
 
419
- // Stock Quantity
420
- echo ",\"" . $product->getQty() . "\"";
421
 
422
- // Size
423
- echo ",\"" . $productSizeDescriptions[$product->getId()] . "\"";
424
 
425
- // Color name
426
- echo ",\"" . $productColors[$product->getId()] . "\"";
427
 
428
- // Color group id
429
- echo ",\"" . $parentProductIds[$product->getId()] . "\"";
430
 
431
- // Color
432
- /*
433
- $productColor = "test";
434
- echo ",\"" . $productColor . "\"";
435
- *
436
- */
437
 
438
- if ($product->getVisibility() == 4)
439
- { //Only products that are individually visible need URLs, pictures, categories.
440
- //Get product URL
441
- echo ",\"" . $product->getProductUrl() . "\"";
442
 
443
- //Get main image
444
- $imageString = $product->getMediaConfig()->getMediaUrl($product->getData('image'));
445
 
446
- //attempt to audo-detect base image url, if necessary
447
- if ($image_base_url == '')
448
- {
449
- $image_base_url = substr($imageString, 0, strrpos($imageString, 'media/catalog/product')) . 'media/catalog/product';
450
- }
451
-
452
- //Option #1 - Fast way to get all media gallery images from preloaded array
453
- $_images = $product->getData('media_gallery');
454
- foreach ($_images as $imagegallery)
455
- {
456
- foreach ($imagegallery as $add_image)
457
  {
 
 
458
 
459
- //Check if image should be filtered.
460
- $image_should_be_added = true;
461
- if ($image_ignore_strings != '')
 
 
462
  {
463
- //echo "\n image_ignore_strings: " . $image_ignore_strings;
464
- $image_ignore_string_array = explode(",", $image_ignore_strings);
465
- foreach ($image_ignore_string_array as $image_ignore_string)
 
466
  {
467
- //echo "\n image_ignore_string: " . $image_ignore_string;
468
- if (strpos($add_image['file'], $image_ignore_string) !== false)
 
469
  {
470
- //echo "\n IGNORING: " . $add_image['file'];
471
- $image_should_be_added = false;
 
 
 
 
472
  }
473
  }
 
 
 
 
474
  }
475
- if ($image_should_be_added)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
476
  {
477
- $imageString .= ',' . $image_base_url . $add_image['file'];
478
  }
479
  }
480
- }
481
 
482
- //Option #2 - Slower, but more reliable way to get all images. Try this is option #1 does not run
483
- // $_images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages();
484
- // foreach ($_images as $imagegallery) {
485
- // $imageString .= ';' . $imagegallery['url'];
486
- // }
487
- echo ",\"" . $imageString . "\"";
488
 
489
- //List the categories this product should be listed in
490
- $cats = $product->getCategoryIds();
491
- $category_id_list = "";
492
- $category_name_list = "";
493
- $category_url_list = "";
494
 
495
- foreach ($cats as $category_id)
496
- {
497
- $category_id_list .= $categories[$category_id]['ids'].';';
498
- $category_name_list .= $categories[$category_id]['name'].';';
499
- $category_url_list .= $categories[$category_id]['url'].';';
500
  }
501
-
502
- $category_id_list = substr($category_id_list, 0, -1);
503
- $category_name_list = substr($category_name_list, 0, -1);
504
- $category_url_list = substr($category_url_list, 0, -1);
505
-
506
- echo ",\"" . $category_id_list . "\"";
507
- echo ",\"" . $category_name_list . "\"";
508
- echo ",\"" . $category_url_list . "\"";
509
  }
510
- echo "\n";
511
  }
512
- if ($_GET['debug'] == '1')
513
- { //Show performance data
514
- echo "\n\n\n" . Varien_Profiler::getSqlProfiler($conn);
 
515
  }
516
  }
517
 
14
  public function indexAction()
15
  {
16
 
17
+ echo "Poq Studio";
 
 
 
 
 
 
 
18
 
19
  // Show settings
20
  //Set CSV content type
24
  $settings = Mage::helper('poq_integration')->getSettings();
25
  var_dump($settings);
26
  }
 
 
 
27
  }
28
 
 
29
  /**
30
  * Gets all categories indexed by id
31
  * @return array
32
  */
 
33
  private function getCategories()
34
  {
35
  // Init category name and url arrays indexed by id
94
  $url .= $activeCategoryUrlsById[$parent] . '>';
95
  }
96
 
97
+ // Get products in category with their positions
98
+ $currentCategory = Mage::getModel('catalog/category')->load($categoryId);
99
+ $collection = $currentCategory->getProductCollection()->addAttributeToSort('position');
100
+ Mage::getModel('catalog/layer')->prepareProductCollection($collection);
101
+
102
  // Index category data
103
  $categories[$categoryId] = array(
104
  'name' => substr($name, 0, -1),
105
  'level' => $category['level'],
106
  'ids' => str_replace('/', '>', $path),
107
  'url' => substr($url, 0, -1),
108
+ 'id' => $categoryId,
109
+ 'products' => $currentCategory->getProductsPosition()
110
  );
111
  }
112
  }
114
  return $categories;
115
  }
116
 
117
+ public function showcategoriesAction()
118
+ {
119
+ var_dump($this->getCategories());
120
+ }
121
+
122
  /**
123
  * Create csv feed file with respecto extension settings
124
  */
126
  {
127
 
128
  // Get extension settings
 
129
 
130
+ try
131
+ {
 
 
 
132
 
133
+ $settings = Mage::helper('poq_integration')->getSettings();
134
+ }
135
+ catch (Exception $e)
136
  {
137
+ echo "An error occured while getting integration settings.";
138
  }
139
 
140
+
141
+ // Check null values for the first time extension installed
142
+ if (is_null($settings->store_id))
143
  {
144
+ $settings->store_id = Mage::app()->getStore()->getStoreId();
145
  }
146
+ else if (empty($settings->store_id))
147
  {
148
+
149
+ $settings->store_id = Mage::app()->getStore()->getStoreId();
150
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
151
 
152
+ if (is_null($settings->image_base_url))
153
  {
154
+ $settings->image_base_url = "";
 
 
 
155
  }
156
 
157
+ if (is_null($settings->image_ignore_string))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
158
  {
159
+ $settings->image_ignore_string = "";
 
 
 
 
 
 
160
  }
161
+
162
+ try
163
  {
164
+
165
+
166
+
167
+ $store_id = $settings->store_id;
168
+ $image_base_url = $settings->image_base_url;
169
+ $image_ignore_strings = $settings->image_ignore_string;
170
+ $tax_rates_enabled = $settings->tax_enabled;
171
+
172
+ //Set CSV content type
173
+ if ($_GET['debug'] == '1')
174
  {
175
+ header("Content-type: text/plain");
176
  }
177
+ else
178
+ {
179
+ header("Content-type: text/csv");
180
+ header("Content-Disposition: attachment; filename=feed.csv");
181
+ }
182
+ header("Pragma: no-cache");
183
+ header("Expires: 0");
184
+
185
+
186
+ // Init product attribute arrays
187
+ $productSizeDescriptions = array();
188
+ $parentProductIds = array();
189
+ $productColors = array();
190
+
191
+
192
+ // Attribute label configurations
193
+ // These can be moved to integration settings
194
+ // They always be lowercase for code convention
195
+ $colorAttributeLabel = "color";
196
+ $sizeAttributeLabel = "size";
197
+
198
+ $conn = Mage::getSingleton('core/resource')->getConnection('core_setup');
199
+ $collection = Mage::getModel('catalog/product')->getCollection()
200
+ ->joinField(
201
+ 'qty', 'cataloginventory/stock_item', 'qty', 'product_id=entity_id', '{{table}}.stock_id=1', 'left'
202
+ )
203
+ ->addAttributeToFilter('status', 1) // enabled
204
+ ->addUrlRewrite()
205
+ ->addPriceData()
206
+ ->addStoreFilter($store_id)
207
+ ->addAttributeToSelect('*');
208
+ Mage::getSingleton('catalog/product_status')->addSaleableFilterToCollection($collection);
209
+ Mage::getSingleton('cataloginventory/stock')->addInStockFilterToCollection($collection);
210
+ $collection->setOrder('sku', 'desc');
211
+
212
+ if ($_GET['debug'] == '1')
213
  {
214
+ //only get the first hundred rows, useful for faster testing. Also enable profiler, so we can see performance data.
215
+ $collection->setPageSize(200)->setCurPage(1);
216
+ $conn->getProfiler()->setEnabled(true);
217
+ echo $collection->getSelect()->__toString() . "\n\n";
218
+ }
219
 
220
+ //Read the dataset into memory
221
+ $collection->load();
222
+
223
+ //Preload the media_gallery image data into memory
224
+ //For use with Option #1 below
225
+ $_mediaGalleryAttributeId = Mage::getSingleton('eav/config')->getAttribute('catalog_product', 'media_gallery')->getAttributeId();
226
+ $_read = Mage::getSingleton('core/resource')->getConnection('catalog_read');
227
+
228
+ // Get media gallery table name
229
+ $mediaGalleryTableName = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_media_gallery');
230
+ $mediaGalleryValueTableName = Mage::getSingleton('core/resource')->getTableName('catalog_product_entity_media_gallery_value');
231
+
232
+ if ($_GET['debug'] == '1')
233
+ {
234
+ var_dump($mediaGalleryTableName);
235
+ var_dump($mediaGalleryValueTableName);
236
+ }
237
+ $_mediaGalleryData = $_read->fetchAll('SELECT
238
+ main.entity_id, `main`.`value_id`, `main`.`value` AS `file`,
239
+ `value`.`label`, `value`.`position`, `value`.`disabled`, `default_value`.`label` AS `label_default`,
240
+ `default_value`.`position` AS `position_default`,
241
+ `default_value`.`disabled` AS `disabled_default`
242
+ FROM ' . $mediaGalleryTableName . ' AS `main`
243
+ LEFT JOIN ' . $mediaGalleryValueTableName . ' AS `value`
244
+ ON main.value_id=value.value_id AND value.store_id=' . $store_id . '
245
+ LEFT JOIN ' . $mediaGalleryValueTableName . ' AS `default_value`
246
+ ON main.value_id=default_value.value_id AND default_value.store_id=0
247
+ WHERE (
248
+ main.attribute_id = ' . $_read->quote($_mediaGalleryAttributeId) . ')
249
+ AND (main.entity_id IN (' . $_read->quote($collection->getAllIds()) . '))
250
+ ORDER BY IF(value.position IS NULL, default_value.position, value.position) ASC
251
+ ');
252
+ $_mediaGalleryByProductId = array();
253
+ foreach ($_mediaGalleryData as $_galleryImage)
254
+ {
255
+ $k = $_galleryImage['entity_id'];
256
+ unset($_galleryImage['entity_id']);
257
+ if (!isset($_mediaGalleryByProductId[$k]))
258
  {
259
+ $_mediaGalleryByProductId[$k] = array();
260
  }
261
+ $_mediaGalleryByProductId[$k][] = $_galleryImage;
262
+ }
263
+ unset($_mediaGalleryData);
264
+ foreach ($collection as &$_product)
265
+ {
266
+ $_productId = $_product->getData('entity_id');
267
+ if (isset($_mediaGalleryByProductId[$_productId]))
268
+ {
269
+ $_product->setData('media_gallery', array('images' => $_mediaGalleryByProductId[$_productId]));
270
+ }
271
+ }
272
+ unset($_mediaGalleryByProductId);
273
 
 
 
 
 
 
274
 
275
+ // End of media_gallery queries for Option #1
276
+ //Go through configurable products, and preload data into $parentProductIds and $productSizeDescriptions
277
+ foreach ($collection as $product)
278
+ {
279
+ if ($product->getTypeId() == "configurable")
280
  {
281
 
282
+ //Get list of related sub-products
283
+ $productids = $product->getTypeInstance()->getUsedProductIds();
284
+ foreach ($productids as $productid)
285
+ {
286
+ $parentProductIds[$productid] = $product->getId(); //Add to array of related products, so we can look up when we loop through the main products later.
287
+ }
288
 
289
+ // Collect options applicable to the configurable product
290
+ $conf = Mage::getModel('catalog/product_type_configurable')->setProduct($product);
291
+
292
+ // Get all attributes of the super products
293
+ $productAttributeOptions = $product->getTypeInstance(true)->getConfigurableAttributesAsArray($product);
294
+
295
+ // Group attributes of children products
296
+ foreach ($productAttributeOptions as $productAttribute)
297
  {
298
+
299
+ $code = $productAttribute["attribute_code"];
300
+
301
+ // Find attribute contains "color"
302
+ if (strpos($code, $colorAttributeLabel) !== false)
303
  {
304
+ // Get children products used attribute code
305
+ $col = $conf->getUsedProductCollection()->addAttributeToSelect($code)->addFilterByRequiredOptions();
306
+ foreach ($col as $simple_product)
307
+ {
308
+ $productColors[$simple_product->getId()] = $simple_product->getAttributeText($code);
309
+ }
310
  }
 
311
 
312
 
313
+ // Find attribute contains "size"
314
+ if (strpos($code, $sizeAttributeLabel) !== false)
 
 
 
 
315
  {
316
+ // Get children products used attribute code
317
+ $col = $conf->getUsedProductCollection()->addAttributeToSelect($code)->addFilterByRequiredOptions();
318
+ foreach ($col as $simple_product)
319
+ {
320
+ $productSizeDescriptions[$simple_product->getId()] = $simple_product->getAttributeText($code);
321
+ }
322
  }
323
  }
324
  }
325
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
326
 
327
+ // Get all active categories
328
+ $categories = $this->getCategories();
329
+
330
+
331
+
332
+ //Add one line of headers above the actual content.
333
+ echo "\"id\"";
334
+ echo ",\"name\"";
335
+ echo ",\"description\"";
336
+ echo ",\"price\"";
337
+ echo ",\"specialprice\"";
338
+ echo ",\"parentproductid\"";
339
+ echo ",\"sku\"";
340
+ echo ",\"isinstock\"";
341
+ echo ",\"quantity\"";
342
+ echo ",\"size\"";
343
+ echo ",\"color\"";
344
+ echo ",\"colorGroupId\"";
345
+ echo ",\"productURL\"";
346
+ echo ",\"pictureurls\"";
347
+ echo ",\"categoryid\"";
348
+ echo ",\"categoryname\"";
349
+ echo ",\"categoryurl\"";
350
+ echo ",\"categorysortindex\"";
351
+ echo "\n";
352
 
353
+ // Get tax rates if enabled
354
+ $tax_classes;
 
355
 
356
+ if ($tax_rates_enabled)
357
+ {
 
 
 
358
 
359
+ // Get all tax classes with their values
360
+ $tax_classes = Mage::helper('tax')->getAllRatesByProductClass();
361
+ }
362
 
363
+ //Loop through the products, get the values and write them in CSV format
364
+ foreach ($collection as $product)
 
 
365
  {
366
 
367
+ $description = strip_tags($product->getDescription()); //Remove HTML tags from the description
368
+ $description = addslashes($description); //Escape quotes etc from the text
369
+ $description = preg_replace('#\s{2,}#', '\\n', $description); //Remove line breaks, "\\n" will be put back in as line breaks when importing.
370
+ //$description = preg_replace('/(?<!,)"(?!,)/', '""', $description); //double up any double quote that is not immediately preceded or followed by a comma
371
+ $description = preg_replace('/\"/', '""', $description); //double up any double quote
372
 
373
+ echo "\"" . $product->getId() . "\"";
374
+ echo ",\"" . addslashes($product->getName()) . "\"";
375
+ echo ",\"" . $description . "\"";
376
+
377
+ // Original price might have been tax rates applied on live site
378
+ // Fix by macromania
379
+ //echo ",\"" . $product->getPrice() . "\""; )
380
+ try
381
  {
382
 
383
+ // Original price without tax
384
+ $product_price = $product->getPrice();
385
+ $special_price = $product->getFinalPrice();
386
+
387
+ if ($tax_rates_enabled)
388
+ {
389
 
390
+ // Get product tax class id
391
+ $tax_class_id = $_product->getData('tax_class_id');
 
 
 
392
 
393
+ // $tax_classes is returned as string. So parsing is needed to get the value
394
+ // an example $tax_classes could be like {"value_2":20,"value_4":20,"value_5":20,"value_6":0,"value_7":5}
395
+ $tax_classes = str_replace('{', '', $tax_classes);
396
+ $tax_classes = str_replace('}', '', $tax_classes);
397
+ $tax_class_value_array = explode(',', $tax_classes);
398
 
399
+ // Tax value
400
+ $product_tax_value = 0;
401
 
402
+ foreach ($tax_class_value_array as $tax_class)
 
403
  {
404
+
405
+ $values = explode(':', $tax_class);
406
+ if ($values[0] == '"value_' . $tax_class_id . '"')
407
+ {
408
+ // Get the rate
409
+ $product_tax_value = $values[1];
410
+ }
411
+ }
412
+
413
+ // Apply tax rate
414
+ if ($product_tax_value > 0)
415
+ {
416
+ $product_price += $product_price * $product_tax_value / 100;
417
+ $special_price += $special_price * $product_tax_value / 100;
418
  }
 
419
 
420
+ echo ",\"" . ceil($product_price) . "\"";
421
+ echo ",\"" . ceil($special_price) . "\"";
422
+ }
423
+ else
424
  {
425
+ echo ",\"" . $product_price . "\"";
426
+ echo ",\"" . $special_price . "\"";
427
  }
428
 
429
+
430
+
431
+ //echo '<p style="color:red">------<br/>Caught exception: ', ceil($product_price),'----',$product->getPrice(), '<br/>-----</p>';
432
  }
433
+ catch (Exception $eproductprice)
434
  {
435
+ echo '<p style="color:red">------<br/>Caught exception: ', $e2->getMessage(), '<br/>-----</p>';
 
436
  }
437
 
438
 
439
+ // Parent product id
440
+ $parentProductIdCombined = $parentProductIds[$product->getId()];
441
+ if ($productColors[$product->getId()])
 
 
 
 
 
 
 
 
 
 
 
442
  {
443
+ if (!empty($productColors[$product->getId()]))
444
+ {
445
+ $parentProductIdCombined .= "-" . $productColors[$product->getId()];
446
+ }
447
  }
 
448
 
449
 
450
+ echo ",\"" . $parentProductIdCombined . "\"";
451
 
452
+ // SKU
453
+ echo ",\"" . $product->getSku() . "\"";
454
 
455
+ // Stock
456
+ echo ",\"" . $product->getStockItem()->getIsInStock() . "\"";
457
 
458
+ // Stock Quantity
459
+ echo ",\"" . $product->getQty() . "\"";
460
 
461
+ // Size
462
+ echo ",\"" . $productSizeDescriptions[$product->getId()] . "\"";
463
 
464
+ // Color name
465
+ echo ",\"" . $productColors[$product->getId()] . "\"";
466
 
467
+ // Color group id
468
+ echo ",\"" . $parentProductIds[$product->getId()] . "\"";
469
 
 
 
 
 
 
 
470
 
471
+ if ($product->getVisibility() == 4)
472
+ { //Only products that are individually visible need URLs, pictures, categories.
473
+ //Get product URL
474
+ echo ",\"" . $product->getProductUrl() . "\"";
475
 
476
+ //Get main image
477
+ $imageString = $product->getMediaConfig()->getMediaUrl($product->getData('image'));
478
 
479
+ //attempt to audo-detect base image url, if necessary
480
+ if ($image_base_url == '')
 
 
 
 
 
 
 
 
 
481
  {
482
+ $image_base_url = substr($imageString, 0, strrpos($imageString, 'media/catalog/product')) . 'media/catalog/product';
483
+ }
484
 
485
+ //Option #1 - Fast way to get all media gallery images from preloaded array
486
+ $_images = $product->getData('media_gallery');
487
+ foreach ($_images as $imagegallery)
488
+ {
489
+ foreach ($imagegallery as $add_image)
490
  {
491
+
492
+ //Check if image should be filtered.
493
+ $image_should_be_added = true;
494
+ if ($image_ignore_strings != '')
495
  {
496
+ //echo "\n image_ignore_strings: " . $image_ignore_strings;
497
+ $image_ignore_string_array = explode(",", $image_ignore_strings);
498
+ foreach ($image_ignore_string_array as $image_ignore_string)
499
  {
500
+ //echo "\n image_ignore_string: " . $image_ignore_string;
501
+ if (strpos($add_image['file'], $image_ignore_string) !== false)
502
+ {
503
+ //echo "\n IGNORING: " . $add_image['file'];
504
+ $image_should_be_added = false;
505
+ }
506
  }
507
  }
508
+ if ($image_should_be_added)
509
+ {
510
+ $imageString .= ',' . $image_base_url . $add_image['file'];
511
+ }
512
  }
513
+ }
514
+
515
+ //Option #2 - Slower, but more reliable way to get all images. Try this is option #1 does not run
516
+ // $_images = Mage::getModel('catalog/product')->load($product->getId())->getMediaGalleryImages();
517
+ // foreach ($_images as $imagegallery) {
518
+ // $imageString .= ';' . $imagegallery['url'];
519
+ // }
520
+ echo ",\"" . $imageString . "\"";
521
+
522
+
523
+
524
+ //List the categories this product should be listed in
525
+ $cats = $product->getCategoryIds();
526
+ $category_id_list = "";
527
+ $category_name_list = "";
528
+ $category_url_list = "";
529
+ $category_sort_list = "";
530
+
531
+ foreach ($cats as $category_id)
532
+ {
533
+ $category_id_list .= $categories[$category_id]['ids'] . ';';
534
+ $category_name_list .= $categories[$category_id]['name'] . ';';
535
+ $category_url_list .= $categories[$category_id]['url'] . ';';
536
+
537
+ // Check if this category has products
538
+
539
+ if (count($categories[$category_id]['products']) > 0)
540
  {
541
+ $category_sort_list .= $categories[$category_id]['products'][$product->getId()] . ';';
542
  }
543
  }
 
544
 
545
+ $category_id_list = substr($category_id_list, 0, -1);
546
+ $category_name_list = substr($category_name_list, 0, -1);
547
+ $category_url_list = substr($category_url_list, 0, -1);
548
+ $category_sort_list = substr($category_sort_list, 0, -1);
 
 
549
 
 
 
 
 
 
550
 
551
+ echo ",\"" . $category_id_list . "\"";
552
+ echo ",\"" . $category_name_list . "\"";
553
+ echo ",\"" . $category_url_list . "\"";
554
+ echo ",\"" . $category_sort_list . "\"";
 
555
  }
556
+ echo "\n";
557
+ }
558
+ if ($_GET['debug'] == '1')
559
+ { //Show performance data
560
+ echo "\n\n\n" . Varien_Profiler::getSqlProfiler($conn);
 
 
 
561
  }
 
562
  }
563
+ catch (Exception $ex)
564
+ {
565
+ var_dump($settings);
566
+ var_dump($ex);
567
  }
568
  }
569
 
app/code/community/Poq/Integration/etc/config.xml CHANGED
@@ -9,7 +9,7 @@
9
  <config>
10
  <modules>
11
  <Poq_Integration>
12
- <version>1.0.0.1.1</version>
13
  </Poq_Integration>
14
  </modules>
15
  <global>
9
  <config>
10
  <modules>
11
  <Poq_Integration>
12
+ <version>2.1.0.0.0</version>
13
  </Poq_Integration>
14
  </modules>
15
  <global>
app/design/frontend/base/default/template/poqintegration/index.phtml DELETED
@@ -1,21 +0,0 @@
1
- <h3>Poq Studio - Integration Settings -
2
- <?php
3
-
4
- echo Mage::getConfig()->getModuleConfig("Poq_Integration")->version;
5
-
6
- ?></h3>
7
-
8
- <p>
9
- <b>Extension is
10
- <?php echo Mage::helper('core')->isModuleEnabled('Poq_Integration') ? 'installed' : 'not installed'; ?>
11
- </b>
12
- </p>
13
- <hr style="margin-bottom: 10px;"/>
14
- <p>
15
- You can change settings on
16
- <b><i>System > Configuration</i></b>
17
- page under <b><i>Poq Studio</i></b>
18
- via admin panel.
19
- </p>
20
- <p><a href="http://www.poqstudio.com" target="_blank">Contact us</a> for detailed guidline and asistance.</p>
21
- <p><b>Poq Studio</b></p>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
app/etc/modules/Poq_Integration.xml DELETED
@@ -1,19 +0,0 @@
1
- <?xml version="1.0" encoding="UTF-8"?>
2
- <!--
3
- /**
4
- * Module initial config
5
- *
6
- * @author Poq Studio
7
- */
8
- -->
9
- <config>
10
- <modules>
11
- <Poq_Integration>
12
- <active>true</active>
13
- <codePool>community</codePool>
14
- <depends>
15
- <Mage_Adminhtml />
16
- </depends>
17
- </Poq_Integration>
18
- </modules>
19
- </config>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
package.xml CHANGED
@@ -1,18 +1,18 @@
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Poq_Integration</name>
4
- <version>2.0.0.0.0</version>
5
  <stability>stable</stability>
6
  <license>ASL</license>
7
  <channel>community</channel>
8
  <extends/>
9
- <summary>This integration forms a connection between the Magento and Poq Studio platforms. </summary>
10
  <description>The extension has two main features: Exposing your catalogue information in the Poq Feed Format and Accepting a shopping cart transfer from the Poq Studio platform.</description>
11
- <notes>Native checkout experience is included in this release.</notes>
12
  <authors><author><name>Oyvind Henriksen</name><user>poqmagento</user><email>info@poqstudio.com</email></author></authors>
13
- <date>2014-07-07</date>
14
- <time>11:12:38</time>
15
- <contents><target name="magecommunity"><dir name="Poq"><dir name="Integration"><dir name="Helper"><file name="Data.php" hash="2b374d55c6cbb107d15f6381d5b5d417"/></dir><dir name="controllers"><file name="IndexController.php" hash="3c44fe7f66b664e0860b2b8aa7e4e7eb"/><file name="OrderController.php" hash="e41bc679854fe870fb74fec4ec25de26"/></dir><dir name="etc"><file name="adminhtml.xml" hash="545b50fa7d354adb75a6bc27ff2ef41e"/><file name="config.xml" hash="af8d41d205fc47793112a57dafc07e0f"/><file name="system.xml" hash="d4e53e147e5c76e6e9977382b31e005b"/></dir><file name=".DS_Store" hash="ff82e8a2dcd7f97dc2e2146502762088"/></dir><file name=".DS_Store" hash="16eb133391c4355b50681fccd76eee48"/></dir></target><target name="mageetc"><dir name="modules"><file name="Poq_Integration.xml" hash="f735b9c01c16c559a05ba50185ef8a61"/></dir></target><target name="magedesign"><dir name="frontend"><dir name="base"><dir name="default"><dir name="template"><dir name="poqintegration"><file name="index.phtml" hash="8c259c3a379932221aae3db248b1c59a"/></dir></dir></dir></dir></dir></target></contents>
16
  <compatible/>
17
  <dependencies><required><php><min>5.1.0</min><max>6.0.0</max></php></required></dependencies>
18
  </package>
1
  <?xml version="1.0"?>
2
  <package>
3
  <name>Poq_Integration</name>
4
+ <version>2.0.0.1.1</version>
5
  <stability>stable</stability>
6
  <license>ASL</license>
7
  <channel>community</channel>
8
  <extends/>
9
+ <summary>This integration forms a connection between the Magento and Poq Studio platforms.</summary>
10
  <description>The extension has two main features: Exposing your catalogue information in the Poq Feed Format and Accepting a shopping cart transfer from the Poq Studio platform.</description>
11
+ <notes>Category sort index support added.</notes>
12
  <authors><author><name>Oyvind Henriksen</name><user>poqmagento</user><email>info@poqstudio.com</email></author></authors>
13
+ <date>2014-09-01</date>
14
+ <time>09:25:48</time>
15
+ <contents><target name="magecommunity"><dir name="Poq"><dir name="Integration"><dir name="Helper"><file name="Data.php" hash="c008f517ee4299385ac8887c9b7852dc"/></dir><dir name="controllers"><file name="IndexController.php" hash="846f034177f524972ad75d32af91d9d0"/><file name="OrderController.php" hash="e41bc679854fe870fb74fec4ec25de26"/></dir><dir name="etc"><file name="adminhtml.xml" hash="545b50fa7d354adb75a6bc27ff2ef41e"/><file name="config.xml" hash="f49058ef8c5cc6326367837c205e4029"/><file name="system.xml" hash="d4e53e147e5c76e6e9977382b31e005b"/></dir><file name=".DS_Store" hash="edbbdc9cbdcde369d585980e7a279eac"/></dir><file name=".DS_Store" hash="16eb133391c4355b50681fccd76eee48"/></dir></target><target name="mageetc"><dir name="modeules"><file name="Poq_Integration.xml" hash=""/></dir></target></contents>
16
  <compatible/>
17
  <dependencies><required><php><min>5.1.0</min><max>6.0.0</max></php></required></dependencies>
18
  </package>