Magento通过编程导入产品添加自定义选项

Magento通过编程导入产品添加自定义选项

本文介绍了Magento通过编程导入产品添加自定义选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在基于.csv文件以编程方式在Magento中导入简单产品.一切正常,但是我必须在创建一些自定义属性的地方添加一些值.但是我不知道如何添加它们.

I'm programmatically importing simple products in Magento based on a .csv file. Everything works fine, but I have to add some values where I have created some custom attributes for. But I don't know how to add them.

我想,自从我在自定义属性所在的位置添加了属性集之后,就可以像这样添加它们了 $ product-> setCustomAttribute(value)但这不起作用.

I thought since I added the attribute set where the custom attributes are in, I could add them like$product->setCustomAttribute(value)but that doesn't work.

如何添加它们?

其余代码供参考

if(($handle = fopen("werkbladvoorraad.csv", "r")) !== FALSE){
    while(($data = fgetcsv($handle, 1000, ";")) !== FALSE){
        $row++;

        if($row == 1){
            continue;
        }

    try{
        $product ->setStoreId(1)
                 ->setWebsiteIds(array(1))
                 ->setAttributeSetId(4)
                 ->setTypeId('simple')
                 //->setCreatedAt(strtotime('now'))
                 ->setSku($data[0])
                 ->setName($data[1])
                 //->setWeight()
                 ->setStatus(1)
                 ->setTaxClassId(2)
                 ->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH)
                 ->setManufacturer($data[14])
                 ->setColor($data[10])
                 ->setPrice($data[6])
                 ->setMaat_heren($data[4]) // CUSTOM ATTRIBUTE
                 ->setStockData(array(
                       'use_config_manage_stock' => 0,
                        'manage_stock'=>1,
                        'min_sale_qty'=>1,
                        'max_sale_qty'=>2,
                        'is_in_stock' =>1,
                        'qty' => 999
                    ));
       $product->save();
    }catch(Exception $e){
        echo ($e->getMessage());
    }
  }
}

注意
没有显示错误消息

Note
There are no error messages displayed

推荐答案

我知道这很愚蠢,但是您可以尝试清理缓存,然后保存有问题的自定义属性.如果这样不起作用,请尝试通过以下方式强制保存:

I know it's silly, but you could try cleaning up your cache and then saving the custom attributes in question. If that doesn't work, try forcing the saving with:

$product->setMyCustomAttr('My Custom Attr');
$product->getResource()->saveAttribute($product, 'my_custom_attr');

只需确保您已经保存了该产品,并再次使用其ID加载了该产品.为此,您将使用

Just make sure that you have already saved the product and loaded it again with its ID. In order to do that you would use

Mage::getModel('catalog/product')->load(1);

让我知道它是否可以正常工作:)

Let me know if this works properly :)

这篇关于Magento通过编程导入产品添加自定义选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 08:10