1

I have 4 products with IDs:

FreeSilver = 9575
Silver = 9576
Gold = 9577
Platinum = 9578

If anyone adds those specific items I want to run different codes to update customer attributes.

is checkout_cart_product_add_after EVENT observer the best way to do this?

Thanks.!

1 Answer 1

1

I got it! Let me know if anyone needs to do something similar to this and I'll do my best to help out and respond!

First off I activated my module: app/etc/Sean_DaysToCart.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Sean_DaysToCart>
            <active>true</active>
            <codePool>local</codePool>
        </Sean_DaysToCart>
    </modules>
</config>

Then we create the config file: app/code/local/Sean/DaysToCart/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Sean_DaysToCart>
            <version>0.1.0</version>
        </Sean_DaysToCart>
    </modules>
    <global>
        <models>
            <sean_testmsg>
                <class>Sean_DaysToCart_Model</class>
            </sean_testmsg>
        </models>
        <events>
            <checkout_cart_product_add_after>
                <observers>
                    <Sean_DaysToCart_Model_Observer>
                        <type>singleton</type>
                        <class>Sean_DaysToCart_Model_Observer</class>
                        <method>addtocartEvent</method>
                    </Sean_DaysToCart_Model_Observer>
                </observers>
            </checkout_cart_product_add_after>
        </events>
    </global>
</config>

Then we create the observer/listener for when item is added to cart: app/code/local/Sean/DaysToCart/Model/Observer.php

<?php
class Sean_DaysToCart_Model_Observer {

    public function addtocartEvent(Varien_Event_Observer $observer){
        $customer = Mage::getSingleton('customer/session')->getCustomer();
        $event = $observer->getEvent();
        $product = $event->getProduct();
        $productId = $product->getId();
        $Qty = $product->getQty();

        if($productId == 9575 || $productId == 9575){
            $remaining = $customer->getData('silver');
            $total = ($Qty * 365) + $remaining;
            $customer->setData('silver', $total);
            $customer->save();
        }
        elseif($productId == 9577){
            $remaining = $customer->getdata('gold');
            $total = ($Qty * 365) + $remaining;
            $customer->setData('gold', $total);
            $customer->save();
        }
        elseif($productId == 9578){
            $remaining = $customer->getdata('platinum');
            $total = ($Qty * 365) + $remaining;
            $customer->setData('platinum', $total);
            $customer->save();
        }
//        TESTING IN SUCCESS MSG IF PROGRESS WAS MADE
//        echo Mage::getSingleton('checkout/session')->addSuccess($QTY);

    }
}

And it works beautifully! Hopefully you guys can follow along with what I did this method will also work for any functions/code you want to run when items are added to the cart. You can have it run after any item is added or even any specific item like I did :) Happy Coding guys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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