0

I have a cron job to set product price dynamically.

If I set price by below code then product view page display updated price but updated price not showing on listing page for product :

$_product = Mage::getModel('catalog/product')->setStoreId()->load($product_id);
$Price = $dynamic;
$_product->setPrice($Price)->getResource()->saveAttribute($_product, 'price');

When I used below code then getting exception :

$_product = Mage::getModel('catalog/product')->setStoreId()->load($product_id);
$Price = $dynamic;
$_product->setPrice($Price);
$_product->save();

Exception is :

Fatal error: Uncaught exception 'Mage_Eav_Model_Entity_Attribute_Exception' with message 'SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '2207-0-3-0' for key 'CC12C83765B562314470A24F2BDD0F36', query was: INSERT INTO `catalog_product_entity_group_price` (`entity_id`, `all_groups`, `customer_group_id`, `value`, `website_id`) VALUES (?, ?, ?, ?, ?)' in /home/cityretails/public_html/app/code/core/Mage/Core/Model/Config.php:1348 Stack trace: #0 /home/cityretails/public_html/app/Mage.php(463): Mage_Core_Model_Config->getModelInstance('eav/entity_attr...', 'SQLSTATE[23000]...') #1 /home/cityretails/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php(661): Mage::getModel('eav/entity_attr...', 'SQLSTATE[23000]...') #2 /home/cityretails/public_html/app/code/core/Mage/Eav/Model/Entity/Abstract.php(1660): Mage_Eav_Model_Entity_Abstract->walkAttributes('backend/afterSa...', Array) #3 /home/cityretails/public_html/app/code/core/Mage/Catalog/Model/Resource/Product.php(195): Mage_Eav_Model_Entity_Abstract in /home/cityretails/public_html/app/code/core/Mage/Core/Model/Config.php on line 1348

While when I save product from admin it saved successfully. And after save product updated price showing on listing page.

Can anyone tell me what is the problem here?

  • provide full code then we will able to judge what is the problem – Murtuza Zabuawala Sep 24 '16 at 11:45
2

Try below code add store as admin and let me know

    Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
    $_product    = Mage::getModel('catalog/product')->load($product_id);
    $Price = $dynamic;
    $_product->setPrice($Price);
    $_product->save();
  • If its help you then mark as accepted, so other users also take benefit from it – Murtuza Zabuawala Sep 24 '16 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.