Set product attribute value in magento errors - magento

I'm writing an importer that creates new attribute (which works) and now I want to set different attribute values for different products, without using attribute sets. A method that seems to work for a lot of people seems to be the following:
Mage::getSingleton('catalog/product_action')
->updateAttributes([$id], ['color'=>'red'], self::STORE_ID);
Sadly this errors for me:
PHP Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42S22]: Column not found: 1054 Unknown column 'attribute_id' in 'where clause'' in /vagrant/htdocs/lib/Zend/Db/Statement/Pdo.php:228
Stack trace:
#0 /vagrant/htdocs/lib/Zend/Db/Statement/Pdo.php(228): PDOStatement->execute(Array)
#1 /vagrant/htdocs/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)
#2 /vagrant/htdocs/app/code/core/Zend/Db/Statement.php(291): Varien_Db_Statement_Pdo_Mysql->_execute(Array)
#3 /vagrant/htdocs/lib/Zend/Db/Adapter/Abstract.php(480): Zend_Db_Statement->execute(Array)
#4 /vagrant/htdocs/lib/Zend/Db/Adapter/Pdo/Abstract.php(238): Zend_Db_Adapter_Abstract->query('DELETE FROM `ca...', Array)
#5 /vagrant/htdocs/lib/Varien/Db/Adapter/Pdo/Mysql.php(428): Zend_Db_Adapter_Pdo_Abstract->query('DELETE FROM `ca...', Array)
#6 /vagrant/htdocs/lib/Zend/Db/Adapter/Abstract.php(664): Varien_Db_Adapter_Pdo_Mysql->query('DELETE FROM `ca...')
#7 /vagrant/htdocs/app/code/core/Mage/Catalog/Model/Resource/Abstract.p in /vagrant/htdocs/lib/Zend/Db/Statement/Pdo.php on line 235
Is there a better method or am I just doing something wrong?

In order to save attribute values for product, you have to load product object first then set attribute values.. See the example below:
$id = 102; // Your product id
$product = Mage::getModel("catalog/product")->load($id);
$product->setStatus(1);
$product->setColor("red");
$product->save();

As I have learned a very good and performant way is to work with the Resource model:
$product = Mage::getModel('catalog/product');
$product->setId($productId);
$product->setStoreId($storeId);
$product->setColor('red');
// or
// $product->setDataUsingMethod('color', 'red')
$resource = Mage::getResourceModel('catalog/product');
$resource->saveAttribute($product, 'color');

Related

Fatal Error When Set CI_DEBUG to False On Production Mode Codeigniter 4

