Skip to content

Instantly share code, notes, and snippets.

@scgoeswild
Last active November 21, 2020 11:08
Show Gist options
  • Save scgoeswild/272dce1dbb5d620ee8b922408fcc6a82 to your computer and use it in GitHub Desktop.
Save scgoeswild/272dce1dbb5d620ee8b922408fcc6a82 to your computer and use it in GitHub Desktop.
Magento 1.9 export products by dataflow profile
<?php
#copy this file to main root of magento and run it using terminal ssh with this command
# php exportProducts.php
#$profileId = 1; #All Products
$profileId = 49; #Edit this ID to specify the profile you want to use
require_once 'app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$profile = Mage::getModel('dataflow/profile');
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
Mage::getSingleton('admin/session')->setUser($userModel);
$profile->load($profileId);
if (!$profile->getId()) {
Mage::getSingleton('adminhtml/session')->addError('[ERROR] Invalid ID');
}
Mage::register('current_convert_profile', $profile);
$profile->run();
$recordCount = 0;
?>
<?php #to export category name instead of category Ids, you should unparse the file app/code/core/Mage/Catalog/Model/Convert/Parser/Product.php by copy and replace this lines (around at 402 line)
/**
* Unparse (prepare data) loaded products
*
* @return Mage_Catalog_Model_Convert_Parser_Product
*/
public function unparse()
{
$entityIds = $this->getData();
foreach ($entityIds as $i => $entityId) {
$product = $this->getProductModel()
->setStoreId($this->getStoreId())
->load($entityId);
$this->setProductTypeInstance($product);
/* @var $product Mage_Catalog_Model_Product */
$position = Mage::helper('catalog')->__('Line %d, SKU: %s', ($i + 1), $product->getSku());
$this->setPosition($position);
/** @var array $categoryNames Retrive category name */
$categoryNames = $product->getCategoryCollection()
->addAttributeToSelect('name')
->getColumnValues('name');
$row = array(
'store' => $this->getStore()->getCode(),
'websites' => '',
'attribute_set' => $this->getAttributeSetName($product->getEntityTypeId(),
$product->getAttributeSetId()),
'type' => $product->getTypeId(),
'category_names' => join(',', $categoryNames)
);
if ($this->getStore()->getCode() == Mage_Core_Model_Store::ADMIN_CODE) {
$websiteCodes = array();
foreach ($product->getWebsiteIds() as $websiteId) {
$websiteCode = Mage::app()->getWebsite($websiteId)->getCode();
$websiteCodes[$websiteCode] = $websiteCode;
}
$row['websites'] = join(',', $websiteCodes);
} else {
$row['websites'] = $this->getStore()->getWebsite()->getCode();
if ($this->getVar('url_field')) {
$row['url'] = $product->getProductUrl(false);
}
}
foreach ($product->getData() as $field => $value) {
if (in_array($field, $this->_systemFields) || is_object($value)) {
continue;
}
$attribute = $this->getAttribute($field);
if (!$attribute) {
continue;
}
if ($attribute->usesSource()) {
$option = $attribute->getSource()->getOptionText($value);
if ($value && empty($option) && $option != '0') {
$this->addException(
Mage::helper('catalog')->__('Invalid option ID specified for %s (%s), skipping the record.',
$field, $value),
Mage_Dataflow_Model_Convert_Exception::ERROR
);
continue;
}
if (is_array($option)) {
$value = join(self::MULTI_DELIMITER, $option);
} else {
$value = $option;
}
unset($option);
} elseif (is_array($value)) {
continue;
}
$row[$field] = $value;
}
if ($stockItem = $product->getStockItem()) {
foreach ($stockItem->getData() as $field => $value) {
if (in_array($field, $this->_systemFields) || is_object($value)) {
continue;
}
$row[$field] = $value;
}
}
$productMediaGallery = $product->getMediaGallery();
$product->reset();
$processedImageList = array();
foreach ($this->_imageFields as $field) {
if (isset($row[$field])) {
if ($row[$field] == 'no_selection') {
$row[$field] = null;
} else {
$processedImageList[] = $row[$field];
}
}
}
$processedImageList = array_unique($processedImageList);
$batchModelId = $this->getBatchModel()->getId();
$this->getBatchExportModel()
->setId(null)
->setBatchId($batchModelId)
->setBatchData($row)
->setStatus(1)
->save();
$baseRowData = array(
'store' => $row['store'],
'website' => $row['website'],
'sku' => $row['sku']
);
unset($row);
foreach ($productMediaGallery['images'] as $image) {
if (in_array($image['file'], $processedImageList)) {
continue;
}
$rowMediaGallery = array(
'_media_image' => $image['file'],
'_media_lable' => $image['label'],
'_media_position' => $image['position'],
'_media_is_disabled' => $image['disabled']
);
$rowMediaGallery = array_merge($baseRowData, $rowMediaGallery);
$this->getBatchExportModel()
->setId(null)
->setBatchId($batchModelId)
->setBatchData($rowMediaGallery)
->setStatus(1)
->save();
}
}
return $this;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment