仅当商店有可用代码时,Magento才会显示折扣代码框

时间:2013-09-05 14:12:13

标签: magento

我正在尝试让我的购物车显示折扣代码框,前提是该特定网站/商店(我有两个)有可用的折扣代码。

这是我到目前为止所得到的(包含在我自己的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; ?>

如果您对在所有网站/商店中检查折扣代码感到满意,这样可以正常工作,但我想更新它,以便只考虑当前的网站/商店。

1 个答案:

答案 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();