Magento Cart: Check for items empties cart - session

I am new to Magento, but I thought I had a grasp on it until today. Here is my problem.
I am writing a new Observer to add a coupon to the cart on page load. The coupon code is passed in the URL and I desire the code to be passable through ANY working URL.
For example: http://magento/?coupon=MYCOUPON
I am catching on the event "controller_front_init_routers" to capture the coupon code.
I have the observer working, but if I already have an item in the cart and I pass a coupon code my cart appears empty, here is how I am adding the coupon:
public function applyCoupon($observer){
$coupon_code = $observer->getEvent()->getData('front')->getRequest()->getParam('coupon');
if(!empty($coupon_code)){
Mage::getSingleton('checkout/session')->setData('coupon_code', $coupon_code);
Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($coupon_code)->save();
Mage::log('Coupon Code: '. $coupon_code);
}
}
It seems that anytime I call Mage::getSingleton('checkout/session')->anything() I lose the session for the cart.
I thought maybe I simply needed to fetch the current cart ID and load it, but I can't seem to find a way to do that either.
Has any one had experience in this, maybe has a solution?

The problem is in event, that you are observing. Because Magento session wasn't initialized at that moment, so cookie has different name, than core one.
Use controller_action_predispatch for setting up some session data from request.

Related

Capturing Add To Cart Event

I am looking for a solution about how to call a javascript function when an item is added to cart on shopify via ajax. Product can be added from homepage, collection-page or product-page.
I am trying with this method but not working for me. These changes i am doing within the theme files "theme.liquid, product.liquid, collection.liquid"
Everytime product go to cart alert should done.
Shopify.onItemAdded = function(line_item) {
alert('success');
};
You'll want to use CartJs... is does exactly what you need and has events etc for when things are added to cart

What are the default observers of checkout_cart_product_add_after?

I am new to Magento.
I am modifying the prices in reloadPrice() method of options.phtml.
But they do not reflect in the cart after "Add to Cart"
I looked around and learnt that I need to observe the checkout_cart_product_add_after event and do the price modifications there to get the correct prices in the cart.
However, in my code base I was not able to find any observer to checkout_cart_product_add_after event. Also the functions from where checkout_cart_product_add_after is fired(in class Mage_Checkout_Model_Cart) doesn't seem to be called.
What am I missing?
Take a look at price.phtml there is a lot to help you out. Almost everything related to price is there.

Magento customer_register_success Observer, get referral page

I have an observer in Magento that fires whenever the customer registers a new account. What I would like to do is find out if that registration came from checkout, or from the normal registration page. What can I call from the observer to find which page the registration referral came from internally?
You could save the last X pageviews in magento registry by putting something like this into some PHP code which is executed each time.
$urlHistory = (array) Mage::getSingleton('core/session')->getMyUrlHistory();
while (is_array($urlHistory) && count($urlHistory) > 3) {
array_shift($urlHistory);
}
$urlHistory[] = Mage::helper('core/url')->getCurrentUrl();
Mage::getSingleton('core/session')->setMyUrlHistory($urlHistory);
You can then analyse Mage::getSingleton('core/session')->getMyUrlHistory() within your observer.

Magento quote items modify sku

I need to change SKU on quoteitems and I do it like this:
foreach($items as $item){
$item->setSku($newSku);
$item->save();
}
But magento always resets SKUs into their original values. Is there some easy way to change it, like superMode on prices? Or if not, then where can I find methods to rewrite, so that they ignore my custom SKUs?
Tried looking for it, but even if I remove cart init and save from index action, they still reset somewhere.
I need it to work in Magento 1.7-1.8
Ok, so I didnt feel like waiting and found it.
You can find it in Mage_Sales_Model_Quote_Item and there in function setProduct. This function is run every time the cart is shown and it resets the data from Product to Item.
Not sure if now my data will get through whole checkout process, will update the answer if not.
EDIT
This has solved only cart, will need to find also function for order. Will update when I get it.
EDIT
In the end, I don't advise you to do this. But if you need it then, the way to do it is to create observe on event sales_order_save_after and modify sku of order items. This seems to work everywhere, only in email after checkout it doesn't work, there you have to modify email. Also I use this only on configurable products.
I do the same thing on my project using sales_quote_collect_totals_after
Just change quote item SKU as you want using this event.

How to pre-fill a shopping cart in Magento?

What's the easiest way to pre-fill a shopping cart in Magento?
There is the CartController on the Checkout route, which has an "add" method, allowing you to do stuff like:
http://<shopurl>/checkout/cart/add?product=1
But what to do when you need more items in the cart? There is an "addGroup" method, but that only takes into account previous order lines.
When digging into the Checkout/CartController/addAction, there appears to be an argument "related_product". It enables you to do this:
http://<shopurl>/checkout/cart/add?product=1&related_product=2,4
Downsides are you only get a message about the main product added, and for the related products you cannot specify quantities. Upside is, you can populate a cart like this with several items at once without even touching Magento code.
If you want to add more items of the same, just repeat the id in the array

Resources