0

我创建了将产品导入 Magento 的模块。它创建简单和可配置的产品,并将这些简单的产品添加到可配置的产品中。产品通过以下方式添加到其类别中

$product->setCategoryIds($categoryIds);

以防万一我也这样做

    foreach($categoryIds as $i=>$v){
            Mage::getSingleton('catalog/category_api')->assignProduct($v, $product->getId());
    }

当我查看后端产品时,会检查他们的类别,当我查看类别时,他们也会添加他们的产品。

所以一切看起来都很好,但是当我从前端转到类别时,那里缺少产品。然后我所要做的就是打开可配置产品和/或其简单产品之一,并且不更改任何内容,我只需点击保存,然后当我转到前端时,该产品在类别中可见。

当然 Cashe 被禁用,并且在导入完成后运行索引,所以没有问题。

编辑

这将创建可配置产品。

private function createParentProduct($productId){
    if($this->product&&!$productId){//create
        $name=$this->product->sGetName($this->language['value']);
        if(!$name||$name==''){
            return Mage::helper('shirtplugin')->__('failed, missing product name');
        }
        $data=$this->product->sGetData();

        $product = Mage::getModel('Mage_Catalog_Model_Product');
        $product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE);
        $product->setTaxClassId($this->taxClass['value']);
        $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
        $product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
        $product->setStoreId($this->scope['scopeId']);

        $store=Mage::getModel('Mage_Core_Model_Store')->load($this->scope['scopeId']);
        $product->setWebsiteIDs(array($store->getWebsiteId()));

        $product->setAttributeSetId(4);
        $product->setSku('shirtId-'.$this->product->sGetId());
        $product->setName($name);
        $product->setUrl(Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')->removeDiacritic($name));
        $product->setPrice(floatVal($this->product->sGetPrice($this->language['value'])));
        $product->setCreatedAt(time());
        $product->setDescription($this->product->sGetDescription($this->language['value']));
        $product->setShortDescription($this->product->sGetShortDescription($this->language['value']));

        $product->setData('shirt_artNr', $data['artNr']);
        $product->setShirtModel($data['model']);
        $materials=$this->product->sGetAssignedMaterials();
        $assigMaterials=array();
        foreach($materials as $i=>$v){
            $material=$v->sGetMaterial()->sGetName();
            if(!$material||$material==''){
                continue;
            }
            $assigMaterials[]=$this->addNewValueToAttribute('shirt_material', $material, $v->sGetMaterial()->sGetId());
        }
        $product->setData('shirt_material', $assigMaterials);

        $categoryIds=array();
        $categories=$this->product->sGetAssignedCategories();
        foreach($categories as $i=>$v){
            $category=$v->sGetCategory()->checkIfCategoryExists();
            if($category){
                $categoryIds[]=$category;
            }
        }
        $product->setCategoryIds($categoryIds);

        $product->setStockData(array(
            //'is_in_stock'=>1,
            //'qty'=>null,
            'manage_stock'=>0,
            'use_config_manage_stock'=>0,
            'use_config_enable_qty_increments'=>0,
            //'use_config_notify_stock_qty'=>0,
            'enable_qty_increments'=>0,
        ));

        $newAttributes=array();
        foreach(array('shirt_color', 'shirt_size') as $attrCode){
            $super_attribute=Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product',$attrCode);
            $configurableAtt=Mage::getModel('catalog/product_type_configurable_attribute')->setProductAttribute($super_attribute);

            $newAttributes[] = array(
                'id'=>$configurableAtt->getId(),
                'label'=>$configurableAtt->getLabel(),
                'position'=>$super_attribute->getPosition(),
                'values'=>array(),
                'attribute_id'=>$super_attribute->getId(),
                'attribute_code'=>$super_attribute->getAttributeCode(),
                'frontend_label'=>$super_attribute->getFrontend()->getLabel(),
            );
        }

        $configurableData=array();
        $colors=$this->product->sGetAssignedColors();
        $sizes=$this->product->sGetAssignedSizes();
        $simpleProducts=array();
        $colorId=Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product','shirt_color')->getId();
        $sizeId=Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product','shirt_size')->getId();
        $filterColors=array();
        $filterSizes=array();
        foreach($colors as $i=>$v){
            $name=$v->sGetColor()->sGetName($this->language['value']);
            if(in_array($name, $filterColors)){
                unset($colors[$i]);
            }else{
                $filterColors[]=$name;

                $newAttributes[0]['values'][]=array(
                    'value_index'=>0,
                    'label'=>$name,
                    'is_percent'=>0,
                    'pricing_value'=>'0',
                    'attribute_id'=>$colorId,
                );
            }
        }
        foreach($sizes as $i=>$v){
            $name=$v->sGetSize()->sGetName($this->language['value']);
            if(in_array($name, $filterSizes)){
                unset($sizes[$i]);
            }else{
                $filterSizes[]=$name;

                $newAttributes[1]['values'][]=array(
                    'value_index'=>0,
                    'label'=>$name,
                    'is_percent'=>0,
                    'pricing_value'=>'0',
                    'attribute_id'=>$sizeId,
                );
            }
        }
        foreach($colors as $i=>$v){
            foreach($sizes as $si=>$sv){
                $clone=null;
                $clone=clone $product;
                $id=$this->createSimpleProduct($clone, $v->sGetColor(), $sv->sGetSize());
                $simpleProducts[$id]=$id;

                $configurableData[$id]=array();
                $configurableData[$id][]=array(
                    'attribute_id'=>$colorId,
                    'label'=>$v->sGetColor()->sGetName($this->language['value']),
                    'value_index'=>$this->addNewValueToAttribute('shirt_color', $v->sGetColor()->sGetName($this->language['value']), $v->sGetColor()->sGetId()),
                );
                $configurableData[$id][]=array(
                    'attribute_id'=>$sizeId,
                    'label'=>$sv->sGetSize()->sGetName($this->language['value']),
                    'value_index'=>$this->addNewValueToAttribute('shirt_size', $sv->sGetSize()->sGetName($this->language['value']), $sv->sGetSize()->sGetId()),
                );
            }
        }
        //echo "<pre>"; var_dump($newAttributes); echo "</pre>"; exit;
        $product->setConfigurableProductsData($configurableData);
        $product->setConfigurableAttributesData($newAttributes);
        $product->setCanSaveConfigurableAttributes(1);

        $product->save();

        foreach($categoryIds as $i=>$v){
            Mage::getSingleton('catalog/category_api')->assignProduct($v, $product->getId());
        }

        $key='importProduct_'.$this->product->sGetId();
        $setting=serialize(array(
            'magentoId'=>$product->getId(),
            'time'=>time()
        ));
        Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin')->saveSetting(Mage::helper('shirtplugin')->getCurrentAdminScope(), $key, $setting);
        $this->return=Mage::helper('shirtplugin')->__('created configurable product with %d simple products', count($simpleProducts));
        return intVal($product->getId());
    }elseif($this->product&&$productId){//update
        $name=$this->product->sGetName($this->language['value']);
        if(!$name||$name==''){
            return Mage::helper('shirtplugin')->__('failed, missing product name');
        }
        $data=$this->product->sGetData();

        $product = Mage::getModel('Mage_Catalog_Model_Product')->load($productId);
        $product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE);
        $product->setTaxClassId($this->taxClass['value']);
        $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH);
        $product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED);
        $product->setStoreId($this->scope['scopeId']);

        $store=Mage::getModel('Mage_Core_Model_Store')->load($this->scope['scopeId']);
        $product->setWebsiteIDs(array($store->getWebsiteId()));

        $product->setAttributeSetId(4);
        $product->setSku('shirtId-'.$this->product->sGetId());
        $product->setName($name);
        $product->setUrl(Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')->removeDiacritic($name));
        $product->setPrice(floatVal($this->product->sGetPrice($this->language['value'])));
        $product->setCreatedAt(time());
        $product->setDescription($this->product->sGetDescription($this->language['value']));
        $product->setShortDescription($this->product->sGetShortDescription($this->language['value']));

        $product->setData('shirt_artNr', $data['artNr']);
        $product->setShirtModel($data['model']);
        $materials=$this->product->sGetAssignedMaterials();
        $assigMaterials=array();
        foreach($materials as $i=>$v){
            $material=$v->sGetMaterial()->sGetName();
            if(!$material||$material==''){
                continue;
            }
            $assigMaterials[]=$this->addNewValueToAttribute('shirt_material', $material, $v->sGetMaterial()->sGetId());
        }
        $product->setData('shirt_material', $assigMaterials);

        $categoryIds=array();
        $categories=$this->product->sGetAssignedCategories();
        foreach($categories as $i=>$v){
            $category=$v->sGetCategory()->checkIfCategoryExists();
            if($category){
                $categoryIds[]=$category;
            }
        }
        $product->setCategoryIds($categoryIds);

        $newAttributes=array();
        foreach(array('shirt_color', 'shirt_size') as $attrCode){
            $super_attribute=Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product',$attrCode);
            $configurableAtt=Mage::getModel('catalog/product_type_configurable_attribute')->setProductAttribute($super_attribute);

            $newAttributes[] = array(
                'id'=>$configurableAtt->getId(),
                'label'=>$configurableAtt->getLabel(),
                'position'=>$super_attribute->getPosition(),
                'values'=>array(),
                'attribute_id'=>$super_attribute->getId(),
                'attribute_code'=>$super_attribute->getAttributeCode(),
                'frontend_label'=>$super_attribute->getFrontend()->getLabel(),
            );
        }

        $configurableData=array();
        $colors=$this->product->sGetAssignedColors();
        $sizes=$this->product->sGetAssignedSizes();
        $simpleProducts=array();
        $colorId=Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product','shirt_color')->getId();
        $sizeId=Mage::getModel('eav/entity_attribute')->loadByCode('catalog_product','shirt_size')->getId();
        $filterColors=array();
        $filterSizes=array();
        foreach($colors as $i=>$v){
            $name=$v->sGetColor()->sGetName($this->language['value']);
            if(in_array($name, $filterColors)){
                unset($colors[$i]);
            }else{
                $filterColors[]=$name;
            }
        }
        foreach($sizes as $i=>$v){
            $name=$v->sGetSize()->sGetName($this->language['value']);
            if(in_array($name, $filterSizes)){
                unset($sizes[$i]);
            }else{
                $filterSizes[]=$name;
            }
        }
        foreach($colors as $i=>$v){
            foreach($sizes as $si=>$sv){
                $clone=null;
                $clone=clone $product;
                $id=$this->createSimpleProduct($clone, $v->sGetColor(), $sv->sGetSize());
                $simpleProducts[$id]=$id;

                $configurableData[$id]=array();
                $configurableData[$id][]=array(
                    'attribute_id'=>$colorId,
                    'label'=>$v->sGetColor()->sGetName($this->language['value']),
                    'value_index'=>$this->addNewValueToAttribute('shirt_color', $v->sGetColor()->sGetName($this->language['value']), $v->sGetColor()->sGetId()),
                );
                $configurableData[$id][]=array(
                    'attribute_id'=>$sizeId,
                    'label'=>$sv->sGetSize()->sGetName($this->language['value']),
                    'value_index'=>$this->addNewValueToAttribute('shirt_size', $sv->sGetSize()->sGetName($this->language['value']), $sv->sGetSize()->sGetId()),
                );
            }
        }
        $product->setConfigurableProductsData($configurableData);
        //$product->setConfigurableAttributesData($newAttributes);//Shouldnt be needed here in update, but might need future modification
        $product->setCanSaveConfigurableAttributes(1);

        $product->save();

        foreach($categoryIds as $i=>$v){
            Mage::getSingleton('catalog/category_api')->assignProduct($v, $product->getId());
        }

        $key='importProduct_'.$this->product->sGetId();
        $setting=serialize(array(
            'magentoId'=>$product->getId(),
            'time'=>time()
        ));
        Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin')->saveSetting(Mage::helper('shirtplugin')->getCurrentAdminScope(), $key, $setting);
        $this->return=Mage::helper('shirtplugin')->__('updated configurable product with %d simple products', count($simpleProducts));
        return intVal($product->getId());
    }
}

