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?
Magento – Apply coupon observer to set message in cart view
cart-rulecouponevent-observer
Related Solutions
I found answer of above question.
Created event of sales_order_status_after
<events>
<sales_order_save_commit_after>
<observers>
<modulename>
<type>singleton</type>
<class>Mycompany_Offer_Model_Observer</class>
<method>sales_order_save_commit_after</method>
</modulename>
</observers>
</sales_order_save_commit_after>
</events>
Added Method sales_order_save_commit_after in observer:
<?php
class Mycompany_Offer_Model_Observer {
public function sales_order_save_commit_after(Varien_Event_Observer $observer)
{
$order = $observer->getOrder();
if($order->getState() == Mage_Sales_Model_Order::STATE_COMPLETE){
$orderTotal = $order->getBaseSubTotal();
$customerId = 0;
if($customerId = $order->getCustomerId())
{
$customer_id = $customer->getId();
}
$discountAmount = 10;
$duration = 30;
$discountIn = 'by_fixed';
Mage::getModel('offer/offer')->createCoupon($customer_id, $discountAmount, $duration, $discountIn);
}
}
}
Added above function createCoupon in model file:
class Mycompany_Offer_Model_Offer extends Mage_Core_Model_Abstract
/**
* Function to create coupon dynamically
*
* @param int $customer_id
* @param int $discount
* @param int $days
* @param text $type
* @return nothing
*/
public function createCoupon($customer_id, $discount,$days,$type)
{
$customer = Mage::getModel('customer/customer')->load($customer_id);
$customerGroupIds = Mage::getModel('customer/group')->getCollection()->getAllIds();
$websitesId = Mage::getModel('core/website')->getCollection()->getAllIds();
$customer_name = $customer->getName();
$couponCode = Mage::helper('core')->getRandomString(9);
$model = Mage::getModel('salesrule/rule');
$model->setName('Discount for ' . $customer_name);
$model->setDescription('Discount for ' . $customer_name);
$model->setFromDate(date('Y-m-d'));
$model->setToDate(date('Y-m-d', strtotime('+'.$days.' days')));
$model->setCouponType(2);
$model->setCouponCode($couponCode);
$model->setUsesPerCoupon(1);
$model->setUsesPerCustomer(1);
$model->setCustomerGroupIds($customerGroupIds);
$model->setIsActive(1);
$model->setConditionsSerialized('');
$model->setActionsSerialized('');
$model->setStopRulesProcessing(0);
$model->setIsAdvanced(1);
$model->setProductIds('');
$model->setSortOrder(1);
$model->setSimpleAction($type);//$model->setSimpleAction('by_fixed');
$model->setDiscountAmount($discount);
$model->setDiscountStep(0);
$model->setSimpleFreeShipping(0);
$model->setTimesUsed(0);
$model->setIsRss(0);
$model->setWebsiteIds($websitesId);
try {
$model->save();
return $model->getData();
} catch (Exception $e) {
print_r($e->getMessage());exit;
Mage::log($e->getMessage());
}
}
}
Mage::getModel('offer/offer')->createCoupon($customer_id, $discountAmount, $duration, $discountIn);
As per above syntax we have to pass parameters like 'customer id', 'discount amount', 'duration' (in days), and 'discountIn'( like 'by_percent' or 'by_fixed')
class NameSpace_Module_Model_Observer
{
$productId = 1;
$couponId = 'xyz';
public function yourObserverMethod($observer)
{
$quote = $event->getQuote();
$item = $event->getItem();
// add
if ($quote->getCouponCode() == $this->couponId && !in_array($this->productId, $item->getAllIds())){
// add product
}
//remove
if($quote->getCouponCode() != $this->couponId && in_array($this->productId, $item->getAllIds()){
// remove product
}
}
}
See
Mage::dispatchEvent('salesrule_validator_process', array(
'rule' => $rule,
'item' => $item,
'address' => $address,
'quote' => $quote,
'qty' => $qty,
'result' => $result,
));
Best Answer
The event
salesrule_validator_process
passes most of the objects that you are looking for.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.