I've created observer method for salesrule_validator_process
event. How can I access coupon code and whole cart to check, if there are specific products in cart? If it found products with a specific criteria how to disable applying entered coupon?
-
Why just not to disable the products with given criteria in the shopping cart rile?– AmastyAug 18, 2014 at 19:13
2 Answers
The event salesrule_validator_process
passes most of the objects that you are looking for.
Mage::dispatchEvent('salesrule_validator_process', array(
'rule' => $rule,
'item' => $item,
'address' => $address,
'quote' => $quote,
'qty' => $qty,
'result' => $result,
));
Here you have the quote (which is the cart). In your method your can call $observer->getQuote()
.
You can then loop through the items attached to the quote and perform your checks.
In your observer
public function myCouponObserver($event){
//get quote
$quote = $event->getQuote();
Get all item (parent)
$cartItems = $quote->getAllVisibleItems();
foreach ($cartItems as $item) {
//check for sku
if($item->getSku() == 'xyz'){
}
}
To get Coupon
$coupon_code = $quote->getCouponCode();
To remove coupon
$quote->setCouponCode('');
$quote->collectTotals()->save();