开发者

Magento save attribute value programmatically causes SQL error

开发者 https://www.devze.com 2023-03-24 08:16 出处:网络
Could somebody please help? In Magento 1.5.0.1 I am trying to save an attribute value for configurable product. I added my attribute to corresponding products using Admin.

Could somebody please help?

In Magento 1.5.0.1 I am trying to save an attribute value for configurable product. I added my attribute to corresponding products using Admin.

I looked at Modify automatically an attribute with magento and Programmatically change product attribute at store view level, so was ab开发者_JAVA技巧le to come up with seemingly simple code which works unfortunately only sometimes - namely for configurable products without tier prices...

Here is a snippet of code:

$id = 126;  # id of existing product
$product = Mage::getModel('catalog/product')->load($id);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 
$product->setx_pos(123);
// tried this too, does not make a difference:  $product->setData('x_pos',  123);
$product->save();

It works perfectly IF configurable product does not have products with tier prices. It can have tier products without tier prices, it this case save works. If tier products have tier prices – than I am getting the exception:

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '126-1-0-5.0000-0' for key 'UNQ_CATALOG_PRODUCT_TIER_PRICE'
Trace:
#0 C:\magento\app\code\core\Mage\Eav\Model\Entity\Abstract.php(1513):        Mage_Eav_Model_Entity_Abstract->walkAttributes('backend/afterSa...', Array)
#1 C:\magento\app\code\core\Mage\Catalog\Model\Resource\Eav\Mysql4\Product.php(142): Mage_Eav_Model_Entity_Abstract->_afterSave(Object(Mage_Catalog_Model_Product))
#2 C:\magento\app\code\core\Mage\Eav\Model\Entity\Abstract.php(1013):    Mage_Catalog_Model_Resource_Eav_Mysql4_Product->_afterSave(Object  (Mage_Catalog_Model_Product))
#3 C:\magento\app\code\core\Mage\Core\Model\Abstract.php(318):  Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Catalog_Model_Product))
#4 C:\magento\app\design\frontend\default\OneOffFit\template\page\data.phtml(90):  Mage_Core_Model_Abstract->save()

I Googled on this error - the only refrence is unanswered question.

Why is Magento updating tier prices in this case? Am I doing anything wrong? Should I update associated products first (they have same attribute but with unset value)? Or rather what should I do right? Could anybody help please?


Move this line above the load command;

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); 

Or if you're not concerned with store-specific values remove it altogether.


in my case when I put following code just above the product load, the error is gone but browser is taking so long time to stop. Finally the attribute is also not updated.

Any idea what is happening to the code?

$productId = Mage::getModel('catalog/product')->getIdBySku('68366');
$product =  Mage::getModel('catalog/product')->load($productId);
$product->setMyAttribute('test')
    ->save();
0

精彩评论

暂无评论...
验证码 换一张
取 消