2012-06-03 52 views
-3

我需要一些幫助做下列事情。Magento編程:在檢查現有重複項並獲取manu時導入製造商。 ID

我試圖從導入的新制造商不存在(如果新的製造商名稱已經存在,跳過)並導入新的製造商ID,從導入的目標商店中導入製造商列表。

我有代碼只是添加製造商W/O檢查現有製造商的名稱或獲得新的製造商ID。代碼在這裏,但我需要上面說明的能力。

任何幫助將是偉大的。提前致謝。

<?php 
require_once 'app/Mage.php'; 
umask(0); 
Mage::app('default'); 
$_manufacturers = file('manufacturers.txt'); 
$_attribute = Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product', 'manufacturer'); 
$manufacturers = array('value' => array(), 'order' => array(), 'delete' => array()); 
$i = 0; 
foreach($_manufacturers as $_manufacturer){ 
$i++; 
$manufacturers['value']['option_' . $i] = array($_manufacturer); 
} 
$_attribute->setOption($manufacturers); 
try{ 
$_attribute->save(); 
echo 'Manufacturer successfully imported'; 
}catch(Exception $e){ 
echo 'Import Error::'.$e->getMessage(); 
} 

?> 
+1

-1 http://www.emilvikstrom.se/whyidownvote.html(有什麼你試過了嗎?) –

+0

我看過網際網路,沒有找到任何幫助。我需要的只是預先檢查一個要添加的製造商是否已經存在,如果沒有,請添加它並獲取其新ID。我試過使用上面的代碼。 – user1433709

+0

所以你還沒有嘗試寫任何代碼? –

回答

1

只是快速和骯髒的腳本,但是這應該是你在找什麼...

<?php 

error_reporting(E_ALL | E_STRICT);  
ini_set('display_errors', 1); 


/* 
* Boostrap Magento 
*/ 
$mageFilename = '../app/Mage.php'; 
require_once $mageFilename;   
Mage::setIsDeveloperMode(true);  
umask(0); 

$mageRunCode = ''; 
$mageRunType = 'store'; 
Mage::init($mageRunCode, $mageRunType); 

/* 
* Set up required data 
*/ 
$newManufacturers = file('manufacturers.txt', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES); 
$newManufacturers = array_unique($newManufacturers); 

$attribute = Mage::getModel('eav/entity_attribute') 
       ->loadByCode('catalog_product', 'manufacturer'); 

$valuesCollection = Mage::getResourceModel('eav/entity_attribute_option_collection') 
      ->setAttributeFilter($attribute->getData('attribute_id')) 
      ->setStoreFilter(0, false) 
      ->getColumnValues('value'); 

$installer = new Mage_Eav_Model_Entity_Setup('core_setup'); 

/* 
* Add new attributes 
*/ 
$addedManufacturers  = array(); 
$skippedManufacturers = array(); 

$i = count($valuesCollection); 
foreach($newManufacturers as $manufacturer) { 

    // If the value already exists then skip to next 
    if (in_array($manufacturer, $valuesCollection)) { 
     $skippedManufacturers[] = $manufacturer; 
     continue; 
    } 

    //If we have reached here then lets add the new attribute option 
    $newOption = array(); 
    $newOption['attribute_id'] = $attribute->getData('attribute_id'); 
    $newOption['value']['option_'.++$i][0] = $manufacturer; 
    $installer->addAttributeOption($newOption); 

    $optionValue = Mage::getResourceModel('eav/entity_attribute_option_collection') 
      ->setAttributeFilter($attribute->getId()) 
      ->setStoreFilter(0, false) 
      ->addFilter('value_id', $installer->getConnection()->lastInsertId()) 
      ->getColumnValues('option_id'); 

    $addedManufacturers[] = $optionValue[0]; 
} 

if (count($addedManufacturers)) { 
    echo "<h2>Manufacturers Added</h2><ul>"; 
    foreach($addedManufacturers as $added) { 
     echo "<li>" . $added . "</li>"; 
    } 
    echo "</ul>"; 
} 

if (count($skippedManufacturers)) { 
    echo "<h2>Manufacturers Skipped</h2><ul>"; 
    foreach($skippedManufacturers as $skipped) { 
     echo "<li>" . $skipped . "</li>"; 
    } 
    echo "</ul>"; 
} 
+0

謝謝,但是,我也需要添加新添加的獨特製造商的magento ID。那可能嗎? – user1433709

+0

@ user1433709 - 我已經爲你更新了示例代碼 –

+0

再次感謝你,最後一件似乎令我驚訝的事情是這樣的...是否有可能獲得所有制造商名稱與其對應的magento ID列表?我嘗試了一些mods,但只得到一個或另一個。如果可能的話,請幫助這最後一件事。我提前感謝你提請:) – user1433709

相關問題