doujiao6116 2012-06-03 15:47
浏览 17
已采纳

Magento编程:在检查现有重复项时导入制造商并获取manu。 ID

I need some help doing the following.

I am trying to import a list of manufacturers with pre-verification from the targeted store that new manufacturer to be imported does not already exist (if new manufacturer name already exist, skip) and get its new manufacturer ID if imported.

I have the code to just add manufacturer w/o checking for existing manufacturer by name nor getting the new manufacturer ID. The code is here, but I need the abilities thats stated above.

Any help would be great. Thanks in advance.

<?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条回答 默认 最新

  • dougou2937 2012-06-03 18:33
    关注

    Just a quick and dirty script, but this should be what you are looking for...

    <?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>";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型