无法以编程方式向产品添加自定义选项
Can't add custom option to product programmatically
我有创建产品然后向其添加自定义选项的代码。
但是,如果我在管理面板中打开它,则不会添加任何自定义选项。此外,不会生成任何错误消息或警告。我的 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
}
我首先要确保该属性已创建并且包含您尝试以编程方式添加的所有自定义选项。否则,该产品可能会尝试创建 options/values 不存在的内容。
https://www.mymagento.com/index.php/admin/catalog_product_attribute/
我知道这已经晚了大约 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()
),您实际上跳过了负责保存这些自定义选项的代码。
我有创建产品然后向其添加自定义选项的代码。 但是,如果我在管理面板中打开它,则不会添加任何自定义选项。此外,不会生成任何错误消息或警告。我的 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
}
我首先要确保该属性已创建并且包含您尝试以编程方式添加的所有自定义选项。否则,该产品可能会尝试创建 options/values 不存在的内容。
https://www.mymagento.com/index.php/admin/catalog_product_attribute/
我知道这已经晚了大约 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()
),您实际上跳过了负责保存这些自定义选项的代码。