这是简单的产品。

private function createSimpleProduct($product, $color, $size){
    $productId=$this->checkIfSimpleProductExists($this->product, $color, $size);
    if(!$productId){//create
        $product->setName($this->product->sGetName($this->language['value']).'-'.$color->sGetName($this->language['value']).'-'.$size->sGetName($this->language['value']));
        $product->setUrl(Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')->removeDiacritic($product->getName()));
        $product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
        $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
        $product->setSku('shirtId-'.$this->product->sGetId().'-'.Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')->removeDiacritic($color->sGetName($this->language['value'])).'-'.Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')->removeDiacritic($size->sGetName($this->language['value'])));
        $product->setWeight(0);

        $product->setData('shirt_color', $this->addNewValueToAttribute('shirt_color', $color->sGetName($this->language['value'])), $color->sGetId());
        $product->setData('shirt_size', $this->addNewValueToAttribute('shirt_size', $size->sGetName($this->language['value'])), $size->sGetId());

        $product->save();

        foreach($categoryIds as $i=>$v){
            Mage::getSingleton('catalog/category_api')->assignProduct($v, $product->getId());
        }

        $key='importProduct_'.$this->product->sGetId().'_'.$color->sGetId().'_'.$size->sGetId();
        $setting=serialize(array(
            'magentoId'=>$product->getId(),
            'time'=>time()
        ));
        Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin')->saveSetting(Mage::helper('shirtplugin')->getCurrentAdminScope(), $key, $setting);
    }else{//update
        $data=$this->product->sGetData();
        $product=Mage::getModel('Mage_Catalog_Model_Product')->load($productId);

        $product->setName($this->product->sGetName($this->language['value']).'-'.$color->sGetName($this->language['value']).'-'.$size->sGetName($this->language['value']));
        $product->setUrl(Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')->removeDiacritic($product->getName()));
        $product->setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE);
        $product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
        $product->setSku('shirtId-'.$this->product->sGetId().'-'.Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')->removeDiacritic($color->sGetName($this->language['value'])).'-'.Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')->removeDiacritic($size->sGetName($this->language['value'])));
        $product->setWeight(0);
        $product->setDescription($this->product->sGetDescription($this->language['value']));
        $product->setShortDescription($this->product->sGetShortDescription($this->language['value']));

        $product->setData('shirt_artNr', $data['artNr']);
        $product->setShirtModel($data['model']);
        $materials=$this->product->sGetAssignedMaterials();
        $assigMaterials=array();
        foreach($materials as $i=>$v){
            $material=$v->sGetMaterial()->sGetName();
            if(!$material||$material==''){
                continue;
            }
            $assigMaterials[]=$this->addNewValueToAttribute('shirt_material', $material, $v->sGetMaterial()->sGetId());
        }
        $product->setData('shirt_material', $assigMaterials);
        $product->setData('shirt_color', $this->addNewValueToAttribute('shirt_color', $color->sGetName($this->language['value'])), $color->sGetId());
        $product->setData('shirt_size', $this->addNewValueToAttribute('shirt_size', $size->sGetName($this->language['value'])), $size->sGetId());

        $categoryIds=array();
        $categories=$this->product->sGetAssignedCategories();
        foreach($categories as $i=>$v){
            $category=$v->sGetCategory()->checkIfCategoryExists();
            if($category){
                $categoryIds[]=$category;
            }
        }
        $product->setCategoryIds($categoryIds);

        $product->save();

        foreach($categoryIds as $i=>$v){
            Mage::getSingleton('catalog/category_api')->assignProduct($v, $product->getId());
        }

        $key='importProduct_'.$this->product->sGetId().'_'.$color->sGetId().'_'.$size->sGetId();
        $setting=serialize(array(
            'magentoId'=>$product->getId(),
            'time'=>time()
        ));
        Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin')->saveSetting(Mage::helper('shirtplugin')->getCurrentAdminScope(), $key, $setting);
    }
    if($product->getId()){
        return $product->getId();
    }else{
        return false;
    }
}
4

3 回答 3

2

问题是 Magento 没有更新库存状态。因此,当您以编程方式创建产品时,至少不要忘记添加:

$stockStatus = Mage::getModel('cataloginventory/stock_status');
$stockStatus->assignProduct($product);
$stockStatus->saveProductStatus($product->getId(), 1);

创建库存项目对象并使用类别 api 分配产品也很好。

于 2013-09-23T13:23:02.243 回答
0

我之前遇到过同样的问题。重新索引都解决了我的问题。你也可以试试。

于 2013-09-22T11:40:05.247 回答
0

根据您的描述,产品似乎很好,只是可见性问题:

$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
// why VISIBILITY_NOT_VISIBLE?

这是故意的吗?默认情况下应该是可见的。

如果这不起作用,请检查您的值:

$product->setWebsiteIDs(...) and $product->setStoreId(...) 
于 2013-09-20T14:32:40.287 回答