-1

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

==编辑==

这个功能在社区>Bmservices>Polaris>Model>Observer下

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

==编辑结束==

$line从 csv 得到。

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
        )
    );
}

有什么我想念的吗。

4

1 回答 1

0

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

于 2014-08-19T13:44:51.583 回答