尝试保存/更新产品时,面临以下问题
SQLSTATE [23000]:完整性约束违规:1062重复条目 ' 98763-1' for key' UNQ_CATALOGINVENTORY_STOCK_ITEM_PRODUCT_ID_STOCK_ID
所以我尝试改变
$product->save();
到
$product->getResource()->save();
毫无疑问,它解决了这个问题。
但现在它没有保存自定义属性。我怎么能这样做?
保存产品的类: -
require_once 'Mage/Adminhtml/controllers/Catalog/ProductController.php';
class Fuze_Class_Adminhtml_Catalog_ProductController extends Mage_Adminhtml_Catalog_ProductController {
/**
* Save product action
*/
public function saveAction()
{
$storeId = $this->getRequest()->getParam('store');
$redirectBack = $this->getRequest()->getParam('back', false);
$productId = $this->getRequest()->getParam('id');
$isEdit = (int)($this->getRequest()->getParam('id') != null);
$data = $this->getRequest()->getPost();
if ($data) {
$this->_filterStockData($data['product']['stock_data']);
$product = $this->_initProductSave();
try {
// $product->save();
$product->getResource()->save();
$productId = $product->getId();
/**
* Do copying data to stores
*/
if (isset($data['copy_to_stores'])) {
foreach ($data['copy_to_stores'] as $storeTo=>$storeFrom) {
$newProduct = Mage::getModel('catalog/product')
->setStoreId($storeFrom)
->load($productId)
->setStoreId($storeTo)
->save();
}
}
Mage::getModel('catalogrule/rule')->applyAllRulesToProduct($productId);
$this->_getSession()->addSuccess($this->__('The product has been saved.'));
} catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage())
->setProductData($data);
$redirectBack = true;
} catch (Exception $e) {
Mage::logException($e);
$this->_getSession()->addError($e->getMessage());
$redirectBack = true;
}
}
}
}
答案 0 :(得分:0)
如果使用产品型号保存自定义属性不起作用,另一种方法是使用产品操作资源模型更新属性,
$storeId = 1;
$productId = 1;
$attributesData = array(
'name' => 'New Product Name',
'price' => 50.00
);
$productAction = Mage::getModel('catalog/resource_product_action');
$productAction->updateAttributes(array($productId), $attributes, $storeId);