简体   繁体   English

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

[英]Magento add custom options with programmatically importing products

I'm programmatically importing simple products in Magento based on a .csv file. 我正在基于.csv文件以编程方式在Magento中导入简单产品。 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. 但是我不知道如何添加它们。

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. 我以为自从我在自定义属性所在的位置添加了属性集之后,就可以像$product->setCustomAttribute(value)那样添加它们了,但这是行不通的。

How can I add them? 如何添加它们?

Rest of the code for reference 其余代码供参考

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');

Just make sure that you have already saved the product and loaded it again with its ID. 只需确保您已经保存了产品并再次使用其ID加载了该产品。 In order to do that you would use 为此,您将使用

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

Let me know if this works properly :) 让我知道这是否正常工作:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM