Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatically created configurable product doesnt show on frontend
    text
    copied!<p>I have created module that imports products to Magento. It creates both simple and configurable products and adds those simple products to the configurable product. Products are added to their categories by</p> <pre><code>$product-&gt;setCategoryIds($categoryIds); </code></pre> <p>and just in case I also do this</p> <pre><code> foreach($categoryIds as $i=&gt;$v){ Mage::getSingleton('catalog/category_api')-&gt;assignProduct($v, $product-&gt;getId()); } </code></pre> <p>When I look in backend products have their categories checked, also when I look at categories they have their products added too.</p> <p>So everything looks fine, but when I go on frontend to category, products are missing from there. Then all I have to do is open the configurable product and/or one of its simple products and WITHOUT changing anything I just hit save and then when I go to frontend the product is visible in category.</p> <p>Ofcourse cashe is disabled and indexing is run after import is done, so no problem there.</p> <p><strong>EDIT</strong></p> <p>This creates the configurable product.</p> <pre><code>private function createParentProduct($productId){ if($this-&gt;product&amp;&amp;!$productId){//create $name=$this-&gt;product-&gt;sGetName($this-&gt;language['value']); if(!$name||$name==''){ return Mage::helper('shirtplugin')-&gt;__('failed, missing product name'); } $data=$this-&gt;product-&gt;sGetData(); $product = Mage::getModel('Mage_Catalog_Model_Product'); $product-&gt;setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE); $product-&gt;setTaxClassId($this-&gt;taxClass['value']); $product-&gt;setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); $product-&gt;setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED); $product-&gt;setStoreId($this-&gt;scope['scopeId']); $store=Mage::getModel('Mage_Core_Model_Store')-&gt;load($this-&gt;scope['scopeId']); $product-&gt;setWebsiteIDs(array($store-&gt;getWebsiteId())); $product-&gt;setAttributeSetId(4); $product-&gt;setSku('shirtId-'.$this-&gt;product-&gt;sGetId()); $product-&gt;setName($name); $product-&gt;setUrl(Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')-&gt;removeDiacritic($name)); $product-&gt;setPrice(floatVal($this-&gt;product-&gt;sGetPrice($this-&gt;language['value']))); $product-&gt;setCreatedAt(time()); $product-&gt;setDescription($this-&gt;product-&gt;sGetDescription($this-&gt;language['value'])); $product-&gt;setShortDescription($this-&gt;product-&gt;sGetShortDescription($this-&gt;language['value'])); $product-&gt;setData('shirt_artNr', $data['artNr']); $product-&gt;setShirtModel($data['model']); $materials=$this-&gt;product-&gt;sGetAssignedMaterials(); $assigMaterials=array(); foreach($materials as $i=&gt;$v){ $material=$v-&gt;sGetMaterial()-&gt;sGetName(); if(!$material||$material==''){ continue; } $assigMaterials[]=$this-&gt;addNewValueToAttribute('shirt_material', $material, $v-&gt;sGetMaterial()-&gt;sGetId()); } $product-&gt;setData('shirt_material', $assigMaterials); $categoryIds=array(); $categories=$this-&gt;product-&gt;sGetAssignedCategories(); foreach($categories as $i=&gt;$v){ $category=$v-&gt;sGetCategory()-&gt;checkIfCategoryExists(); if($category){ $categoryIds[]=$category; } } $product-&gt;setCategoryIds($categoryIds); $product-&gt;setStockData(array( //'is_in_stock'=&gt;1, //'qty'=&gt;null, 'manage_stock'=&gt;0, 'use_config_manage_stock'=&gt;0, 'use_config_enable_qty_increments'=&gt;0, //'use_config_notify_stock_qty'=&gt;0, 'enable_qty_increments'=&gt;0, )); $newAttributes=array(); foreach(array('shirt_color', 'shirt_size') as $attrCode){ $super_attribute=Mage::getModel('eav/entity_attribute')-&gt;loadByCode('catalog_product',$attrCode); $configurableAtt=Mage::getModel('catalog/product_type_configurable_attribute')-&gt;setProductAttribute($super_attribute); $newAttributes[] = array( 'id'=&gt;$configurableAtt-&gt;getId(), 'label'=&gt;$configurableAtt-&gt;getLabel(), 'position'=&gt;$super_attribute-&gt;getPosition(), 'values'=&gt;array(), 'attribute_id'=&gt;$super_attribute-&gt;getId(), 'attribute_code'=&gt;$super_attribute-&gt;getAttributeCode(), 'frontend_label'=&gt;$super_attribute-&gt;getFrontend()-&gt;getLabel(), ); } $configurableData=array(); $colors=$this-&gt;product-&gt;sGetAssignedColors(); $sizes=$this-&gt;product-&gt;sGetAssignedSizes(); $simpleProducts=array(); $colorId=Mage::getModel('eav/entity_attribute')-&gt;loadByCode('catalog_product','shirt_color')-&gt;getId(); $sizeId=Mage::getModel('eav/entity_attribute')-&gt;loadByCode('catalog_product','shirt_size')-&gt;getId(); $filterColors=array(); $filterSizes=array(); foreach($colors as $i=&gt;$v){ $name=$v-&gt;sGetColor()-&gt;sGetName($this-&gt;language['value']); if(in_array($name, $filterColors)){ unset($colors[$i]); }else{ $filterColors[]=$name; $newAttributes[0]['values'][]=array( 'value_index'=&gt;0, 'label'=&gt;$name, 'is_percent'=&gt;0, 'pricing_value'=&gt;'0', 'attribute_id'=&gt;$colorId, ); } } foreach($sizes as $i=&gt;$v){ $name=$v-&gt;sGetSize()-&gt;sGetName($this-&gt;language['value']); if(in_array($name, $filterSizes)){ unset($sizes[$i]); }else{ $filterSizes[]=$name; $newAttributes[1]['values'][]=array( 'value_index'=&gt;0, 'label'=&gt;$name, 'is_percent'=&gt;0, 'pricing_value'=&gt;'0', 'attribute_id'=&gt;$sizeId, ); } } foreach($colors as $i=&gt;$v){ foreach($sizes as $si=&gt;$sv){ $clone=null; $clone=clone $product; $id=$this-&gt;createSimpleProduct($clone, $v-&gt;sGetColor(), $sv-&gt;sGetSize()); $simpleProducts[$id]=$id; $configurableData[$id]=array(); $configurableData[$id][]=array( 'attribute_id'=&gt;$colorId, 'label'=&gt;$v-&gt;sGetColor()-&gt;sGetName($this-&gt;language['value']), 'value_index'=&gt;$this-&gt;addNewValueToAttribute('shirt_color', $v-&gt;sGetColor()-&gt;sGetName($this-&gt;language['value']), $v-&gt;sGetColor()-&gt;sGetId()), ); $configurableData[$id][]=array( 'attribute_id'=&gt;$sizeId, 'label'=&gt;$sv-&gt;sGetSize()-&gt;sGetName($this-&gt;language['value']), 'value_index'=&gt;$this-&gt;addNewValueToAttribute('shirt_size', $sv-&gt;sGetSize()-&gt;sGetName($this-&gt;language['value']), $sv-&gt;sGetSize()-&gt;sGetId()), ); } } //echo "&lt;pre&gt;"; var_dump($newAttributes); echo "&lt;/pre&gt;"; exit; $product-&gt;setConfigurableProductsData($configurableData); $product-&gt;setConfigurableAttributesData($newAttributes); $product-&gt;setCanSaveConfigurableAttributes(1); $product-&gt;save(); foreach($categoryIds as $i=&gt;$v){ Mage::getSingleton('catalog/category_api')-&gt;assignProduct($v, $product-&gt;getId()); } $key='importProduct_'.$this-&gt;product-&gt;sGetId(); $setting=serialize(array( 'magentoId'=&gt;$product-&gt;getId(), 'time'=&gt;time() )); Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin')-&gt;saveSetting(Mage::helper('shirtplugin')-&gt;getCurrentAdminScope(), $key, $setting); $this-&gt;return=Mage::helper('shirtplugin')-&gt;__('created configurable product with %d simple products', count($simpleProducts)); return intVal($product-&gt;getId()); }elseif($this-&gt;product&amp;&amp;$productId){//update $name=$this-&gt;product-&gt;sGetName($this-&gt;language['value']); if(!$name||$name==''){ return Mage::helper('shirtplugin')-&gt;__('failed, missing product name'); } $data=$this-&gt;product-&gt;sGetData(); $product = Mage::getModel('Mage_Catalog_Model_Product')-&gt;load($productId); $product-&gt;setTypeId(Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE); $product-&gt;setTaxClassId($this-&gt;taxClass['value']); $product-&gt;setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); $product-&gt;setStatus(Mage_Catalog_Model_Product_Status::STATUS_ENABLED); $product-&gt;setStoreId($this-&gt;scope['scopeId']); $store=Mage::getModel('Mage_Core_Model_Store')-&gt;load($this-&gt;scope['scopeId']); $product-&gt;setWebsiteIDs(array($store-&gt;getWebsiteId())); $product-&gt;setAttributeSetId(4); $product-&gt;setSku('shirtId-'.$this-&gt;product-&gt;sGetId()); $product-&gt;setName($name); $product-&gt;setUrl(Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')-&gt;removeDiacritic($name)); $product-&gt;setPrice(floatVal($this-&gt;product-&gt;sGetPrice($this-&gt;language['value']))); $product-&gt;setCreatedAt(time()); $product-&gt;setDescription($this-&gt;product-&gt;sGetDescription($this-&gt;language['value'])); $product-&gt;setShortDescription($this-&gt;product-&gt;sGetShortDescription($this-&gt;language['value'])); $product-&gt;setData('shirt_artNr', $data['artNr']); $product-&gt;setShirtModel($data['model']); $materials=$this-&gt;product-&gt;sGetAssignedMaterials(); $assigMaterials=array(); foreach($materials as $i=&gt;$v){ $material=$v-&gt;sGetMaterial()-&gt;sGetName(); if(!$material||$material==''){ continue; } $assigMaterials[]=$this-&gt;addNewValueToAttribute('shirt_material', $material, $v-&gt;sGetMaterial()-&gt;sGetId()); } $product-&gt;setData('shirt_material', $assigMaterials); $categoryIds=array(); $categories=$this-&gt;product-&gt;sGetAssignedCategories(); foreach($categories as $i=&gt;$v){ $category=$v-&gt;sGetCategory()-&gt;checkIfCategoryExists(); if($category){ $categoryIds[]=$category; } } $product-&gt;setCategoryIds($categoryIds); $newAttributes=array(); foreach(array('shirt_color', 'shirt_size') as $attrCode){ $super_attribute=Mage::getModel('eav/entity_attribute')-&gt;loadByCode('catalog_product',$attrCode); $configurableAtt=Mage::getModel('catalog/product_type_configurable_attribute')-&gt;setProductAttribute($super_attribute); $newAttributes[] = array( 'id'=&gt;$configurableAtt-&gt;getId(), 'label'=&gt;$configurableAtt-&gt;getLabel(), 'position'=&gt;$super_attribute-&gt;getPosition(), 'values'=&gt;array(), 'attribute_id'=&gt;$super_attribute-&gt;getId(), 'attribute_code'=&gt;$super_attribute-&gt;getAttributeCode(), 'frontend_label'=&gt;$super_attribute-&gt;getFrontend()-&gt;getLabel(), ); } $configurableData=array(); $colors=$this-&gt;product-&gt;sGetAssignedColors(); $sizes=$this-&gt;product-&gt;sGetAssignedSizes(); $simpleProducts=array(); $colorId=Mage::getModel('eav/entity_attribute')-&gt;loadByCode('catalog_product','shirt_color')-&gt;getId(); $sizeId=Mage::getModel('eav/entity_attribute')-&gt;loadByCode('catalog_product','shirt_size')-&gt;getId(); $filterColors=array(); $filterSizes=array(); foreach($colors as $i=&gt;$v){ $name=$v-&gt;sGetColor()-&gt;sGetName($this-&gt;language['value']); if(in_array($name, $filterColors)){ unset($colors[$i]); }else{ $filterColors[]=$name; } } foreach($sizes as $i=&gt;$v){ $name=$v-&gt;sGetSize()-&gt;sGetName($this-&gt;language['value']); if(in_array($name, $filterSizes)){ unset($sizes[$i]); }else{ $filterSizes[]=$name; } } foreach($colors as $i=&gt;$v){ foreach($sizes as $si=&gt;$sv){ $clone=null; $clone=clone $product; $id=$this-&gt;createSimpleProduct($clone, $v-&gt;sGetColor(), $sv-&gt;sGetSize()); $simpleProducts[$id]=$id; $configurableData[$id]=array(); $configurableData[$id][]=array( 'attribute_id'=&gt;$colorId, 'label'=&gt;$v-&gt;sGetColor()-&gt;sGetName($this-&gt;language['value']), 'value_index'=&gt;$this-&gt;addNewValueToAttribute('shirt_color', $v-&gt;sGetColor()-&gt;sGetName($this-&gt;language['value']), $v-&gt;sGetColor()-&gt;sGetId()), ); $configurableData[$id][]=array( 'attribute_id'=&gt;$sizeId, 'label'=&gt;$sv-&gt;sGetSize()-&gt;sGetName($this-&gt;language['value']), 'value_index'=&gt;$this-&gt;addNewValueToAttribute('shirt_size', $sv-&gt;sGetSize()-&gt;sGetName($this-&gt;language['value']), $sv-&gt;sGetSize()-&gt;sGetId()), ); } } $product-&gt;setConfigurableProductsData($configurableData); //$product-&gt;setConfigurableAttributesData($newAttributes);//Shouldnt be needed here in update, but might need future modification $product-&gt;setCanSaveConfigurableAttributes(1); $product-&gt;save(); foreach($categoryIds as $i=&gt;$v){ Mage::getSingleton('catalog/category_api')-&gt;assignProduct($v, $product-&gt;getId()); } $key='importProduct_'.$this-&gt;product-&gt;sGetId(); $setting=serialize(array( 'magentoId'=&gt;$product-&gt;getId(), 'time'=&gt;time() )); Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin')-&gt;saveSetting(Mage::helper('shirtplugin')-&gt;getCurrentAdminScope(), $key, $setting); $this-&gt;return=Mage::helper('shirtplugin')-&gt;__('updated configurable product with %d simple products', count($simpleProducts)); return intVal($product-&gt;getId()); } } </code></pre> <p>And here is simple product.</p> <pre><code>private function createSimpleProduct($product, $color, $size){ $productId=$this-&gt;checkIfSimpleProductExists($this-&gt;product, $color, $size); if(!$productId){//create $product-&gt;setName($this-&gt;product-&gt;sGetName($this-&gt;language['value']).'-'.$color-&gt;sGetName($this-&gt;language['value']).'-'.$size-&gt;sGetName($this-&gt;language['value'])); $product-&gt;setUrl(Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')-&gt;removeDiacritic($product-&gt;getName())); $product-&gt;setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE); $product-&gt;setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE); $product-&gt;setSku('shirtId-'.$this-&gt;product-&gt;sGetId().'-'.Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')-&gt;removeDiacritic($color-&gt;sGetName($this-&gt;language['value'])).'-'.Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')-&gt;removeDiacritic($size-&gt;sGetName($this-&gt;language['value']))); $product-&gt;setWeight(0); $product-&gt;setData('shirt_color', $this-&gt;addNewValueToAttribute('shirt_color', $color-&gt;sGetName($this-&gt;language['value'])), $color-&gt;sGetId()); $product-&gt;setData('shirt_size', $this-&gt;addNewValueToAttribute('shirt_size', $size-&gt;sGetName($this-&gt;language['value'])), $size-&gt;sGetId()); $product-&gt;save(); foreach($categoryIds as $i=&gt;$v){ Mage::getSingleton('catalog/category_api')-&gt;assignProduct($v, $product-&gt;getId()); } $key='importProduct_'.$this-&gt;product-&gt;sGetId().'_'.$color-&gt;sGetId().'_'.$size-&gt;sGetId(); $setting=serialize(array( 'magentoId'=&gt;$product-&gt;getId(), 'time'=&gt;time() )); Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin')-&gt;saveSetting(Mage::helper('shirtplugin')-&gt;getCurrentAdminScope(), $key, $setting); }else{//update $data=$this-&gt;product-&gt;sGetData(); $product=Mage::getModel('Mage_Catalog_Model_Product')-&gt;load($productId); $product-&gt;setName($this-&gt;product-&gt;sGetName($this-&gt;language['value']).'-'.$color-&gt;sGetName($this-&gt;language['value']).'-'.$size-&gt;sGetName($this-&gt;language['value'])); $product-&gt;setUrl(Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')-&gt;removeDiacritic($product-&gt;getName())); $product-&gt;setTypeId(Mage_Catalog_Model_Product_Type::TYPE_SIMPLE); $product-&gt;setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE); $product-&gt;setSku('shirtId-'.$this-&gt;product-&gt;sGetId().'-'.Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')-&gt;removeDiacritic($color-&gt;sGetName($this-&gt;language['value'])).'-'.Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin_Import')-&gt;removeDiacritic($size-&gt;sGetName($this-&gt;language['value']))); $product-&gt;setWeight(0); $product-&gt;setDescription($this-&gt;product-&gt;sGetDescription($this-&gt;language['value'])); $product-&gt;setShortDescription($this-&gt;product-&gt;sGetShortDescription($this-&gt;language['value'])); $product-&gt;setData('shirt_artNr', $data['artNr']); $product-&gt;setShirtModel($data['model']); $materials=$this-&gt;product-&gt;sGetAssignedMaterials(); $assigMaterials=array(); foreach($materials as $i=&gt;$v){ $material=$v-&gt;sGetMaterial()-&gt;sGetName(); if(!$material||$material==''){ continue; } $assigMaterials[]=$this-&gt;addNewValueToAttribute('shirt_material', $material, $v-&gt;sGetMaterial()-&gt;sGetId()); } $product-&gt;setData('shirt_material', $assigMaterials); $product-&gt;setData('shirt_color', $this-&gt;addNewValueToAttribute('shirt_color', $color-&gt;sGetName($this-&gt;language['value'])), $color-&gt;sGetId()); $product-&gt;setData('shirt_size', $this-&gt;addNewValueToAttribute('shirt_size', $size-&gt;sGetName($this-&gt;language['value'])), $size-&gt;sGetId()); $categoryIds=array(); $categories=$this-&gt;product-&gt;sGetAssignedCategories(); foreach($categories as $i=&gt;$v){ $category=$v-&gt;sGetCategory()-&gt;checkIfCategoryExists(); if($category){ $categoryIds[]=$category; } } $product-&gt;setCategoryIds($categoryIds); $product-&gt;save(); foreach($categoryIds as $i=&gt;$v){ Mage::getSingleton('catalog/category_api')-&gt;assignProduct($v, $product-&gt;getId()); } $key='importProduct_'.$this-&gt;product-&gt;sGetId().'_'.$color-&gt;sGetId().'_'.$size-&gt;sGetId(); $setting=serialize(array( 'magentoId'=&gt;$product-&gt;getId(), 'time'=&gt;time() )); Mage::getModel('PSDCoding_Shirtplugin_Model_Shirtplugin')-&gt;saveSetting(Mage::helper('shirtplugin')-&gt;getCurrentAdminScope(), $key, $setting); } if($product-&gt;getId()){ return $product-&gt;getId(); }else{ return false; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload