Magento编程:导入制造商,同时检查现有的重复项并获得手稿.同上


Magento Programming: Importing manufacturers while checking for existing duplicates and get manu. ID

我需要一些帮助来做以下事情。

我正在尝试从目标商店导入一个经过预验证的制造商列表,该列表表明要导入的新制造商尚不存在(如果新的制造商名称已存在,请跳过),并在导入时获取其新的制造商 ID。

我有代码可以添加制造商,而无需按名称检查现有制造商,也没有获取新的制造商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();
}
?> 

只是一个快速而肮脏的脚本,但这应该是您正在寻找的......

<?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>";
}