How can i export fields like category name not category id via Dateflow-profiles export
-
08-10-2020 - |
解決
In addition to B G Kavinga's answer, to get full category path like books/cheldreans/Tamil Books
use below code.
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());
$categoryFullPaths = $this->getCategoryData();
$this->setPosition($position);
$row = array(
'store' => $this->getStore()->getCode(),
'websites' => '',
'attribute_set' => $this->getAttributeSetName($product->getEntityTypeId(),
$product->getAttributeSetId()),
'type' => $product->getTypeId(),
'category_ids' => join(',', $product->getCategoryIds()),
'category_names' => join(',', $this->get_values_for_keys($categoryFullPaths, $product->getCategoryIds()))
);
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;
}
}
foreach ($this->_imageFields as $field) {
if (isset($row[$field]) && $row[$field] == 'no_selection') {
$row[$field] = null;
}
}
$batchExport = $this->getBatchExportModel()
->setId(null)
->setBatchId($this->getBatchModel()->getId())
->setBatchData($row)
->setStatus(1)
->save();
$product->reset();
}
return $this;
}
public function get_values_for_keys($mapping, $keys) {
foreach($keys as $key) {
$output_arr[] = $mapping[$key];
}
return $output_arr;
}
public function getCategoryData(){
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$categoryFullPaths= array();
$ids = $tree->getCollection()->getAllIds();
$categories = array();
$rootCategoryId = Mage::app()->getStore('default')->getRootCategoryId();
if ($ids)
{
foreach ($ids as $id)
{
$category->load($id);
$categories[$id]['name'] = $category->getName();
$categories[$id]['path'] = $category->getPath();
}
foreach ($ids as $id)
{
$path = explode('/', $categories[$id]['path']);
$string = '';
foreach ($path as $pathId)
{
if($pathId == 1 /* || $rootCategoryId == $pathId */) continue; // Uncomment commented code if you want to remove Default Category name.
$string.= $categories[$pathId]['name'] . '/';
}
$categoryFullPaths[$id] = rtrim($string,"/");
}
}
return $categoryFullPaths;
}
他のヒント
There are 2 options.
- Rewrite
Mage_Catalog_Model_Convert_Parser_Product
and updateunparse()
as below. - Create new parser by extending
Mage_Catalog_Model_Convert_Parser_Product
and updateunparse()
as below. Then update action xml to use new parser.
Updated Code
/** @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)
);
Entire Function
/**
* 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;
}
所属していません magento.stackexchange