Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is $address->hasCouponCode() always returning null?

For some reason, the magento website does not apply the coupon codes. It always returns an invalid:

Coupon code is not valid

message. However, strangely enough, this happens when the price of cart is larger then 120 my currency.

If I have one product in cart with the price of 65, the coupon code works OK, but if I have the same product in cart but with qty 2 (which means the total price becomes 130) I get the above mentioned invalid error message

I've traced down the code to Mage_Sales_Model_Quote class, which has a function named_validateCouponCode()`, inside where the following:

$address->hasCouponCode() 

Always returns false. What should I try?. I ahve reindexed, refreshed cache, etc., but nothing has changed and I can't seem to find in address model the hasCouponCode function to see what happens in there...

like image 361
Fellner Arthur Avatar asked Aug 16 '13 09:08

Fellner Arthur


2 Answers

First you need to understand how $address->hasCouponCode() works.

There is no such method it is called using magic function __call.

You can not directly access it because $_data is protected.

Now, best way to debug is by checking whether you have coupon set in your address object.

$data = $address->getData();
$coupon = $data['coupon_code'];

Now check whether coupon is set or not in the case when you have 2 items in your cart.

Another way is to try adding coupon using setter as

$address->setCouponCode('Your Coupon Code');

or

$address->setData('coupon_code', 'Your Coupon Code');

and check whether this works or not. If it do not then look into

_validateCouponCode in Mage_Sales_Model_Quote

Will be a good start to debug.

like image 137
rajatsaurastri Avatar answered Oct 31 '22 06:10

rajatsaurastri


Well, without your exact rules it's hard to give a full answer.

First, the Mage_Sales_Model_Quote::_validateCouponCode() call has a single job of removing the coupon code from the quote if the coupon code is not on the quote address. This is because during the collectTotal call the discount total model will copy the coupon code to the address IF it was valid and applied. So, the fact that $address->hasCouponCode() is returning null in this case is just an indicator that the rule validator refused to apply the supplied coupon code.

Second, there are two methods that you should trace out to find out why your coupon is not being applied. Likely it's only one or the other depending on your coupon, but I'll point out both. Mage_SalesRule_Model_Validator::process() and Mage_SalesRule_Model_Validator::processShippingAmount() are the two places that will check your coupon code and possibly apply it and copy the code to the address.

As a side note, Mage_SalesRule_Model_Validator::_maintainAddressCouponCode() is the method responsible for copying the coupon code from the validator (who got the code from the quote) to the address. If this method is never called then your coupon code will always be removed from the quote.

tl;dr

Step debug Mage_SalesRule_Model_Validator::process() and Mage_SalesRule_Model_Validator::processShippingAmount() to debug why your coupon code is not valid.

like image 45
Lee Saferite Avatar answered Oct 31 '22 05:10

Lee Saferite