【问题标题】:Programmatically created configurable product doesnt show on frontend以编程方式创建可配置产品不显示在前端
【发布时间】:2023-03-22 00:46:01
【问题描述】:

我创建了将产品导入 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;
    }
}

【问题讨论】:

  • 可能是创建产品的 php 代码中的错误。你能再发一些吗?

标签: magento


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:

    从您的描述看来,产品还不错,只是知名度的问题:

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

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

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

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

    【讨论】:

    • 如果你看看我写的,这一切都OK。当我到达后端时,我没有更改任何内容,我只是点击“保存”。可见性、storeId、websiteId 都可以。
    • 我读到了,但是由于您的产品创建代码似乎没有遗漏任何内容,后端中的“保存”会改变一些东西。切换的产品在后端是否可见? (参见 Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE 问题)
    • 否,后端的可见性不会改变。 Config 仍然是 Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,简单的产品是 Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE。我也迷路了/困惑,应该没问题,但同时它不是。已经尝试解决它超过一天,但没有任何效果。
    • 也许库存也是你的问题:stackoverflow.com/questions/15137413/…
    • 会看的。但是它确实有一件事,但只是有时,所以我不确定它是否相关。有时在我导入产品后,产品价格索引不会重新索引并显示服务器错误,它只会在我删除导入的产品后重新索引。当添加到可配置产品时,也许我对简单产品价格做错了什么?
    【解决方案3】:

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

    【讨论】:

    • 手动尝试过,我也有它在代码中,以便在导入后重新索引。但有时价格指数没有完成并显示服务器错误。只有在我删除所有已导入的产品后才能使用。
    猜你喜欢
    • 1970-01-01
    • 2023-04-04
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 2012-05-18
    • 2013-05-05
    • 1970-01-01
    • 2017-01-07
    相关资源
    最近更新 更多