1

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 :

enter image description here

Stores :

enter image description here

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();
    }
}
  • i am following this link : stackoverflow.com/questions/15999439/… , same code i am using...... – user52996 Jun 9 '17 at 10:16
  • The link that you provide above is sample code to do a batch save of products (aka an admin product save/bulk update). It seems as if you are trying to update the product attribute of a quote item. What exactly are you trying to do? – Renon Stewart Jun 9 '17 at 14:15
  • @RenonStewart I am trying to update a single product attribute value from frontend. but its updating for all products..... – user52996 Jun 12 '17 at 5:22
  • Have you something in your error logs? – sv3n Jun 12 '17 at 7:45
  • @sv3n log error 1 : 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 – user52996 Jun 12 '17 at 7:56
-1
+100

First you need to add observer throught you extension config.xml.

app/code/CODEPOOL/Vendor/Extension/etc/config.xml

<frontend>
    <events>
        <checkout_cart_product_add_after>
            <observers>
                <extension_add_to_cart_after>
                    <class>YOUR_OBSERVER_CLASS</class>
                    <method>AddToCartAfter</method>
                </extension_add_to_cart_after>
            </observers>
        </checkout_cart_product_add_after>
    </events>
</frontend>

Now you have to code in yorr observer.php file like this :

app/code/CODEPOOL/Vendor/Extension/Model/Observer.php

class Vendor_Extension_Model_Observer extends Mage_Core_Model_Observer
{
    public function AddToCartAfter($observer)
    {
        $product = $observer->getEvent()->getProduct();         
        $product->setCartStatus(1);
        $product->save();
    }
}
  • onclick add to cart button, attribute values are updating now, but once i refresh page, again it show old values only..... – user52996 Jun 12 '17 at 6:31
  • can you clear cache and indexing. – Dhiren Vasoya Jun 12 '17 at 6:32
  • done reindexing & cleared cache, still same issue..... – user52996 Jun 12 '17 at 6:46
0
  1. override your checkout controller there in addAction after add to cart code use your code.

  2. OR create an event observer checkout_cart_product_add_after there you can get the particular product.

For the above process, if you not familiar do google you'll get those information.

  • thanks for reply, is there any easy way to solve this other than those 2 you mentioned in answer..... ? because once i click add to cart attribute value is already changing for all products , i need to change the value only for that particular product...... if there is no other easy solution, i will surely follow yours, please let me know about this..... – user52996 Jun 9 '17 at 13:02
  • Try to pass the product ID onclick the Add to cart event and filter with that ID into your collection. – JAY Jun 12 '17 at 3:50
0

you want to update particular product attribute value which one is getting added to cart. So it will better if you use observer for add to cart. Using that you can update attribute value for the specific product that you are adding to cart.

0

You are better off using event checkout_cart_product_add_after and then utilise the code mentioned here

0

For your error message ... please try this code in your observer method:

$product = $observer->getEvent()->getProduct();
Mage::getSingleton('catalog/product_action')->updateAttributes(
    array($product->getId()),
    array('cart_status' => 1),
    $storeId
);

Note: set $storeId to 0 if you want to save it as default value or use Mage::app()->getStore()->getId() for store view specific value.

  • Thanks, those error messages in log file gone now after updating your code, but the original issue is still there...... – user52996 Jun 12 '17 at 9:10
  • What do you mean? Value is set for all products OR value is not set to "Default Value"? – sv3n Jun 12 '17 at 9:20
  • i set $storeId=0 but still onclick add to cart button , attribute value is updating only for stores [ not for default values] also once we refresh the page in frontend, it show old values only...... – user52996 Jun 12 '17 at 9:26
  • Can you please double-check your code. And remove the "update code" from your original answer. I've tested it and works as intended?!? – sv3n Jun 12 '17 at 9:51
  • Found the issue, when i tried with other dropdown attribute, it worked fine.... but not for this attribute : cart_status...... – user52996 Jun 12 '17 at 9:51
0

Create a custom module

app/code/local/MagePal/UpdateProductOnAddToCart/etc/config.xml

<frontend>
    <events>
        <checkout_cart_product_add_after>
            <observers>
                <magepal_updateproductonaddtocart_add_to_cart_after>
                    <class>MagePal_UpdateProductOnAddToCart_Observer</class>
                    <method>AddToCartAfter</method>
                </magepal_updateproductonaddtocart_add_to_cart_after>
            </observers>
        </checkout_cart_product_add_after>
    </events>
</frontend>

Observer

app/code/local/MagePal/UpdateProductOnAddToCart/Model/Observer.php

class MagePal_UpdateProductOnAddToCart_Observer extends Mage_Core_Model_Observer
{
    public function AddToCartAfter($observer)
    {
        // Set store id to admin
        Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
        $product_id = $observer->getEvent()->getProduct()->getId();
        $store_id = 0;

        $action = Mage::getModel('catalog/resource_product_action');
        $action->updateAttributes(array($product_id), array(
            'cart_status' => 1
        ), $store_id);
    }
}
  • Updated your code, once we click on add to cart , cart_status attribute value is changing to yes and once i refresh page, again its displaying No , but its working fine for other attributes , is there any idea what may be wrong for that particular attribute cart_status [scope : global ] – user52996 Jun 12 '17 at 11:29
  • When you "refresh" what page (admin or cart page)? – Renon Stewart Jun 12 '17 at 11:30
  • i refresh in My account page as we displaying products there... – user52996 Jun 12 '17 at 11:31
  • Global scope should be store id = 0, which you should not be able to save to from the frontend unless you are using Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); – Renon Stewart Jun 12 '17 at 11:32
  • What are you showing in "My account page" products or cart item? – Renon Stewart Jun 12 '17 at 11:36

Your Answer

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