-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathexportEANCSV.php
52 lines (44 loc) · 1.45 KB
/
exportEANCSV.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
<?php
// Magento export EAN codes
// blog.gaiterjones.com
// 10.10.2012 v0.1
//
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=exportMyEANCodes.csv");
// define ean code prefix - country / manufacturer
$eanPrefixCode="0123456";
// load collection
$storeId = Mage::app()->getStore()->getId();
$product = Mage::getModel('catalog/product');
$products = $product->getCollection()->addStoreFilter($storeId)->getAllIds();
// csv header
echo '"sku","ean"'. "\n";
// loop through all products
foreach($products as $productid)
{
// load product data
$product = Mage::getModel('catalog/product')->load($productid);
// get sku
$sku = $product->getSku();
// generate ean13
$ean=ean13_check_digit($eanPrefixCode. str_pad($product->getId(), 5, "0", STR_PAD_LEFT));
// output csv data
$output='"'. $sku. '","'. $ean. '"';
echo $output. "\n";
}
// function to generate ean13 checksum digit
function ean13_check_digit($digits){
$digits =(string)$digits;
$even_sum = $digits{1} + $digits{3} + $digits{5} + $digits{7} + $digits{9} + $digits{11};
$even_sum_three = $even_sum * 3;
$odd_sum = $digits{0} + $digits{2} + $digits{4} + $digits{6} + $digits{8} + $digits{10};
$total_sum = $even_sum_three + $odd_sum;
$next_ten = (ceil($total_sum/10))*10;
$check_digit = $next_ten - $total_sum;
return $digits . $check_digit;
}
?>