0

製品を Magento にインポートするモジュールを作成しました。単純な製品と構成可能な製品の両方を作成し、それらの単純な製品を構成可能な製品に追加します。製品は、によってカテゴリに追加されます

$product->setCategoryIds($categoryIds);

念のため、私もこれを行います

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

バックエンド製品を見ると、カテゴリがチェックされています。また、カテゴリを見ると、製品も追加されています。

すべて問題ないように見えますが、フロントエンドでカテゴリに移動すると、そこに製品がありません。次に、構成可能な製品および/またはその単純な製品の1つを開いて、何も変更せずに保存するだけで、フロントエンドに移動すると、製品がカテゴリに表示されます。

もちろん、キャッシュは無効で、インポートが完了した後にインデックス作成が実行されるため、問題はありません。

編集

これにより、構成可能な製品が作成されます。

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