2014-11-06 84 views
0

我有另一个Magento问题。 我正在尝试将所有活动产品分类到新类别中,这会占用大量时间。所以现在我想导出产品并做得更快一些,但Magento导出函数并不适合我,因为它在导出到csv时不会在正确的列中显示正确的值。Magento获取下拉属性值与PHP

所以我使用这个我在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); 
?> 

这只对两个属性返回“NO”。我也试过以下,这让两栏留空:

<?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

发现错误自己...

忘了这两个属性加载到$产品阵列,因此它不可能显示出来。

感谢您的帮助反正:)

谁想要使用这个,这里是正确的代码:

<?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); 
?>