我正在尝试让我的购物车显示折扣代码框,前提是该特定网站/商店(我有两个)有可用的折扣代码。
这是我到目前为止所得到的(包含在我自己的coupon.phtml
)
<?php
/** @var $coupon Mage_SalesRule_Model_Coupon */
$coupon = Mage::getModel('salesrule/coupon');
$validCoupons = $coupon->getCollection()
->addFieldToFilter('expiration_date', array('gt' => NOW()))
->count();
?>
<?php if($validCoupons > 0): ?>
<!-- discount code form here -->
<?php endif; ?>
如果您对在所有网站/商店中检查折扣代码感到满意,这样可以正常工作,但我想更新它,以便只考虑当前的网站/商店。
答案 0 :(得分:2)
我认为更好的方法是检查是否存在使用优惠券的折扣规则。由于折扣规则可以具有指定的优惠券而不是优惠券列表。为此,请使用以下代码:
$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();