clockworkgeek / Magento-Guest-Cookies

Record cart contents and viewing history in cookies for anonymous guests.
11 stars 6 forks source link

Coupon code #3

Closed gety9 closed 6 years ago

gety9 commented 6 years ago

Daniel hi,

It's not an issue but more a question

By default cart cookie is created when product is in the cart (or added to cart)

Is there a way to make cart cookie be also created when coupon is applied.

We apply coupon to our guest users carts' programmatically when they come from specific link using this code:

Mage::getSingleton("checkout/session")->setData("coupon_code",$coupon_code);
Mage::getSingleton('checkout/cart')->getQuote()->setCouponCode($coupon_code)->save();
Mage::getSingleton('core/session')->setMiscellaneous_Scripts('script'); 

So sometimes coupon code is applied, but guest did not add product to cart yet. (And if he leave before adding to cart, coupon is lost). So i would like to create cart cookie for this situation too.

clockworkgeek commented 6 years ago

This extension only checks that the quote object exists. If it doesn't exist after a coupon is applied then it is Magento's core behaviour at fault.

Perhaps there is another way. Have your code set a "coupon" cookie when someone uses the specific link. If a visitor already has this cookie and no matching session value, then add it to the session again. That is essentially how this extension works so you can use it as a guide. The "coupon" cookie can be safely removed some time during checkout.

clockworkgeek commented 6 years ago

Correction; this extension checks for a quote ID. Please make sure this ID is in checkout/session object as a result of the coupon being applied.

gety9 commented 6 years ago

Thank you Daniel, you are very helpful.

clockworkgeek commented 6 years ago

I guess one of those two suggestions worked for you.

gety9 commented 6 years ago

Daniel, we are still working on it, you just pointed us in the right direction :)

How would you recommend to exclude coupon from your cart cookie?

We came to the solution to have 2 separate cookies - coupon and cart.

Coupon cookie will be created/renewed only when visiting this special url i've mentioned above.

What can happen is the following situation: guest adds product to cart when coupon cookie expires in 1 day, when adding to cart cart cookie is created with life time 1 month. Guest does not finish the purchase, leaves and returns in 2 days. Coupon cookie is expired, BUT coupon is still in quote in cart cookie.

clockworkgeek commented 6 years ago

I suppose the same problem could occur for registered users who are returning to their cart after several days, or any user who is online 24 hours later and the coupon expires during their session. The common fix to all scenarios would be to watch the sales_quote_load_after event and check all quote objects for expired coupons. Your coupon cookie already has an expiration date built in but cookies can get lost for various reasons, ditto for the local cache. The ideal action would be to confirm each quote's coupon with the database.

I notice the database contains a table salesrule_coupon which has an expiration_date for each individual coupon code. Maybe that's involved somehow..?

gety9 commented 6 years ago

Thank you for quick reply,

"Your coupon cookie already has an expiration date built in but cookies can get lost for various reasons, ditto for the local cache. The ideal action would be to confirm each quote's coupon with the database"

The thing is coupon itself never expires, it's coupon cookie expiration we are focused on. It's kind of mini affiliate/referral system. And we use those coupons to monitor who send us people/sales.

I guess what we can do as you've suggested to watch the sales_quote_load_after, check if coupon cookie exists/not expired and if it does not exist/expired - remove the coupon if it's there.

clockworkgeek commented 6 years ago

In that case you only need to match the coupon cookie to each quote object during the aforementioned event. If that proves inadequate (such as customers complaining) then you can make the more complicated method of recording when each user uses their coupon and then check that instead of any cookie.

gety9 commented 6 years ago

Thank you for your suggestion, we will try this approach.