我认为更好的方法是检查是否有使用优惠券的折扣规则。因为折扣规则可以有一个指定的优惠券而不是优惠券列表。为此,请使用以下代码:
$coupon = Mage::getModel('salesrule/rule');
$todayEndOfDayDate = Mage::app()->getLocale()->date()
->setTime('23:59:59')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$todayStartOfDayDate = Mage::app()->getLocale()->date()
->setTime('00:00:00')
->toString(Varien_Date::DATETIME_INTERNAL_FORMAT);
$validCouponRules = $coupon->getCollection()
->addFieldToFilter('coupon_type', 2)//rules that have coupons fixed or generated
->addFieldToFilter('website_ids', array('finset'=>Mage::app()->getWebsite()->getId())) //filter rules available for current website
->addFieldToFilter('to_date', array('or'=> array(
0 => array('date' => true, 'from' => $todayEndOfDayDate), // filter rules that end later than today
1 => array('is' => new Zend_Db_Expr('null'))) //or rules that don't have an end date
), 'left')
->addFieldToFilter('from_date', array('or'=> array(
0 => array('date' => true, 'to' => $todayStartOfDayDate), //filter rules that started today or earlier
1 => array('is' => new Zend_Db_Expr('null'))) // or rules that don't have a start date
), 'left');
$validCoupons = $validCouponRules->count();