以编程方式创建可配置产品不会在前端显示

时间:2013-09-20 13:45:58

标签: magento

我创建了将产品导入Magento的模块。它创建了简单和可配置的产品,并将这些简单的产品添加到可配置的产品中。

将产品添加到其类别中
$product->setCategoryIds($categoryIds);

以防万一我也这样做

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

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

所以一切看起来都很好,但是当我继续前进到类别时,那里的产品就不见了。然后,我所要做的就是打开可配置产品和/或其中一个简单的产品,而不更改任何我点击保存的内容,然后当我转到前端时,产品在类别中可见。

当然,在导入完成后禁用了索引,因此没有问题。

修改

这会创建可配置的产品。

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

3 个答案:

答案 0 :(得分:2)

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

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

创建库存物品对象并使用类别api来分配产品也很不错。

答案 1 :(得分:0)

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

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

是有意的吗?应默认显示。

如果这不起作用,请检查以下值:

$product->setWebsiteIDs(...) and $product->setStoreId(...) 

答案 2 :(得分:0)

我之前遇到的同样问题。 Reindex都解决了我的问题。你也可以试试。