Skip to content

Instantly share code, notes, and snippets.

@tegansnyder
Last active November 22, 2018 15:31
Show Gist options
  • Save tegansnyder/8529606 to your computer and use it in GitHub Desktop.
Save tegansnyder/8529606 to your computer and use it in GitHub Desktop.
Really rough around the edge way (not complete) way to grab all product attributes to a csv with direct SQL in Magento. Don't trust this worth your life :)
<?php
/**
* export-all-product-attributes.php
* @author Tegan Snyder <tsnyder@tegdesign.com>
*/
// init Magento
require_once('app/Mage.php');
umask(0);
Mage::app('default');
// setup database connection
$db_conn = Mage::getSingleton('core/resource');
$r_conn = $db_conn->getConnection('core_read');
// grab all attribute_codes
$sql = "SELECT DISTINCT attribute_code FROM eav_attribute WHERE entity_type_id IN (SELECT entity_type_id FROM eav_entity_type WHERE entity_type_code = 'catalog_product')";
$results = $r_conn->fetchAll($sql);
$attribute_codes = array();
foreach ($results as $row) {
$attribute_codes[$row['attribute_code']] = '';
}
$attribute_codes = array_keys($attribute_codes);
// grab a list of your skus that are SIMPLE products...
$sql = "SELECT sku FROM catalog_product_entity WHERE type_id = 'simple'";
$results = $r_conn->fetchAll($sql);
// limit the products to a specific store
$store_id = 0;
// counter for header
$x = 0;
$y = 0;
foreach ($results as $row) {
// grab product attributes for sku into array
$product_attributes = array();
$product_attributes = getAttributes($store_id, $row['sku'], $attribute_codes);
if (isset($product_attributes['skipped'])) {
continue;
}
// loop through attributes array
foreach ($product_attributes as $k => $n) {
// print headers
if ($x == 0) {
echo '"sku","' . implode('","', array_keys($n)) . '"' . PHP_EOL;
}
if (count(array_filter($n)) == 0) {
continue;
}
// output in comma enclosed csv format
echo '"' . $row['sku'] . '","' . implode('","', $n) . '"' . PHP_EOL;
// increment counter
$x = $x + 1;
}
$y = $y + 1;
}
/**
* Get product attributes via direct SQL
*/
function getAttributes($store_id, $sku, $attribute_codes) {
global $db_conn, $r_conn;
$sql = <<<"SQL"
SELECT * FROM ( SELECT
ce.sku,
ea.attribute_id,
ea.attribute_code,
CASE ea.backend_type
WHEN 'varchar' THEN ce_varchar.value
WHEN 'int' THEN ce_int.value
WHEN 'text' THEN ce_text.value
WHEN 'decimal' THEN ce_decimal.value
WHEN 'datetime' THEN ce_datetime.value
ELSE ea.backend_type
END AS value,
CASE ea.backend_type
WHEN 'varchar' THEN ce_varchar.store_id
WHEN 'int' THEN ce_int.store_id
WHEN 'text' THEN ce_text.store_id
WHEN 'decimal' THEN ce_decimal.store_id
WHEN 'datetime' THEN ce_datetime.store_id
ELSE ea.backend_type
END AS store_id,
ea.is_required AS required
FROM catalog_product_entity AS ce
LEFT JOIN eav_attribute AS ea
ON ce.entity_type_id = ea.entity_type_id
LEFT JOIN catalog_product_entity_varchar AS ce_varchar
ON ce.entity_id = ce_varchar.entity_id
AND ea.attribute_id = ce_varchar.attribute_id
AND ea.backend_type = 'varchar'
LEFT JOIN catalog_product_entity_int AS ce_int
ON ce.entity_id = ce_int.entity_id
AND ea.attribute_id = ce_int.attribute_id
AND ea.backend_type = 'int'
LEFT JOIN catalog_product_entity_text AS ce_text
ON ce.entity_id = ce_text.entity_id
AND ea.attribute_id = ce_text.attribute_id
AND ea.backend_type = 'text'
LEFT JOIN catalog_product_entity_decimal AS ce_decimal
ON ce.entity_id = ce_decimal.entity_id
AND ea.attribute_id = ce_decimal.attribute_id
AND ea.backend_type = 'decimal'
LEFT JOIN catalog_product_entity_datetime AS ce_datetime
ON ce.entity_id = ce_datetime.entity_id
AND ea.attribute_id = ce_datetime.attribute_id
AND ea.backend_type = 'datetime'
WHERE ce.sku = "$sku") AS tab WHERE tab.value != ""
SQL;
$results = $r_conn->fetchAll($sql);
$product_attributes = array();
foreach ($results as $row) {
if ($row['store_id'] != $store_id) {
$product_attributes['skipped'] = true;
return $product_attributes;
}
foreach ($attribute_codes as $attr_code) {
if (!isset($product_attributes[$row['store_id']][$attr_code])) {
$product_attributes[$row['store_id']][$attr_code] = '';
}
}
if ($row['value'] == 'static') {
if ($row['attribute_code'] == 'category_ids') {
$sql = 'SELECT entity_id FROM catalog_product_entity WHERE sku = "' . $sku . '"';
$a_val = $r_conn->fetchAll($sql);
if (isset($a_val[0]['entity_id'])) {
$sql = 'SELECT GROUP_CONCAT(category_id) as category_ids FROM catalog_category_product WHERE product_id = ' . $a_val[0]['entity_id'];
$c_val = $r_conn->fetchAll($sql);
$product_attributes[$store_id][$row['attribute_code']] = $c_val[0]['category_ids'];
}
} else {
$sql = 'SELECT entity_id, ' . $row['attribute_code'] . ' FROM catalog_product_entity WHERE sku = "' . $sku . '"';
$a_val = $r_conn->fetchAll($sql);
if (isset($a_val[0][$row['attribute_code']])) {
$product_attributes[$store_id][$row['attribute_code']] = $a_val[0][$row['attribute_code']];
}
}
} else {
$product_attributes[$row['store_id']][$row['attribute_code']] = $row['value'];
}
}
return $product_attributes;
}
@tegansnyder
Copy link
Author

run like this:

php export-all-product-attributes.php > output-file.csv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment