Onclick Add to cart button we are trying to update the Attribute value from "NO" to "YES".
[ attribute code : cart_status
(Yes/No dropdown) Attribute ]
I tried below code, its changing attribute values for all products, but i need to change value only for that particular product that i added to cart.
<?php
foreach ($collections as $key => $product)
{
echo $product->getName().' <br><br>';
$_product = Mage::getModel('catalog/product')->load($product->getId());
?>
<form action="<?php
echo Mage::helper('checkout/cart')->getAddUrl($product);
// update cart_status
echo "\n".'updating '.$_product->getSku()."...\n";
$_product->setData('cart_status', 1)->getResource()->saveAttribute($_product, 'cart_status');
$_product->save();
// update cart_status end
?>" method="post">
<div>
<button>Add to cart</button>
</div>
</form>
Update
After following Dhiren Vasoya's Solution , once i click on Add to cart
, In 1)Frontend its changing Attribute value [From "NO" to "YES" , but once i refresh page, again it showing old value [ NO ] , But in 2)In Backend, updated Values are not showing for "Default Values" , but showing for "Stores"
Default values :
Stores :
config :
<checkout_cart_product_add_after>
<observers>
<example_add_to_cart_after>
<class>Amasty_Example_Model_Observer</class>
<method>AddToCartAfter</method>
</example_add_to_cart_after>
</observers>
</checkout_cart_product_add_after>
app/code/local/Amasty/Example/Model/Observer.php
class Amasty_Example_Model_Observer
{
public function AddToCartAfter($observer)
{
$product = $observer->getEvent()->getProduct();
$product->setCartStatus(1);
$product->save();
}
}
Recoverable Error: Argument 3 passed to Mage_Catalog_Model_Resource_Abstract::_canUpdateAttribute() must be of the type array, null given, called in /app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 1225 and defined in /app/code/core/Mage/Catalog/Model/Resource/Abstract.php on line 543