我有另一个Magento问题。 我试图将所有活跃的产品分类到新的类别,这需要花费大量的时间。所以现在我想要导出产品并且更快一点,但是Magento导出功能对我来说并不是真的,因为它在导出到csv时没有在右栏中显示正确的值。
所以我使用这个我在StackOverflow上找到的php脚本,它看起来效果很好。但是我试图从我们创建的2个下拉属性中获取值,但不幸的是,我似乎无法正确使用它。
非常感谢帮助!
<?php
error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
Mage::app();
$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToSelect('category_ids');
$products->addAttributeToFilter('status', 1);//optional for only enabled products
$products->addAttributeToFilter('visibility', 4);//optional for products only visible in catalog and search
$fp = fopen('exports.csv', 'w');
$csvHeader = array("sku", "z_typ", "z_bereich", "category_ids");
fputcsv( $fp, $csvHeader,";");
foreach ($products as $product){
$sku = $product->getSku();
$z_typ = $product->getResource()->getAttribute('z_typ')->getFrontend()->getValue($product);
$z_bereich = $product->getResource()->getAttribute('z_bereich')->getFrontend()->getValue($product);
$categoryIds = implode(',', $product->getCategoryIds());//change the category separator if needed
fputcsv($fp, array($sku, $z_typ, $z_bereich, $categoryIds), ";");
}
fclose($fp);
?>
这只会返回&#34; NO&#34;这两个属性。我也试着跟随,这两列都是空的:
<?php
error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
Mage::app();
$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToSelect('category_ids');
$products->addAttributeToFilter('status', 1);//optional for only enabled products
$products->addAttributeToFilter('visibility', 4);//optional for products only visible in catalog and search
$fp = fopen('exports.csv', 'w');
$csvHeader = array("sku", "z_typ", "z_bereich", "category_ids");
fputcsv( $fp, $csvHeader,";");
foreach ($products as $product){
$sku = $product->getSku();
$z_typ = $product->getAttributeText('z_typ');
$z_bereich = $product->getAttributeText('z_bereich');
$categoryIds = implode(',', $product->getCategoryIds());//change the category separator if needed
fputcsv($fp, array($sku, $z_typ, $z_bereich, $categoryIds), ";");
}
fclose($fp);
?>
答案 0 :(得分:0)
自己发现错误......
忘记将两个属性加载到$ products数组中,因此无法显示它们。
感谢您的帮助:)
对于任何想要使用它的人来说,这是正确的代码:
<?php
error_reporting(E_ALL | E_STRICT);
define('MAGENTO_ROOT', getcwd());
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Mage::setIsDeveloperMode(true);
ini_set('display_errors', 1);
Mage::app();
$products = Mage::getModel("catalog/product")->getCollection();
$products->addAttributeToSelect('category_ids');
$products->addAttributeToSelect('z_typ');
$products->addAttributeToSelect('z_bereich');
$products->addAttributeToFilter('status', 1);//optional for only enabled products
$products->addAttributeToFilter('visibility', 4);//optional for products only visible in catalog and search
$fp = fopen('exports.csv', 'w');
$csvHeader = array("sku", "z_typ", "z_bereich", "category_ids");
fputcsv( $fp, $csvHeader,";");
foreach ($products as $product){
$sku = $product->getSku();
$attributes = $product->getAttributes();
$z_typ = $attributes['z_typ']->getFrontend()->getValue($product);
$z_bereich = $attributes['z_bereich']->getFrontend()->getValue($product);
$categoryIds = implode(',', $product->getCategoryIds());//change the category separator if needed
fputcsv($fp, array($sku, $z_typ, $z_bereich, $categoryIds), ";");
}
fclose($fp);
?>