forked from gaiterjones/magento-mass-import-export
-
Notifications
You must be signed in to change notification settings - Fork 0
/
exportAllPricesCSV.php
127 lines (104 loc) · 3.71 KB
/
exportAllPricesCSV.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
// Magento export all prices to csv
//
//
// get Magento
require_once '../app/Mage.php';
umask(0);
Mage::app();
Mage::app()->loadArea(Mage_Core_Model_App_Area::AREA_FRONTEND);
header("Content-type:text/octect-stream");
header("Content-Disposition:attachment;filename=exportPrices.csv");
//header('Content-Type: text/html; charset=utf-8');
// load collection
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel('catalog/product');
$products = $product->getCollection()->addStoreFilter($storeId)->getAllIds();
// configure ean prefix
$eanPrefixCode="4037493";
echo '"SKU","Name","GC","Unit Price","Tier1 Qty","Tier1 Price","Tier2 Qty","Tier2 Price","Tier3 Qty","Tier3 Price","Tier4 Qty","Tier4 Price","Tier5 Qty","Tier5 Price","EAN"'. "\n";
// loop through all products
foreach($products as $productid)
{
// load product data
$product = Mage::getModel('catalog/product')->load($productid);
$existingTierPrice = $product->tier_price;
// ignore grouped products they are a container for child simple products and have no price
if($product->getTypeId() == "grouped") {continue;}
$groupedChild=' ';
// find invisible child simple products belonging to grouped product
if($product->getTypeId() == "simple")
{
if ($product->getVisibility()!= "4")
{
if (Mage::getVersion() >= 1.4)
{
// Magento v1.42 +
$parentIdArray = Mage::getModel('catalog/product_type_grouped')
->getParentIdsByChild( $product->getId() );
} else {
// pre 1.42
$parentIdArray = $product->loadParentProductIds()->getData('parent_product_ids');
}
if (!empty($parentIdArray[0]))
{
// use parent product if parent is grouped otherwise move on, these are not the products you are looking for...
$groupedProduct = Mage::getModel('catalog/product')->load($parentIdArray[0]);
if($groupedProduct->getTypeId() == "grouped") {
$groupedChild='X';
} else {
continue;
}
}
}
}
$sku = $product->getSku();
$name = $product->getName();
$unitPrice = round($product->getPrice(),2);
$output='"'. $sku. '","'. $name. '","'. $groupedChild. '","'. $unitPrice. '",';
// get tier prices
if ($existingTierPrice)
{
$numItems = count($existingTierPrice);
$i = 0;
foreach($existingTierPrice as $key=>$value)
{
if($i+1 == $numItems) // check for last item in array
{
// last item in array
$output=$output. '"'. round($value['price_qty'],1). '","'. round($value['price'],2). '",';
for ($y = $i+2; $y <= 5; $y++) {
$output=$output. '" "," ",';
}
} else {
$output=$output. '"'. round($value['price_qty'],1). '","'. round($value['price'],2). '",';
}
$i++;
}
} else {
for ($y = 1; $y <= 5; $y++) {
$output=$output. '" "," ",';
}
}
// ean
$ean=ean13_check_digit($eanPrefixCode.str_pad($product->getId(), 5, "0", STR_PAD_LEFT));
$output=$output. '"'. $ean. '"';
echo $output. "\n";
}
function ean13_check_digit($digits){
//first change digits to a string so that we can access individual numbers
$digits =(string)$digits;
// 1. Add the values of the digits in the even-numbered positions: 2, 4, 6, etc.
$even_sum = $digits{1} + $digits{3} + $digits{5} + $digits{7} + $digits{9} + $digits{11};
// 2. Multiply this result by 3.
$even_sum_three = $even_sum * 3;
// 3. Add the values of the digits in the odd-numbered positions: 1, 3, 5, etc.
$odd_sum = $digits{0} + $digits{2} + $digits{4} + $digits{6} + $digits{8} + $digits{10};
// 4. Sum the results of steps 2 and 3.
$total_sum = $even_sum_three + $odd_sum;
// 5. The check character is the smallest number which, when added to the result in step 4, produces a multiple of 10.
$next_ten = (ceil($total_sum/10))*10;
$check_digit = $next_ten - $total_sum;
return $digits . $check_digit;
}
?>