自定义产品导出 PHP 脚本不工作
Custom Products Export PHP Script Not working
我需要导出 csv,例如 SKU |类别编号 | Category Name,导出后SKU字段为空剩余有值。
代码:https://codeshare.io/5Q690Q
如何查找错误?
试试这个代码。
<?php
$mageFilename = 'app/Mage.php';
if (!file_exists($mageFilename)) {
echo $mageFilename." was not found";
exit;
}
require_once $mageFilename;
Mage::app();
try {
function getCategoryData()
{
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
$categories = array();
$rootCategoryId = 2;
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;
$string.= $categories[$pathId]['name'] . '/';
}
$categoryFullPaths[$id] = rtrim($string,"/");
}
}
return $categoryFullPaths;
}
function get_values_for_keys($mapping, $keys)
{
$output_arr = array();
foreach($keys as $key)
{
$output_arr[] = $mapping[$key];
}
return $output_arr;
}
$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToSelect('category_ids');
$products->addAttributeToSelect('sku');
$fp = fopen('var/export/exports.csv', 'w');
$csvHeader = array("sku", "category_ids","category_name");
fputcsv( $fp, $csvHeader,",");
$cat_name = array();
$cat_array = array();
$categoryFullPaths = getCategoryData();
foreach ($products as $product)
{
$sku = $product->getSku();
$categoryIds = implode('/', $product->getCategoryIds());//change the category separator
$_cate_name = join(',', get_values_for_keys($categoryFullPaths, $product->getCategoryIds()));
fputcsv($fp, array($sku, $categoryIds,$_cate_name), ",");
}
fclose($fp);
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
我需要导出 csv,例如 SKU |类别编号 | Category Name,导出后SKU字段为空剩余有值。
代码:https://codeshare.io/5Q690Q
如何查找错误?
试试这个代码。
<?php
$mageFilename = 'app/Mage.php';
if (!file_exists($mageFilename)) {
echo $mageFilename." was not found";
exit;
}
require_once $mageFilename;
Mage::app();
try {
function getCategoryData()
{
$category = Mage::getModel('catalog/category');
$tree = $category->getTreeModel();
$tree->load();
$ids = $tree->getCollection()->getAllIds();
$categories = array();
$rootCategoryId = 2;
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;
$string.= $categories[$pathId]['name'] . '/';
}
$categoryFullPaths[$id] = rtrim($string,"/");
}
}
return $categoryFullPaths;
}
function get_values_for_keys($mapping, $keys)
{
$output_arr = array();
foreach($keys as $key)
{
$output_arr[] = $mapping[$key];
}
return $output_arr;
}
$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToSelect('category_ids');
$products->addAttributeToSelect('sku');
$fp = fopen('var/export/exports.csv', 'w');
$csvHeader = array("sku", "category_ids","category_name");
fputcsv( $fp, $csvHeader,",");
$cat_name = array();
$cat_array = array();
$categoryFullPaths = getCategoryData();
foreach ($products as $product)
{
$sku = $product->getSku();
$categoryIds = implode('/', $product->getCategoryIds());//change the category separator
$_cate_name = join(',', get_values_for_keys($categoryFullPaths, $product->getCategoryIds()));
fputcsv($fp, array($sku, $categoryIds,$_cate_name), ",");
}
fclose($fp);
} catch (Exception $e) {
echo $e->getMessage();
exit;
}