无法以编程方式向产品添加自定义选项

时间:2015-03-07 22:28:59

标签: php magento

我有代码可以创建产品,然后为其添加自定义选项。 但是,如果我在管理面板中打开它,则不会添加自定义选项。此外,不会生成任何错误消息或警告。我的Magento版本是1.9.1.0。 脚本有什么问题?

$sku = 'test2';
$name = 'Test Product';
$description = 'Test Product Description';
$shortdescription = 'Test Product Short Description';
$price = '100';
$specialprice = '80';
$specialfromdate = '08/20/2014';
$specialtodate = '08/22/2014';
$categoryids = array(3,4,5);
$taxClassId = 0;    // None
$visibility = 4;    // catalog, search
$productStatus = 1;    // enabled
$createdDate = '08/20/2014';
$updatedDate = '08/20/2014';
//$imagepath = 'C:\Users\Public\Pictures\Sample Pictures\Chrysanthemum.jpg';    //    absolute path of image in local file system/server path
$color = 7;    //    Dropdown Attribute i.e 7 is attribute option id
$brand = '13,14,16';    //    Multiselect Attribute must be pass as string i.e 13,14 and 16 are attribute option's id


$product = Mage::getModel('catalog/product');
$product->setSku($sku);
$product->setName($name);
$product->setDescription($description);
$product->setShortDescription($shortdescription);
//$product->setUrlKey($data['5']);    //    Uncomment only if custom url type
$product->setPrice($price);
$product->setSpecialPrice($specialprice);
$product->setSpecialFromDate($specialfromdate);
$product->setSpecialToDate($specialtodate);
$product->setTypeId('simple');
$product->setAttributeSetId(4); // enter the catalog attribute set id here
//$product->addImageToMediaGallery($imagepath,'image',true,false); // absolute path of image in local file system
$product->setCategoryIds($categoryids); // id of categories
$product->setWeight(1.0);
$product->setTaxClassId($taxClassId);
$product->setVisibility($visibility);
$product->setStatus($productStatus);
$product->setColor($color);
$product->setBrand($brand);
$product->setStockData(
array(
'manage_stock' => 1,
'is_in_stock' => 1,
'qty' => 100
)
);
// assign product to the default website
$product->setWebsiteIds(array(Mage::app()->getStore(true)->getWebsite()->getId()));
//$product->setCreatedAt($createdDate);    //    uncomment if add custom date
//$product->setUpdatedAt($updatedDate);    //    uncomment if add custom date
try
{
/*  Add custom options */
$sizes = array(M,L,XL,XXL);
if(count(array_filter($sizes)) > 0){
$options = array();
$optionData = array();
for($i = 0; $i < count($sizes); $i++){
$options[$i]['is_delete'] = '';
$options[$i]['title'] = $sizes[$i];
$options[$i]['price_type'] = 'fixed';
$options[$i]['price'] = '';
$options[$i]['sku'] = '';
}

$optionData = array(
'is_delete'         => 0,
'is_require'        => false,
'previous_group'    => '',
'title'             => 'Size',
'type'              => 'drop_down',
'sort_order'        => 1,
'values'            => $options
);

$optionInstance = $product->getOptionInstance()->unsetOptions();

$product->setHasOptions(1);
$optionInstance->addOption($optionData);
$optionInstance->setProduct($product);

}
/*  ------------------- */
$product->getResource()->save($product);
echo $product->getId().'  Save Successfully';
}
catch (Exception $ex) {
echo $ex->getMessage();
//Handle the error
}

2 个答案:

答案 0 :(得分:0)

我首先要确保已创建该属性并包含您尝试以编程方式添加的所有自定义选项。否则,产品可能会尝试创建不存在的选项/值。

https://www.mymagento.com/index.php/admin/catalog_product_attribute/

答案 1 :(得分:0)

我知道这大约5年了……

TL / DR: 通过使用$product->getResourceModel()->save(),您将跳过负责在产品上保存自定义选项的代码。如果您使用$product->save(),它将在Mage_Catalog_Model_Product _afterSave()方法期间保存您的自定义选项。

长版: 在产品保存期间保存自定义选项的代码实际上是在Mage_Catalog_Model_Product的_afterSave()(版本1.9.4.3中为第546行)方法中定义的,该方法在$product->save()(在Mage_Core_Model_Abstract中定义)中调用,如果您遵循继承链, ,您将发现Mage_Catalog_Model_Product最终继承自)。因此,直接进入资源模型($product->getResourceModel()->save()),您实际上是在跳过负责保存这些自定义选项的代码。

相关问题