以编程方式导入可配置产品

时间:2014-08-11 07:45:06

标签: php magento magento-1.9

我使用cron导入数据库中的产品。产品已导入,但可配置产品与简单产品之间的关联未正确完成。每个新产品都会完成关联,但最后只会导入最后一个产品。请找到我在下面使用的功能。

== EDIT ==

此功能位于社区> Bmservices> Polaris>模型>观察者

它由config.xml中定义的cron调用。我确信该函数被调用,因为我正在登录它并且日志正在填充正确的信息。

== END OF EDIT ==

我从csv获得$line

private function _importProduct($line){
    $utf8bom = "\xef\xbb\xbf";
    $rayon = $this->_checkCategory($line[21]+2, $line[22]);
    $famille = $this->_checkCategory($line[23], $line[24], $rayon);
    $sousfamille = $this->_checkCategory($line[25], $line[26], $famille);
    $idManu = $this->_checkAttribute($line[5], $line[27], $this->_attributManufacturer);
    $idSais = $this->_checkAttribute($line[15], $line[28], $this->_attributSaison);
    $idColl = $this->_checkAttribute($line[16], $line[29], $this->_attributCollection);
    $idMati = $this->_checkAttribute($line[11], $line[35], $this->_attributMatiere);
    $idCoul = $this->_checkAttribute($line[3], $line[20], $this->_attributCouleur);
    $idTail = $this->_checkAttribute($line[4], $line[42], $this->_attributTaille, $line[43]);
    $idAttS = $this->_checkAttributeSet($line[49], $line[50]);
    $product = Mage::getModel('catalog/product');
    $sku = trim($line[0], $utf8bom);
    $sku = trim($sku, '"');
    $product->loadByAttribute('sku',$sku);
    $id = $product->getId();
    if (empty($id)){
        Mage::log("nouveau produit avec le sku $sku");
        $product->setStoreId(1);
        $product->setSku($sku);
        $product->setAttributeSetId($idAttS);
        $product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
    }
    if ($line!=''){
        $product->setName($line[18]);
    } else {
        $product->setName($line[17]);
    }
    $product->setDescription($line[19]);
    $product->setCreatedAt($line[41]);
    $product->setShortDescription($line[19]);
    $product->setData('collecannee', $idColl);
    $product->setData('matiere', $idMati);
    $product->setData('manufacturer', $idManu);
    $product->setData('saison', $idSais);    
    $product->setMetaKeyword($line[22].','.$line[24].','.$line[26].','.$line[27].','.$line[28]);
    $product->setMetaTitle($line[18]);
    $product->setMetaDescription($line[19]);
    $product->setWeight(0);
    $product->setData('price',$line[47]);
    $product->setCategoryIds(array($rayon, $famille, $sousfamille));
    $product->setFinalPrice($line[47])        
        ->setMediaGallery(array('images' => array(), 'values' => array())) //media gallery initialization
        ->setMsrpEnabled(0) //enable MAP
        ->setMsrpDisplayActualPriceType(4) //display actual price (1 - on gesture, 2 - in cart, 3 - before order confirmation, 4 - use config)
        ->setMsrp($line[47]); //Manufacturer's Suggested Retail Price
    if ($line[48]!=''){
        $product->setSpecialPrice($line[48]);
        $product->setSpecialFromDate('2014-08-01');
        $product->setSpecialFromDateIsFormated(true);
        $product->setSpecialToDate('2014-08-30');
        $product->setSpecialToDateIsFormated(true);
    } else {
        $product->setSpecialPrice(null);
    }
    $product->setTaxClassId(2);
    if ((!isset($this->_productConf))||($this->_productConf->getSku()!='C-'.$line[2])){
        if ((isset($this->_productConf))&&($this->_productConf->getSku()!='C-'.$line[2])){
            $this->_productConf->setConfigurableProductsData($this->_configurableProductsData);
            $this->_productConf->save();
        }
        $this->_productConf = Mage::getModel('catalog/product');
        $skuConf = 'C-'.$line[2];
        $this->_productConf->loadByAttribute('sku',$skuConf);
        $this->_configurableProductsData = array();
        $idConf = $this->_productConf->getId();
        if (empty($idConf)){
            Mage::log('Création d\'un produit configurable : '.$skuConf);
            $this->_productConf->setData($product->getData());
            $this->_productConf->setTypeId('configurable');
            $this->_productConf->setData('size', NULL);
            $this->_productConf->setData('color', NULL);
            $attribute_ids = array($this->_attributTaille, $this->_attributCouleur);
            $this->_productConf->getTypeInstance()->setUsedProductAttributeIds($attribute_ids);
            $configurableAttributesData = $this->_productConf->getTypeInstance()->getConfigurableAttributesAsArray();
            $this->_productConf->setCanSaveConfigurableAttributes(true);
            $this->_productConf->setConfigurableAttributesData($configurableAttributesData);
            $StockData['manage_stock'] = 1;
            $StockData['is_in_stock'] = 1;
            $StockData['use_config_manage_stock'] = 0;
            $StockData['use_config_min_qty'] = 0;
            $this->_productConf->setStockData($StockData);
            $this->_productConf->setCanSaveConfigurableAttributes(true);
            $this->_productConf->setCanSaveCustomOptions(true);
            $this->_productConf->setSku($skuConf);
            $this->_productConf->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
        }
    }
    $StockData['qty'] = floatval($line[46]);
    $StockData['is_in_stock'] = ($StockData['qty']>0) ? 1 : 0;
    $StockData['manage_stock'] = 1;
    $StockData['use_config_manage_stock'] = 0;
    $StockData['use_config_min_qty'] = 0;
    $product->setStockData($StockData);
    $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
    $product->setData('size', $idTail);
    $product->setData('color', $idCoul);
    Mage::log("sauvegarde du produit");
    $product->validate();
    $product->save();

    /**
     * mise à jour du prix et de la classe de taxe
     */
    Mage::getSingleton('catalog/product_action')->updateAttributes(
        array($product->getId()),
        array(121 => 2, 75 => $line[47]),
        0
    );

    /**
     * gestion du stock
     */
    $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product->getId());
    $stockItemId = $stockItem->getId();
    $stock = array();

    if (!$stockItemId) {
        $stockItem->setData('product_id', $product->getId());
        $stockItem->setData('stock_id', 1);
    } else {
        $stock = $stockItem->getData();
    }

    foreach($StockData as $field => $value) {
        $stockItem->setData($field, $value?$value:0);
    }
    $stockItem->save();
    $this->_configurableProductsData[$product->getId()] = array( //['920'] = id of a simple product associated with this configurable
        '0' => array(
            'label' => $line[20], //attribute label
            'attribute_id' => $this->_attributCouleur, //attribute ID of attribute 'color' in my store
            'value_index' => $idCoul, //value of 'Green' index of the attribute 'color'
            'is_percent' => '0', //fixed/percent price for this option
            'pricing_value' => $line[47] //value for the pricing
        ),
        '1' => array(
            'label' => $line[42], //attribute label
            'attribute_id' => $this->_attributTaille, //attribute ID of attribute 'color' in my store
            'value_index' => $idTail, //value of 'Green' index of the attribute 'color'
            'is_percent' => '0', //fixed/percent price for this option
            'pricing_value' => $line[47] //value for the pricing
        )
    );
}

我有什么想念。

1 个答案:

答案 0 :(得分:0)

我发现了自己的错误,我无法相信它让我陷入了一个多星期的困境。如果有人遇到同样的问题,答案就在于网站ID。我没有影响,因此产品在任何商店都不存在,因此受到影响但在产品页面上看不到。我无法解释为什么我在管理员和数据库中拥有它,但现在一切正常。