2014-11-06 108 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); 
?>