Question

I need to get my all products category name, actually dataflow profiles return category id only, how can i get category name not category id.

enter image description here

Était-ce utile?

La solution

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;
}

Autres conseils

There are 2 options.

  1. Rewrite Mage_Catalog_Model_Convert_Parser_Product and update unparse() as below.
  2. Create new parser by extending Mage_Catalog_Model_Convert_Parser_Product and update unparse() 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;
    }
Licencié sous: CC-BY-SA avec attribution
Non affilié à magento.stackexchange
scroll top