Why I get this error when set from Config/Boot/production.php CI_DEBUG to false in production mode ? this is the default setting from CI4.
defined('CI_DEBUG') || define('CI_DEBUG', false);
Fatal error: Declaration of CodeIgniter\Log\Logger::emergency($message, array $context = []): bool must be compatible with Psr\Log\LoggerInterface::emergency(Stringable|string $message, array $context = []): void in E:\testing\Source Code\testing_CI_v.4.2.6\system\Log\Logger.php on line 157
Fatal error: Uncaught Error: Class "CodeIgniter\Log\Logger" not found in E:\testing\Source Code\testing_CI_v.4.2.6\system\Config\Services.php:391 Stack trace: #0 E:\testing\Source Code\testing_CI_v.4.2.6\system\Config\BaseService.php(253): CodeIgniter\Config\Services::logger(false) #1 E:\testing\Source Code\testing_CI_v.4.2.6\system\Config\BaseService.php(194): CodeIgniter\Config\BaseService::__callStatic('logger', Array) #2 E:\testing\Source Code\testing_CI_v.4.2.6\system\Config\Services.php(388): CodeIgniter\Config\BaseService::getSharedInstance('logger') #3 E:\testing\Source Code\testing_CI_v.4.2.6\system\Config\BaseService.php(253): CodeIgniter\Config\Services::logger(true) #4 E:\testing\Source Code\testing_CI_v.4.2.6\system\Common.php(799): CodeIgniter\Config\BaseService::__callStatic('logger', Array) #5 E:\testing\Source Code\testing_CI_v.4.2.6\system\Debug\Exceptions.php(114): log_message('critical', '{message}\nin {e...', Array) #6 E:\testing\Source Code\testing_CI_v.4.2.6\system\Debug\Exceptions.php(180): CodeIgniter\Debug\Exceptions->exceptionHandler(Object(ErrorException)) #7 [internal function]: CodeIgniter\Debug\Exceptions->shutdownHandler() #8 {main} thrown in E:\testing\Source Code\testing_CI_v.4.2.6\system\Config\Services.php on line 391
For Development And Testing Mode run without problem. This error just occur when using routes->resource (RESTFUL API).
It is not clear what error is this. I try to replace the system folder with new one. But the error still exists.
It is said "Uncaught Error: Class "CodeIgniter\Log\Logger" not found" I don't know what is the problem with this and why it is not found.
Now my app just run with set CI_DEBUG to true to prevent the error. But the default is set to false for production.
defined('CI_DEBUG') || define('CI_DEBUG', true);
What is missing in here ? It is so strange that setting the CI_ENVIRONMENT to development or testing worked without a problem. But for production not working. So I decide to turn on the error display in production mode and found the error above.
Seriously help need.
I found the problem in the restful api.
The problem is in the config/filter.php
$routes->resource('ApiManageTips', ['controller' =>'App\Controllers\ApiData\ApiManageTips']); // get, put, create, delete
public $filters = [
'basicauth' => ['before' =>
[
'ApiManageTips/*',
'ApiManageTips',
]
]
];
The Above example is working in the version 4.1.2 but is not working in version 4.2.6
Instead change to :
public $filters = [
'basicauth' => ['before' =>
[
'ApiManageTips/*',
]
];
I don't why it is not working in the CI Version 4.2.6.

Magento error: Invalid backend model specified: customer_entity/address_attribute_backend_region

After updating my magento instance from 1.4.0 to 1.7.0.2 I've got the following error:
Invalid backend model specified: customer_entity/address_attribute_backend_region
The error occurs during the checkout process while the customer is selecting the adress from drop-down list and press the "continue" button.
A AJAX-request to /checkout/onepage/saveBilling/ fails, the second request to checkout/onepage/getAdditional responses. The customer will be redirected to the cart overview page, but content the failed ajax response displays an error report number, which can be looked up at with FTP in the folder var/reports/####reportnumber###.log
EDIT: Stack trace
a:5:{i:0;s:81:"Invalid backend model specified: customer_entity/address_attribute_backend_region";i:1;s:1907:"#0 /home/www/p10000/html/magento/app/code/core/Mage/Eav/Model/Entity/Attribute/Abstract.php(346): Mage::exception('Mage_Eav', 'Invalid backend...')
#1 /home/www/p10000/html/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php(1094): Mage_Eav_Model_Entity_Attribute_Abstract->getBackend()
#2 /home/www/p10000/html/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php(998): Mage_Eav_Model_Entity_Abstract->_setAttributeValue(Object(Mage_Customer_Model_Address), Array)
#3 /home/www/p10000/html/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php(959): Mage_Eav_Model_Entity_Abstract->_loadModelAttributes(Object(Mage_Customer_Model_Address))
#4 /home/www/p10000/html/magento/app/code/core/Mage/Core/Model/Abstract.php(225): Mage_Eav_Model_Entity_Abstract->load(Object(Mage_Customer_Model_Address), '209', NULL)
#5 /home/www/p10000/html/magento/app/code/core/Mage/Checkout/Model/Type/Onepage.php(246): Mage_Core_Model_Abstract->load('209')
#6 /home/www/p10000/html/magento/app/code/core/Mage/Checkout/controllers/OnepageController.php(320): Mage_Checkout_Model_Type_Onepage->saveBilling(Array, '209')
#7 /home/www/p10000/html/magento/app/code/core/Mage/Core/Controller/Varien/Action.php(419): Mage_Checkout_OnepageController->saveBillingAction()
#8 /home/www/p10000/html/magento/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('saveBilling')
#9 /home/www/p10000/html/magento/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#10 /home/www/p10000/html/magento/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#11 /home/www/p10000/html/magento/app/Mage.php(686): Mage_Core_Model_App->run(Array)
#12 /home/www/p10000/html/magento/index.php(101): Mage::run('', 'store')
#13 {main}";s:3:"url";s:60:"/magento/index.php/checkout/onepage/saveBilling/";s:11:"script_name";s:18:"/magento/index.php";s:4:"skin";s:11:"german";}
I found this topic: http://www.magentocommerce.com/boards/viewthread/261499/#t350593
So I created this statement (the first two lines I found at learningmagento.com, but they are incomplete and leads into a total break of the onepage/saveBilling-Page, if you don't change the source model, too.
UPDATE `eav_attribute` SET `backend_model` = 'customer/entity_address_attribute_backend_street' WHERE `attribute_code` ='street' LIMIT 1 ;
UPDATE `eav_attribute` SET `backend_model` = 'customer/entity_address_attribute_backend_region' WHERE `attribute_code` ='region' LIMIT 1 ;
UPDATE `eav_attribute` SET `source_model` = 'customer/entity_address_attribute_source_country' WHERE `source_model` = 'customer_entity/address_attribute_source_country' LIMIT 1 ;
UPDATE `eav_attribute` SET `source_model` = 'customer/entity_address_attribute_source_region' WHERE `source_model` ='customer_entity/address_attribute_source_region' LIMIT 1 ;
It worked for me. The AJAX request responses with a validation error for the phone number in my case, but this can be solved easily.
The "customer_entity/..." can be also found in the table "eav_entity_type", but in my case it was (not yet) necessary to change the value.

error when using createEntityTables

I get this error when I include
$installer->createEntityTables( $this->getTable('red/red') );
I am using enterprise 1.11
I have also seen alternative to that is type every thing which is really time consuming. Could someone please tell me who to make this function work.
[previous:Exception:private] =>
[xdebug_message] => ( ! ) Mage_Eav_Exception: Can't create table: red_faqs_eavexample in C:\wamp\www\ubt.onlocal.com.au\app\Mage.php on line 549
Call Stack
#TimeMemoryFunctionLocation
10.0003690528{main}( )..\index.php:0
20.00271167384Mage::run( )..\index.php:81
30.01012776112Mage_Core_Model_App->run( )..\Mage.php:640
40.02304545784Mage_Core_Model_App->_initModules( )..\App.php:338
50.46364871080Mage_Core_Model_Resource_Setup::applyAllUpdates( )..\App.php:412
60.528411772936Mage_Core_Model_Resource_Setup->applyUpdates( )..\Setup.php:235
70.528611769664Mage_Core_Model_Resource_Setup->_installResourceDb( )..\Setup.php:327
80.528611769824Mage_Core_Model_Resource_Setup->_modifyResourceDb( )..\Setup.php:421
90.529511778144include( 'C:\wamp\www\ubt.onlocal.com.au\app\code\local\Magelocal\Red\sql\red_setup\install-0.1.0.php' )..\Setup.php:624
100.529711778712Mage_Eav_Model_Entity_Setup->createEntityTables( )..\install-0.1.0.php:6
)
Error in file: "C:\wamp\www\ubt.onlocal.com.au\app\code\local\Magelocal\Red\sql\red_setup\install-0.1.0.php" - Can't create table: red_faqs_eavexample
#0 C:\wamp\www\ubt.onlocal.com.au\app\code\core\Mage\Core\Model\Resource\Setup.php(645): Mage::exception('Mage_Core', 'Error in file: ...')
#1 C:\wamp\www\ubt.onlocal.com.au\app\code\core\Mage\Core\Model\Resource\Setup.php(421): Mage_Core_Model_Resource_Setup->_modifyResourceDb('install', '', '0.1.0')
#2 C:\wamp\www\ubt.onlocal.com.au\app\code\core\Mage\Core\Model\Resource\Setup.php(327): Mage_Core_Model_Resource_Setup->_installResourceDb('0.1.0')
#3 C:\wamp\www\ubt.onlocal.com.au\app\code\core\Mage\Core\Model\Resource\Setup.php(235): Mage_Core_Model_Resource_Setup->applyUpdates()
#4 C:\wamp\www\ubt.onlocal.com.au\app\code\core\Mage\Core\Model\App.php(412): Mage_Core_Model_Resource_Setup::applyAllUpdates()
#5 C:\wamp\www\ubt.onlocal.com.au\app\code\core\Mage\Core\Model\App.php(338): Mage_Core_Model_App->_initModules()
#6 C:\wamp\www\ubt.onlocal.com.au\app\Mage.php(640): Mage_Core_Model_App->run(Array)
#7 C:\wamp\www\ubt.onlocal.com.au\index.php(81): Mage::run('', 'store')
#8 {main}
This is a problem with an error being triggered when it shouldn't be. Long story short, MySQL doesn't support DDL transactions, and in the course of the method DDL routines are being found in the SQL and an error is thrown.
The easy answer is to comment out this line:
lib/Varien/Db/Adapter/PDO/Mysql.php
protected function _checkDdlTransaction($sql)
{
if (is_string($sql) && $this->getTransactionLevel() > 0) {
$startSql = strtolower(substr(ltrim($sql), 0, 3));
if (in_array($startSql, $this->_ddlRoutines)) {
// comment this out: trigger_error(Varien_Db_Adapter_Interface::ERROR_DDL_MESSAGE, E_USER_ERROR);
}
}
}
Doing so allows your module to install. Obviously hacking a core file is a terrible idea. You should extend the method or allow the SQL to run, and then translate the generated SQL into the Magento MySQL API, as seen in core sql setup scripts. The latter is a major pain.. the better idea would be to extend the method.
More here on background information and troubleshooting.

Programmatic Setting of Item's setSpecialPrice Doesn't Stick

I'm trying to programmaticly add an item to the visitor's shopping cart. In the process, the item is given a custom price. On the page initially displayed after item addition, the "My Cart" summary (in the right column) indicates that the cart contains an item with the correct price. However, when I go to the view cart page, the item is listed with its default price (instead of the custom price).
How do I make the custom price stick?
Thank you,
Ben
Here's the code:
$product = Mage::getModel('catalog/product');
$product->load(169);
$product->setSpecialPrice(10);
$cart = Mage::getSingleton('checkout/cart');
$cart->addProduct($product, array('qty' => 1, 'options' => array(5 => 'spec goes here' . time())));
$cart->save();
I tried calling $product->save() after $product->setSpecialPrice() but that resulted in:
Warning: Invalid argument supplied for foreach() in app\code\core\Mage\Eav\Model\Entity\Abstract.php on line 1068
#0 app\code\core\Mage\Eav\Model\Entity\Abstract.php(1068): mageCoreErrorHandler(2, 'Invalid argumen...', 'C:\Program File...', 1068, Array)
#1 app\code\core\Mage\Eav\Model\Entity\Abstract.php(1012): Mage_Eav_Model_Entity_Abstract->_collectSaveData(Object(Mage_Catalog_Model_Product))
#2 app\code\core\Mage\Core\Model\Abstract.php(318): Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Catalog_Model_Product))
#3 app\code\local\BenGribaudo\RazaOrderGrid\controllers\IndexController.php(13): Mage_Core_Model_Abstract->save()
#4 app\code\core\Mage\Core\Controller\Varien\Action.php(418): BenGribaudo_RazaOrderGrid_IndexController->addAction()
#5 app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(253): Mage_Core_Controller_Varien_Action->dispatch('add')
#6 app\code\core\Mage\Core\Controller\Varien\Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#7 app\code\core\Mage\Core\Model\App.php(340): Mage_Core_Controller_Varien_Front->dispatch()
#8 app\Mage.php(627): Mage_Core_Model_App->run(Array)
#9 index.php(80): Mage::run('', 'store')
#10 {main}
Set Special Price Programatically In Magento

Magento model collections - save data

So I'm loading a collection of products using this code:
$magento_time= 'some time string';
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addFieldToFilter(array(
array('attribute'=> 'updated_at', 'gt'=> date('Y-m-d H:i:s', strtotime($magento_time))),
));
$collection->save();
And getting this error in return on save:
Warning: Invalid argument supplied for foreach() in /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 970
#0 /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php(970): mageCoreErrorHandler(2, 'Invalid argumen...', '/var/www/magent...', 970, Array)
#1 /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Abstract.php(925): Mage_Eav_Model_Entity_Abstract->_collectSaveData(Object(Mage_Catalog_Model_Product))
#2 /var/www/magento/app/code/core/Mage/Core/Model/Abstract.php(251): Mage_Eav_Model_Entity_Abstract->save(Object(Mage_Catalog_Model_Product))
#3 /var/www/magento/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php(845): Mage_Core_Model_Abstract->save()
#4 /var/www/magento/app/code/local/MyModule/controllers/IndexController.php(16): Mage_Eav_Model_Entity_Collection_Abstract->save()
#5 /var/www/magento/app/code/core/Mage/Core/Controller/Varien/Action.php(376): MyModule_IndexController->sayHelloAction()
#6 /var/www/magento/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(248): Mage_Core_Controller_Varien_Action->dispatch('sayHello')
#7 /var/www/magento/app/code/core/Mage/Core/Controller/Varien/Front.php(158): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#8 /var/www/magento/app/Mage.php(459): Mage_Core_Controller_Varien_Front->dispatch()
#9 /var/www/magento/index.php(68): Mage::run()
#10 {main}
How do I save products back to the database after they've been loaded into a collection?
Changing the following worked for me:
Manually set the current store:
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
Save each collection independently.
foreach ($collection as $entry) {
$entry->save();
}
EDIT:
Note the Magento API has changed significantly since I first asked this question ~2.5 years ago - this answer may no longer apply.
You do a foreach and save elements one by one.

Resources